简体   繁体   中英

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. I want to combine them to make a single .sh script that uses both. 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:

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. 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.

$? stores the return code of the previously executed command, so we validate that it is equal to 0 before continuing to bar.sh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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