[英]run two shell scripts where first requires user input
I have two .sh scripts that need to be run in order, where the first requires the user to input a filename. 我有两个.sh脚本需要按顺序运行,其中第一个需要用户输入文件名。 I want to combine them to make a single .sh script that uses both. 我想将它们结合起来以制作一个同时使用两者的.sh脚本。 It is typically run in succession like so: 它通常像这样连续运行:
foo.sh input1 input2
bar.sh > file.csv
They each take about an hour to run. 它们每个都需要大约一个小时才能运行。 Is there a way to make a command that runs both in a single .sh script so the input looks like this: 有没有一种方法可以使一个命令同时在单个.sh脚本中运行,因此输入如下所示:
newscript.sh input1 input2
where the output is: 输出为:
file.csv
If you wrote the original scripts you can refactor them into one script. 如果您编写了原始脚本,则可以将它们重构为一个脚本。 But it sounds like you don't want to modify the scripts. 但这听起来像您不想修改脚本。 You can instead create a script that wraps the execution of both scripts. 您可以改为创建一个包装两个脚本的执行的脚本。
If the second script requires the first to be successful then just check the return code of the first (assuming that the first script properly returns non-zero in an error scenario) before executing the second script: 如果第二个脚本要求第一个脚本成功,则在执行第二个脚本之前,只需检查第一个脚本的返回码(假设第一个脚本在错误情况下正确返回非零):
#!/bin/bash
foo.sh $1 $2
if [ $? -eq 0 ]; then
bar.sh > file.csv
else
echo "foo.sh returned with an error. Skippping execution of bar.sh due to error."
fi
$1
and $2
are the first and second parameters to the script and we will redirect them to foo.sh. $1
和$2
是脚本的第一个和第二个参数,我们会将它们重定向到foo.sh。 I often like to write a function to check and handle command line inputs before calling foo.sh but that may not be worth your time if foo.sh can do that error checking quickly when executed. 我经常喜欢在调用foo.sh之前编写一个函数来检查和处理命令行输入,但是如果foo.sh在执行时可以快速进行错误检查,那可能不值得您花时间。
$?
stores the return code of the previously executed command, so we validate that it is equal to 0 before continuing to bar.sh. 存储先前执行的命令的返回码,因此我们在继续执行bar.sh之前先验证它等于0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.