简体   繁体   English

如何在Shell脚本中同时运行两个命令?

[英]how to run two commands simultaneously in shell script?

I am trying to make a shell script run jeykll -w serve and sass --watch style.scss:style.css so I don't have to worry each time I want to develop locally. 我正在尝试让一个外壳脚本运行jeykll -w servesass --watch style.scss:style.css因此我不必每次都想在本地开发时就担心。 So I made a shell script, but obviously, if I put the jekyll command first, it won't run the sass one till jekyll is done. 所以我制作了一个shell脚本,但是显然,如果我先输入jekyll命令,那么在jekyll完成之前,它不会运行sass。 So how can run two commands at once? 那么如何一次运行两个命令呢? Do I have to make a command to open another tab in the terminal and then run sass? 我是否必须发出命令以打开终端中的另一个选项卡,然后运行sass? there must be a better way of doing that. 必须有一个更好的方法。

You are looking to fork the process so the current shell is not waiting on the command I assume. 您正在寻找分叉的过程,以便当前的外壳程序不等待我假设的命令。

jeykll -w serve && sass --watch style.scss:style.css

The above will wait for the first command to complete with an non-errored status 上面的内容将等待第一个命令以非错误状态完成

jeykll -w serve ; sass --watch style.scss:style.css

The above will wait for the first command to complete and disregard the exit status. 上面将等待第一个命令完成并忽略退出状态。

So if I understand correctly you want multiple commands to run near simultaneously. 因此,如果我理解正确,则希望多个命令同时运行。 For this you use the & operator at the end of a command reference. 为此,请在命令参考的末尾使用&运算符。

The Bash & (ampersand) is a builtin control operator used to fork processes. Bash&(与号)是用于派生进程的内置控制运算符。 From the Bash man page, "If a command is terminated by the control operator &, the shell executes the command in the background in a subshell". 在Bash手册页中,“如果命令被控制操作员&终止,则外壳程序将在子外壳程序的后台执行该命令”。

jeykll -w serve &
sass --watch style.scss:style.css & 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM