Running Several Commands in Sequence
Problem
You need to run several commands, but some take a while and you don’t want to wait for the last one to finish before issuing the next command.
Solution
There are three solutions to this problem, although the first is rather trivial: just keep typing. A Linux or Unix system is advanced enough to be able to let you type while it works on your previous commands, so you can simply keep typing one command after another.
Another rather simple solution is to type those commands into a file and then tell bash to execute the commands in the file—i.e., a simple shell script.
Assume that we want to run three commands: long, medium, and short, each of whose execution time is reflected in its name. We need to run them in that order, but don’t want to wait around for long to finish before starting the other commands. We could use a shell script (aka batch file). Here’s a primitive way to do that:
$ cat > simple.script long medium short ^D # Ctrl-D, not visible $ bash ./simple.script
The third, and arguably best, solution is to run each command in sequence. If you want to run each program, regardless if the preceding ones fail, separate them with semicolons:
$ long ; medium ; short
If you only want to run the next program if the preceding program worked, and all the programs correctly set exit codes, separate them with double-ampersands:
$ long && medium && short
Discussion
The cat example was just a very primitive way to enter text into a file. We redirect the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access