Bash 将exec与&;一起使用时;,最后一个命令不运行
如果/Bash 将exec与&;一起使用时;,最后一个命令不运行,bash,unix,Bash,Unix,如果/fi未运行,之后的代码似乎未运行。以下是我所拥有的: 我有一个脚本/my/scripts/dir/directoryPercentFull.sh: directoryPercentFull="$(df | grep '/aDir/anotherDir' | grep -o '...%' | sed 's/%//g' | sed 's/ //g')" if [ $directoryPercentFull -gt 90 ] then echo $directoryPercentFull
fi
未运行,之后的代码似乎未运行。以下是我所拥有的:
我有一个脚本/my/scripts/dir/directoryPercentFull.sh:
directoryPercentFull="$(df | grep '/aDir/anotherDir' | grep -o '...%' | sed 's/%//g' | sed 's/ //g')"
if [ $directoryPercentFull -gt 90 ]
then
echo $directoryPercentFull
exec /someDir/someOtherDir/test01.sh &
exec /someDir/someOtherOtherDir/test02.sh &
exec /someDir/yetAnotherDir/test03.sh
fi
echo "Processing Done"
正在调用的脚本包括:
/someDir/someOtherDir/test01.sh
#!/usr/bin/env bash
echo "inside test01.sh"
sleep 5
echo "leaving test01.sh"
/someDir/someotherdir/test02.sh
#!/usr/bin/env bash
echo "inside test02.sh"
sleep 5
echo "leaving test02.sh"
/someDir/yetAnotherDir/test03.sh
#!/usr/bin/env bash
echo "inside test03.sh"
sleep 5
echo "leaving test03.sh"
通过将cd刻录到/my/scripts/dir,然后执行./directoryPercentFull.sh来运行脚本,将给出:
输出:
预期产出:
93
inside test01.sh
inside test02.sh
inside test03.sh
leaving test01.sh
leaving test02.sh
leaving test03.sh
Processing Done
echo命令的顺序并没有什么大不了的,不过如果有人知道为什么要执行3,2,1,然后执行3,1,2,我也不会讨厌解释
但是,我没有完成最后的处理。有人知道为什么在/my/scripts/dir/directoryPercentFull.sh
中没有出现最后的回显吗?我特意没有将&
放在最后一个exec
语句之后,因为我不想让if
/fi
后面的内容运行,直到所有处理完成
/someDir/someOtherDir/test01.sh &
/someDir/someOtherOtherDir/test02.sh &
/someDir/yetAnotherDir/test03.sh
摆脱所有的exec
sexec
导致shell进程被给定的命令替换,这意味着shell不会继续执行更多的命令
echo命令的顺序并没有什么大不了的,不过如果有人知道为什么要执行3,2,1,然后执行3,1,2,我也不会讨厌解释
打印输出可以是任何顺序。这三个脚本在并行进程中运行,因此无法确定它们的打印输出顺序。您的问题可以归结为exec echo 1;echo 2
。您不需要使用exec来运行命令,从每行的开头删除这些exec,直接调用脚本。@melpomene,我想我知道exec在做什么。我在stackoverflow上发现了这一点,并假设这样做是有效的:链接的答案恰好起作用,因为&
-lessexec
是脚本中的最后一个命令。它不应该有那些exec
s,回答者应该提到它。@Rorylaham,…正如John所说,输出是无序的;仅仅因为输出顺序在一次运行(或在给定系统负载水平下执行的一组运行)中以给定的方式出现,并不意味着它将以一致的方式运行。这就像Python中的字典顺序;如果您正在以特定的方式进行测试,它可能看起来是一致的,但是如果不能保证它是一致的,那么编写假定不是一致的代码将对您不利。
/someDir/someOtherDir/test01.sh &
/someDir/someOtherOtherDir/test02.sh &
/someDir/yetAnotherDir/test03.sh