Solution To Part Three What Else Are Tests Good For?
Solution To Part Three What Else Are Tests Good For?
#!/bin/bash
set -e
We have seen that we can use tests in
…
# Read in parameters from standard input while loops. What else are they good
# and then run program with them for?
# and run it again and again until there are no more
while read alpha beta zeta delta population junk ; do
# Instead of using read in value for size,
# cycle through command line arguments. Suppose we know some (valid)
for population in "$@" ; do
# Run program
parameters for our program produce no
run_program "$alpha" "$beta" "$zeta" "$delta" "$population" interesting output. Could we use some
done tests to filter these out?
…
[email protected] Simple Shell Scripting for Scientists: Day Three 27 [email protected] Simple Shell Scripting for Scientists: Day Three 28
You should be able to tell what all the highlighted parts of the shell script
above do, and you should be able to see why this is a solution to this part of
the exercise – if there is anything you don’t understand, or if you had any
difficulty with this part of the exercise, please let the course giver or a
demonstrator know.
You can test that this script works by doing the following:
$ cd
$ rm –f *.dat *.png stdout-* logfile
$ cat scripts/param_set | scripts/multi-sizes 50 500 500
$ ls
You should see that a number of PNG and .dat files have been produced.
Note that even if set -e is in effect, or the first line of our shell
script is
#!/bin/bash -e
the shell script will not exit if the result of the command or test the
if statement depends on is false (i.e. it returns a non-zero exit
status), since if it did, this would make if statements fairly
useless(!).
[email protected] Simple Shell Scripting for Scientists: Day Three 31 [email protected] Simple Shell Scripting for Scientists: Day Three 32
We use an if statement like this: The exit shell builtin command causes a shell script to exit (stop executing)
if <command> ; then and can also explicitly set the exit status of the shell script (if you specify a
value for the exit status).
<some commands>
Recall that the exit status is an integer between 0 and 255, and should be 0
fi only if the script was successful in what it was trying to do. If the script
where <command> is either a command or a test, and <some encounters an error it should set the exit status to a non-zero value.
commands> is a collection of one or more commands. Note that if
<command> is false the shell script will not exit, even if set -e is in effect, If you don’t give exit an exit status then the exit status of the shell script
or the first line of the shell script is #!/bin/ will be the exit status of the last command executed by the script before it
bash -e reached the exit shell builtin command.
In a similar manner to for and while loops, you can put the then on a
separate line, in which case you can omit the semi-colon (;), i.e. (If you don’t have an exit shell builtin command in your shell script, then
if <command> your script will exit when it executes its last command. In this case its exit
status will be the exit status of the last command executed by your script.)
then
<some commands>
fi
Now, we just need to know how to tell our script to stop executing and we will
have all the pieces we need to modify our script to behave the way we want…
Version: Lent 2020 31 Version: Lent 2020 32