如何一次运行多个 Unix 命令?
我对 Unix 还是个新手。 Unix下是否可以同时运行多个命令?比如将我想要运行的所有命令写在一个文件中,那么在我调用该文件后,它将运行该文件中的所有命令?或者有什么我不知道的方法(或更好的方法)?
感谢您提出所有意见和建议,我将不胜感激。
I'm still new to Unix. Is it possible to run multiple commands of Unix in one time? Such as write all those commands that I want to run in a file, then after I call that file, it will run all the commands inside that file? or is there any way(or better) which i do not know?
Thanks for giving all the comments and suggestions, I will appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
简短的回答是,是的。这个概念称为 shell 脚本或 bash 脚本(一种常见的 shell)。为了创建一个简单的 bash 脚本,请创建一个文本文件,顶部包含以下内容:
然后将命令粘贴到其中,一个到一行。
保存您的文件,通常使用 .sh 扩展名(但不是必需的),您可以像这样运行它:
或者您可以更改权限以使其可执行:
然后像这样运行它:
此站点和网络上有很多资源可供使用,以了解更多信息信息,如果需要的话。
Short answer is, yes. The concept is known as shell scripting, or bash scripts (a common shell). In order to create a simple bash script, create a text file with this at the top:
Then paste your commands inside of it, one to a line.
Save your file, usually with the .sh extension (but not required) and you can run it like:
Or you could change the permissions to make it executable:
Then run it like:
Lots of resources available on this site and the web for more info, if needed.
只需用
&&
分隔命令Just separate your commands with
&&
我们可以使用 ; 在 shell 中运行多个命令作为多个命令之间的分隔符
例如,
如果我们使用 &&作为分隔符,如果上一个命令成功,则下一个命令将运行。
We can run multiple commands in shell by using ; as separator between multiple commands
For example,
If we use && as separator then next command will be running if last command is successful.
您还可以使用分号“;”并运行多个命令,例如:
$ls ; WHO
you can also use a semicolon ';' and run multiple commands, like :
$ls ; who
是的,只需将所有命令放入一个文件中,然后
这将按顺序运行命令。如果您希望它们全部并行运行(即不等待命令完成),则将
&
添加到文件中每行的末尾Yep, just put all your commands in one file and then
This will run the commands in sequence. If you want them all to run in parallel (i.e. don't wait for commands to finish) then add an
&
to the end of each line in the file如果要在命令行中使用多个命令,可以使用管道来执行操作。
它将给出该文件中存在“Hello”的次数。
If you want to use multiple commands at command line, you can use pipes to perform the operations.
It will give number of times "Hello" exist in that file.
当然。它被称为“shell 脚本”。在bash中,将所有命令放在一个后缀为“sh”的文件中。然后运行这个:
然后输入
or
或者 只是
Sure. It's called a "shell script". In bash, put all the commands in a file with the suffix "sh". Then run this:
then type
or
or just
要让命令实际同时运行,您可以使用 zsh 的工作能力
$ zsh -c "[command1] [command1 argument] & ; [command2] [command2 argument]"
或者如果您正在将 zsh 作为当前 shell 运行:
$ ping google.com & ; ping 127.0.0.1
;
是一个标记,可让您将另一个命令放在直接在第一个命令之后运行的同一行上。&
是放置在命令后面的标记,用于在后台运行该命令。To have the commands actually run at the same time you can use the job ability of zsh
$ zsh -c "[command1] [command1 arguments] & ; [command2] [command2 arguments]"
Or if you are running zsh as your current shell:
$ ping google.com & ; ping 127.0.0.1
The
;
is a token that lets you put another command on the same line that is run directly after the first command.The
&
is a token placed after a command to run it in the background.