简体   繁体   English

运行两个shell脚本,首先需要用户输入

[英]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.

相关问题 运行循环 N 次的 shell 脚本,其中 N 是用户输入 - shell script to run for loop N times where N is user input 如何在启动时运行两个Shell脚本? - How to run two shell scripts at startup? 当服务器负载超过20时运行两个脚本的Shell脚本 - Shell script to run two scripts when server load is above 20 rails使用用户输入运行shell命令 - rails run shell command with user input 运行文件夹中的所有 shell 脚本 - Run all shell scripts in folder 在文件夹中运行“ N”个Shell脚本 - Run “N” Shell Scripts in Folder 并行运行两个shell脚本,然后在两个完成后运行另一个 - Run two shell scripts in parallel then run another one after both finished Shell脚本新手-如何自动运行某些脚本,其中必须对运行这些脚本时被问到的一些问题做出响应 - shell scripting newbie- how to automate running some scripts where a response has to be given to some questions asked when those scripts are run 是否可以编写一个接受输入的 shell 脚本,然后将 ssh 连接到服务器并在我的本地机器上运行脚本? - Is it possible to write a shell script that takes input then will ssh into a server and run scripts on my local machine? Shell:如何并行运行两个脚本A和B并在A结束时停止B - Shell: how to run two scripts A and B in parallel and stop B when A is over
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM