![迭代脚本执行:A调用B,B调用A](https://linux22.com/image/104285/%E8%BF%AD%E4%BB%A3%E8%84%9A%E6%9C%AC%E6%89%A7%E8%A1%8C%EF%BC%9AA%E8%B0%83%E7%94%A8B%EF%BC%8CB%E8%B0%83%E7%94%A8A.png)
我有一个脚本scriptA.sh
,如果一个变量假定某个值,它必须执行另一个脚本scriptB.sh
来执行某些操作,然后调用 a scriptA.sh
,这将调用 ascriptB.sh
等等。
我画出执行情况:
ScriptA - ScriptB
|
|
_
|
_
|
|
_
|
_
我已经尝试过这种方式,但进程从未关闭
ScriptA.sh
./scriptB.sh &
exit
ScriptB.sh
./scriptA.sh &
exit
也是这样,但进程永远不会关闭
ScriptA.sh
./scriptB.sh && exit
ScriptB.sh
./scriptA.sh && exit
有什么建议吗?
实际脚本:
脚本A.sh:
#!/bin/bash
val=123
directory_path=`pwd`
script_name=$0
if [ $val -eq 123 ];then
echo "call B and exit"
./scriptB.sh $directory_path $script_name && exit 0
fi
脚本B.sh:
#!/bin/bash
directory_path=$1
exec_command=$2
cd
cd $directory_path
echo "call A and exit"
$exec_command && exit 0
答案1
更新: 为了保持结构,您可以创建这两个文件:
脚本文件
#!/bin/bash
./controller.sh a
脚本b.sh
#!/bin/bash
./controller.sh b
这是一个让它变得更容易的建议:
控制器.sh
#!/bin/bash
# run ./controller.sh a to run a/b
# run ./controller.sh b to run b/a
if [ "$1" == "a" ]; then
./ascript.sh
./bscript.sh
elif [ "$1" == "b" ]; then
./bscript.sh
./ascript.sh
else
echo "choose a or b ./controller.sh [a|b]"
exit 255
fi
exit 0
ascript.sh
#!/bin/bash
echo "script a"
# add code here
脚本文件
#!/bin/bash
echo "script b"
# add code here