简体   繁体   English

Shell 脚本一个接一个地运行

[英]Shell scripts run one after another

I have 5 shell scripts.我有 5 个 shell 脚本。 Each has a java command.每个都有一个java命令。 Previous jobs output is input to the next job.以前的作业 output 输入到下一个作业。

I created a superScript.sh我创建了一个superScript.sh

//mail - to inform beginning
sh script1.sh;
sh script2.sh;
sh script3.sh;
sh script4.sh;
sh script5.sh;
//mail to inform end

Sample script1.sh示例script1.sh

cd toBaseDirectory;
java -cp /path/to/application.jar main.class parameter

But all the jobs are started at the same time.但是所有的工作都是同时开始的。 How can I make this sequential?我怎样才能使这个顺序?

Try to run javas like this尝试像这样运行javas

java -cp /path/to/application.jar main.class parameter & wait

If want to run the second command only if the first exited successfully.如果只想在第一个成功退出时运行第二个命令。 To do so, join them with &&为此,请使用 && 加入他们

command1 && command2

simple Example简单的例子

~$ cat abc.sh 
#!/usr/bin/env bash
echo "hi"

~$ cat pqr.sh 
#!/usr/bin/env bash
echo "batman say :  $1"

If abc.sh execute successfully then only execute pqr.sh如果 abc.sh 执行成功则只执行 pqr.sh

~$ retval=$(./abc.sh) && result=$(./pqr.sh "$retval") && echo "$result"
batman say :  hi

you can also try similar approach with your java command execution using shell script您也可以尝试使用 shell 脚本执行 java 命令的类似方法

Note: To execute Shell scripts as command sequentially, waiting for the first to finish before the next one starts.注意:要按命令顺序执行 Shell 脚本,等待第一个完成后再开始下一个。 You can use;您可以使用; command1; command2

wait waits for a process to finish wait等待进程完成

You can also choose to run the second command only if the first exited successfully.您也可以选择仅在第一个命令成功退出时才运行第二个命令。 To do so, join them with &&为此,请使用&&加入他们

cmd1 && cmd2

but In repetitive cases like this I recommended using a simple loop in the body of your script:但在这样的重复情况下,我建议在脚本正文中使用一个简单的循环:

for n in {1..5} ; do sh script${n}.sh ; done

The loop not only to run them in order but is easier to tweak and reuse when needed, with using brace expansion循环不仅可以按顺序运行它们,而且在需要时更容易调整和重用,使用大括号扩展

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

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