bash脚本执行顺序

jav*_*y79 7 bash scripting

bash 脚本中的行是否按顺序执行?我看不出有什么理由不这样做,但我对 bash 脚本确实很陌生,而且我有几个命令需要按顺序执行。

例如:

#!/bin/sh
# will this get finished before the next command starts?
./someLongCommand1 arg1
./someLongCommand2 arg1
Run Code Online (Sandbox Code Playgroud)

小智 8

是的,它们是顺序执行的。但是,如果您在后台运行程序,则脚本中的下一个命令将在后台命令启动后立即执行。

#!/bin/sh
# will this get finished before the next command starts?
./someLongCommand1 arg1 &
./someLongCommand2 arg1 &
Run Code Online (Sandbox Code Playgroud)

将导致脚本几乎立即完成;但是,其中启动的命令将不会完成。(您可以通过在名称后面添加与号 (&) 在后台启动命令。


Jim*_*wis 6

是的...除非您特意在后台运行其中一个命令,否则一个命令将在下一个命令开始之前完成。