bash 如何按顺序运行多个程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12021032/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 03:04:02  来源:igfitidea点击:

How to run multiple programs in a sequence?

linuxbashshell

提问by user1280282

I have three different executables to run in sequence, two of them are byte executables. I want to automatically run them in sequence. It requires user inputs also initially. How to automate this with a script? Can anyone redirect to some tutorial or examples?

我有三个不同的可执行文件要按顺序运行,其中两个是字节可执行文件。我想按顺序自动运行它们。它最初也需要用户输入。如何使用脚本自动执行此操作?任何人都可以重定向到一些教程或示例吗?

回答by David

Separate by semicolons for sequential execution:

用分号分隔以便顺序执行:

cmd1 ; cmd2 ; cmd3

Separate by '&&' so that the next cmd runs only if the previous one succeeded:

用 '&&' 分隔,以便下一个 cmd 仅在前一个成功时运行:

cmd1 && cmd2 && cmd3

Here's a tutorial. See section 4.3.

这是一个教程。见第 4.3 节。

回答by Debaditya

You can write a simple shell script for it.

您可以为它编写一个简单的 shell 脚本。

Test.sh

测试文件

#!/bin/sh

<Paste your commands>

Execute the code :

执行代码:

sh Test.sh

回答by user3708117

You're requirements are pretty vague. "I need stuff to execute in sequence some which require user input". Bash affords us a lot of flexibility for addressing these issues on a case by case basis but to really give you a GOOD answer we would probably want to know what the commands are and what inputs are required. Just want to execute commands in sequence? semicolons are for you

你的要求很模糊。“我需要按顺序执行一些需要用户输入的东西”。Bash 为我们提供了很大的灵活性来逐案解决这些问题,但要真正给您一个好的答案,我们可能想知道命令是什么以及需要哪些输入。只想按顺序执行命令?分号是给你的

cmd1 ; cmd2; cmd3

Does it matter if one of the commands fails or should the second command only be executed upon successful completion of the first? If these exit codes matter you are probably better off using double &&

如果其中一个命令失败,或者第二个命令应该只在第一个命令成功完成后执行,这是否重要?如果这些退出代码很重要,那么最好使用 double &&

cmd1 && cmd2 && cmd3

A lot of times user inputs can be redirected into a program or specified within the command line using arguments for example:

很多时候,用户输入可以重定向到程序中或使用参数在命令行中指定,例如:

{ echo "remotecommand 1" } | telnet localhost 1234

or

或者

ssh -o StrictHostKeyChecking=no 192.168.1.1

Outside of that you are starting to require expect scripting. So you see there are many cases which can come up where automation is possible but without much to go on it's hard to give you a specific answer.

除此之外,您开始需要期望脚本。因此,您会看到很多情况下可以实现自动化,但没有太多事情要做,很难给您一个具体的答案。