Search code examples
linuxshellbatch-processing

batch run of shell sript with passing values


I have a shell script (test.sh) which has some arguments to be passed during execution in linux. For example,

# sh test.sh        #My script
Choose model name
0:ESM 1:WED 2:PWD
1                   #I need to choose one from the above
Choose time period
0:2050 1:2080 2:2100
2                   #I need to choose one from the above
Downloading WED data for 2100    #download started
....

I am looking for a command to execute the test.sh for the above example. I tried with

sh test.sh > & out &

It is terminating without asking me anything.


Solution

  • I guess you are looking for a way to pass input to a script in a batch-way. Just pipe it:

    { echo 1; echo 2; } | sh test.sh
    

    Alternatively, I sometimes do it with printf "%s\n", which doesn't need to do { } braces for multiple lines of input.

    printf "%s\n" 1 2 "another arg" "etc.." | sh test.sh