0% found this document useful (0 votes)
40 views3 pages

Solution To Part Three What Else Are Tests Good For?

This document discusses using tests and if statements in shell scripts to conditionally execute parts of a script. It explains that an if statement allows executing a block of commands only if the result of a test or command is true. Tests can check things like whether parameters are within a given range to filter out invalid values. The document also covers using the exit builtin command to stop script execution and set an exit status code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views3 pages

Solution To Part Three What Else Are Tests Good For?

This document discusses using tests and if statements in shell scripts to conditionally execute parts of a script. It explains that an if statement allows executing a block of commands only if the result of a test or command is true. Tests can check things like whether parameters are within a given range to filter out invalid values. The document also covers using the exit builtin command to stop script execution and set an exit status code.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

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

If you examine the multi-sizes script in the scripts subdirectory of


your home directory, you will see that it is a version of the multi-50-500-
5000 script that has been modified as shown above.

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.

Version: Lent 2020 27 Version: Lent 2020 28


if statement
Using tests (1) Failure command
(or test) Do something only
We’ve met (integer) arithmetic tests. if some command
(or test) is true
Suppose we’d like to test to see whether some of Success
our parameters are within a certain range (say
1 to 1000000). If they are not, we shouldn’t do commands
anything, i.e.
to do if true
If parameter < 1 or parameter > 1000000 stop
executing the script…

How do we do this? rest


of script
[email protected] Simple Shell Scripting for Scientists: Day Three 29 [email protected] Simple Shell Scripting for Scientists: Day Three 30

We can decide whether a collection of commands should be executed


using an if statement. An if statement executes a collection of
commands if and only if the result of some command or test is true.
(Recall that the result of a command is considered to be true if it
returns an exit status of 0 (i.e. if the command succeeded)).

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(!).

Version: Lent 2020 29 Version: Lent 2020 30


if exit
Do something only if some command is To stop executing a shell script:
true
exit
if <command> ; then …can explicitly set an exit status thus:
<some commands>
exit value
fi

[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

You might also like