如何在Shell脚本中同时运行两个命令? [英] how to run two commands simultaneously in shell script?

查看:1067
本文介绍了如何在Shell脚本中同时运行两个命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使Shell脚本运行jeykll -w servesass --watch style.scss:style.css,所以我不必每次都想在本地进行开发时就担心.所以我制作了一个shell脚本,但是显然,如果我先输入jekyll命令,那么在jekyll完成之前,它不会运行sass.那么如何一次运行两个命令呢?我是否必须发出命令以在终端中打开另一个选项卡,然后运行sass?必须有一个更好的方法.

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. 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. 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? there must be a better way of doing that.

推荐答案

您希望分叉该进程,以使当前的Shell不再等待我假定的命令.

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& (&"号)是用于派生进程的内置控制运算符.在Bash手册页上,如果命令被控制操作员& amp;终止,则外壳程序将在子外壳程序的后台执行该命令."

The Bash & (ampersand) is a builtin control operator used to fork processes. 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".

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

这篇关于如何在Shell脚本中同时运行两个命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆