我有一个像这样的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
答案 0 :(得分:0)
您需要在source
中child.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}}。