使用shell脚本设置环境变量(Linux)

时间:2015-01-05 12:04:34

标签: linux shell

我是shell脚本的新手。我编写了一个脚本来检查是否设置了ORACLE_HOME和ORACLE_SID。在这里我调用了其他脚本来设置env变量。


第一个脚本

while [ 1 -gt 0 ]
do
    echo -e "Please enter path of oracle home directory:\c"
    read DB_HOME
            if [ -d $DB_HOME ]
            then
                    ./oracle_env.sh $DB_HOME "test1"
                    echo "ORACLE_HOME has been set successfully!"
                    status="Y"
                    break
            else
            echo "Path or directory does not exist."
            fi
done

第二个脚本

#This script will set ORACLE_HOME and SID
export ORACLE_HOME=$1
export  ORACLE_SID=$2

当我以

运行第二个脚本时
./oracle_env.sh /u01/app/oracle test
它工作正常。我的意思是,当我跑步时

echo $ORACLE_HOME

它给出了像

这样的路径
/u01/app/oracle

现在问题是当我从第一个脚本运行相同的脚本时,它无法正常工作。

请帮帮我!!!

3 个答案:

答案 0 :(得分:4)

问题很简单:

如果您执行脚本,它将在新shell中启动,在那里设置环境并关闭shell。结果,第一个调用shell没有任何变化。

所以你必须使用source <shellscript>

在第一个shell中执行脚本

有关详细信息,请参阅man bash

我不知道你使用的是哪个shell。也许解决方案对于其他炮弹来说有点不同。

答案 1 :(得分:0)

尝试在终端中设置环境变量:(下面的代码是针对xampp而不是oracle,路径会根据你的要求而变化)

export PATH=/opt/lamp/bin:$PATH

您可以通过以下方式查看环境变量:

echo $PATH

看,这是否适合你。

答案 2 :(得分:0)

使用source(或)运行脚本。命令。

while [ 1 -gt 0 ]
do
    echo -e "Please enter path of oracle home directory:\c"
    read DB_HOME
            if [ -d $DB_HOME ]
            then
                    . oracle_env.sh $DB_HOME "test1" ## Here you run the script with . command.
                    echo "ORACLE_HOME has been set successfully!"
                    status="Y"
                    break
            else
            echo "Path or directory does not exist."
            fi
done

使用.命令运行您的第一个脚本。

$ . script.sh
相关问题