在其他脚本中访问和更新shell脚本变量

时间:2016-06-14 10:32:32

标签: shell

我有一个像这样的shell脚本 parent.sh 。我希望在 child.sh 中使用变量 flag ,在那里更新 flag 的值并在 parent.sh 。我怎样才能做到这一点。

#!/bin/bash
flag=999       #I want ths flag in child.sh and there i will update its value depending on some condition
$HOME/child.sh
if [ $? -eq $flag ];then   #The value of flag got changed on child.sh
echo "Success"
else
echo "Failed"
fi

我的 child.sh 如下

#!/bin/bash
hive -f $HOME/creat.hql          #It should be create.hql so it will fail.
if [ $? -eq 0 ];then
    flag=0
else
    flag=1     #This will execute and I want this change to be get reflected in parent.sh
fi

1 个答案:

答案 0 :(得分:0)

您需要在sourcechild.sh parent.sh,以便在当前子shell中执行:

#!/bin/bash
flag=999       #I want ths flag in child.sh and there i will update its value depending on some condition
. ./child.sh
if [ $? -eq $flag ];then   #The value of flag got changed on child.sh
echo "Success"
else
echo "Failed"
fi

您可以在采购flag后立即查看child.sh的vaule,以确保在child.sh内正确设置了help source。有关help .工作原理的详细信息,请参阅bash内的source或{{1}}。