bash 命令是否可以在上一个命令的结果之前继续?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2398388/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible for bash commands to continue before the result of the previous command?
提问by corydoras
When running commands from a bash script, does bash always wait for the previous command to complete, or does it just start the command then go on to the next one?
从 bash 脚本运行命令时,bash 是否总是等待上一个命令完成,还是只是启动命令然后继续执行下一个命令?
ie: If you run the following two commands from a bash script is it possible for things to fail?
即:如果您从 bash 脚本运行以下两个命令,是否可能会失败?
cp /tmp/a /tmp/b
cp /tmp/b /tmp/c
回答by Zach Hirsch
Yes, if you do nothing else then commands in a bash script are serialized. You can tell bash to run a bunch of commands in parallel, and then wait for them all to finish, but doing something like this:
是的,如果你什么都不做,那么 bash 脚本中的命令就会被序列化。您可以告诉 bash 并行运行一堆命令,然后等待它们全部完成,但执行以下操作:
command1 &
command2 &
command3 &
wait
The ampersands at the end of each of the first three lines tells bash to run the command in the background. The fourth command, wait
, tells bash to wait until all the child processes have exited.
前三行每一行末尾的与号告诉 bash 在后台运行命令。第四个命令,wait
告诉 bash 等待,直到所有子进程都退出。
Note that if you do things this way, you'll be unable to get the exit status of the child commands (and set -e
won't work), so you won't be able to tell whether they succeeded or failed in the usual way.
请注意,如果您以这种方式进行操作,您将无法获得子命令的退出状态(并且set -e
将无法工作),因此您将无法以通常的方式判断它们是成功还是失败。
The bash manualhas more information (search for wait
, about two-thirds of the way down).
在bash的手册有更多的信息(搜索wait
,关于一路下跌的三分之二)。
回答by Tareq Sha
add '&' at the end of a command to run it parallel. However, it is strange because in your case the second command depends on the final result of the first one. Either use sequential commands or copy to b and c from a like this:
在命令末尾添加“&”以并行运行它。但是,这很奇怪,因为在您的情况下,第二个命令取决于第一个命令的最终结果。要么使用顺序命令,要么像这样从 a 复制到 b 和 c:
cp /tmp/a /tmp/b &
cp /tmp/a /tmp/c &
回答by R Samuel Klatchko
Unless you explicitly tell bash to start a process in the background, it will wait until the process exits. So if you write this:
除非你明确告诉 bash 在后台启动一个进程,否则它会一直等到进程退出。所以如果你这样写:
foo args &
bash will continue without waiting for foo to exit. But if you don't explicitly put the process in the background, bash will wait for it to exit.
bash 将继续运行而无需等待 foo 退出。但是如果你没有明确地把进程放在后台,bash 会等待它退出。
Technically, a process can effectively put itself in the background by forking a child and then exiting. But since that technique is used primarily by long-lived processes, this shouldn't affect you.
从技术上讲,进程可以通过 fork 子进程然后退出来有效地将自己置于后台。但由于该技术主要用于长期存在的进程,因此这不会影响您。
回答by Ignacio Vazquez-Abrams
In general, unless explicitly sent to the background or forking themselves off as a daemon, commands in a shell script are serialized.
通常,除非明确发送到后台或将自己分叉为守护进程,否则 shell 脚本中的命令会被序列化。
回答by mingos
They wait until the previous one is finished. However, you can write 2 scripts and run them in separate processes, so they can be executed simultaneously. It's a wild guess, really, but I think you'll get an access error if a process tries to write in a file that's being read by another process.
他们等到上一个完成。但是,您可以编写 2 个脚本并在不同的进程中运行它们,因此它们可以同时执行。这是一个疯狂的猜测,真的,但我认为如果一个进程试图写入另一个进程正在读取的文件,你会得到一个访问错误。
回答by Devin Ceartas
I think what you want is the concept of a subshell. Here's one reference I just googled: http://www.linuxtopia.org/online_books/advanced_bash_scripting_guide/subshells.html
我认为您想要的是子外壳的概念。这是我刚刚用谷歌搜索的一个参考:http: //www.linuxtopia.org/online_books/advanced_bash_scripting_guide/subshells.html