0

我正在尝试同时运行多个命令。我通过将每个命令附加到看起来像的单个命令来在脚本中准备它

and=" & "
command=$command1$and$command2
eval $command

但是,当我运行它时,每个命令都会被分叉并正确完成,但它会挂在命令行上并且永远不会让我返回提示符。

示例(脚本 run.sh)。如果这两个命令是:

command1="echo 'Hello'"
command2="echo 'World'"

那么输出是:

person: ./run.sh
Hello
World
## It Hangs here

代替

person: ./run.sh
Hello
World
person: 

那么如何终止分叉进程,或者让脚本阻塞直到分叉进程完成?

4

3 回答 3

1

假设您的示例已简化(因为“echo”应立即完成并返回提示符),您只需将第二个命令置于后台即可。IOW,尝试:

command="$command1 & $command2 &"

请注意,我已经删除了您的“和”变量,因为它只是混淆。单曲&绝不是and手术。

于 2012-07-31T15:43:08.143 回答
0

Are you getting output like this:

~/dev/other $ echo foo &
[1] 522
~/dev/other $ foo
▯ # blink, blink, blink…

In this case you are being returned to the prompt, but the prompt is appearing before echo completes, and thus the string to be echoed is appearing after the prompt, followed by a newline. You can write your next command on that new line after the prompt, or just hit enter to get a new prompt.

If this is happening in a script, you can add the builtin wait to the end of the script to ensure that the script doesn't return the prompt before all of its backgrounded child processes are complete.

于 2012-07-31T15:45:29.107 回答
0

我认为你的命令应该是这样的:

command= $command1$and$command2$and
于 2012-07-31T15:50:11.583 回答