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.
3 Answers 3
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.
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– Galaxy2016年05月07日 05:27:50 +00:00Commented May 7, 2016 at 5:27
-
@Galaxy Yes, that is exactly what you do. And, yes, scripts can run scripts which can run scripts .....John1024– John10242016年05月07日 05:28:43 +00:00Commented May 7, 2016 at 5:28
-
So you don't need the
#!/bin/bashline when calling a script withbash script?Xen2050– Xen20502016年05月07日 05:55:32 +00:00Commented May 7, 2016 at 5:55 -
Correct. When calling
bash script(wherescriptis the full name of the file, including any extension), the#!/bin/bashline is optional.John1024– John10242016年05月07日 06:07:26 +00:00Commented May 7, 2016 at 6:07
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.
wait(seeman bashorhelp waitfrom bash), probably not needed here, but it will wait for a process/PID or all children to finish