1

I have several bash scripts, or rather one script that has to be run multiple times.

This script accepts several command line arguments and does something with them. I need to run this script multiple times in sequence, passing in different argument values. The problem is that this script is very long and it takes a lot of time to run.

I want to run this script once, with some arguments. Then I want to run this script again right away, passing in different argument values this time.

I need to give my finished results to my boss in the morning. One solution would be to just run the script once, and then sit at the computer all through the night, waiting for it to finish, and then run the script once again. But lack of sleep is not an option for me.

Is there some way to automate the running of scripts with predetermined argument values? By the way, I need to see not just the output of the scripts as the final result, but also the script as it was passed in to the command line with the appropriate arguments.

Perhaps it would be better to look at this image to understand my question better.

screenshot

  • You may be interested in the bash command wait (see man bash or help wait from bash), probably not needed here, but it will wait for a process/PID or all children to finish – Xen2050 May 7 '16 at 5:54
1

Simplest way to run one command after another is just to use ; Like:

command1 --options;command2 --options;command3 --options

If you wish to skip running commands if the previous one fails, you can do it with &&:

 command1 --options && command2 --options && command3 --options

Unlike pipe (|) or ampersand (&), the semicolon waits for the command to finish before the next command is run.

0

You need a for loop:

cd ~/Desktop/344/08
for arg in --resources --requesters --errors
do
    echo Starting: ./log_parse.sh "$arg" --day 10-20 --number 10 access_log_Jul95
    ./log_parse.sh "$arg" --day 10-20 --number 10 access_log_Jul95
done

Maybe you will want to save a copy of the output to a logfile as well as display it on a screen:

cd ~/Desktop/344/08
{
    for arg in --resources --requesters --errors
    do
        echo Starting: ./log_parse.sh "$arg" --day 10-20 --number 10 access_log_Jul95
        ./log_parse.sh "$arg" --day 10-20 --number 10 access_log_Jul95
    done
} 2>&1 | tee output.log

Put the above code in a file called, say, script.sh. Then, at the command prompt, run:

bash ./script.sh
  • So how exactly do I use this for-loop? Do I first need to create a script and then put the for-loop inside of it? Is it really possible to have a script that would run another script? – Galaxy May 7 '16 at 5:27
  • @Galaxy Yes, that is exactly what you do. And, yes, scripts can run scripts which can run scripts ..... – John1024 May 7 '16 at 5:28
  • So you don't need the #!/bin/bash line when calling a script with bash script? – Xen2050 May 7 '16 at 5:55
  • Correct. When calling bash script (where script is the full name of the file, including any extension), the #!/bin/bash line is optional. – John1024 May 7 '16 at 6:07
0

Check out Task Spooler (http://vicerveza.homeunix.net/~viric/soft/ts/) - there are packages for Debian.

Basically you can install it, then execute a bunch of tsc {script} {arguments} and it will stack the command in a queue which the spooler works on in the background.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.