Main index

Introducing UNIX and Linux


Introduction to shells

Overview
Why do we need a shell?
Shell syntax
      Types of shell command
      Simple commands
      Pipelines
      Grouping commands
      Exit status
      List commands
Arithmetic
      Operators and functions
Making decisions
      The 'test' statement
            Operators used by 'test'
      The 'if' statement
Loops
      'For' loops
      'While' and 'until' loops
Searching for files
      Arguments to 'find'
Formatted output
      Arguments to 'printf'
Passing information to scripts
      Scripts with arguments
      Parameter expansion
Summary
Exercises

List commands

A simple use of exit status is when using a list command. A list command is a sequence of pipelines separated by either || (pronounced 'or') or && (pronounded 'and'). In the case of an or-list

$ pipeline1 ||  pipeline2

pipeline1 is run, and if a non-zero exit status is returned pipeline2 is run. If pipeline1 returns 0, the exit status for this list command is 0, otherwise it is the status of pipeline2. Thus the or-list succeeds if either the first command or the second succeeds. In the case of an and-list

$ pipeline1 &&  pipeline2

pipeline1 is run, and if a zero exit status is returned pipeline2 is run. The exit status for this list command is that of pipeline1, if non-zero, otherwise that of pipeline2. An and-list succeeds if both its first and its second component succeed. Both or-lists and and-lists can be strung together, and the pipelines separated by || and && are evaluated from left to right.

Simple examples for || and && would be to check whether a command has in fact completed successfully:

mycommand || echo Cannot run mycommand
mycommand && echo mycommand ran OK

In the first line above, if mycommand fails - that is, returns exit status not zero - the subsequent echo command is run and informs you that mycommand failed. In the second, if mycommand succeeds the subsequent echo command is run.

Worked example

Compare files named file1 and file2, and if they are different mail yourself a message indicating the differences.
Solution: Use diff to compare the two files. We see from the manual page for diff that an exit status of 0 is returned only when the two arguments to diff are identical. You can therefore send the output of diff to a file and then mail yourself the contents of that file. || can be used so that the mail will only be performed if the diff returned non-zero exit status.

diff file1 file2 >diffout ||
> mailx -s "Output of diff" chris <diffout

Using && we can sequence commands so that subsequent commands will only run if earlier ones have been completed successfully.

Worked example

Compare files named file1 and file2, and if they are identical delete file2.
Solution: Since we do not require a list of any differences it will be quicker to use cmp which, like diff, returns 0 exit status if its arguments have the same contents. Use && to perform rm upon successful completion of cmp.

cmp file1 file2 && rm file2

Parentheses can also be used to group list commands so that, for instance,

command1 || ( command2 && command3 )

would cause command1 to be run, and if it failed the and-list command2 && command3 would then be run.

Two other commands it is appropriate to introduce here are true and false. Both these commands do nothing at all, but return exit status 0 and 1 respectively. We shall use them later on in this chapter. There is also a command : (colon), which has the same effect as true.


Copyright © 2002 Mike Joy, Stephen Jarvis and Michael Luck