CSC 161 Grinnell College Spring, 2009
 
Imperative Problem Solving and Data Structures
 

Linux Scripts

Reading

Please read the following materials carefully:

Additional Notes

The Bash shell allows users to interact with the computer through a terminal window, organizing commands that tie various programs together. First developed by Brian Fox in 1987, the Bash shell seeks to incorporate numerous features from earlier shells for the Unix operating system. In particular, the Bash shell draws upon the Bourne shell (developed by Ken Thompson and then Stephen Bourne around 1977), the Korn shell (developed by David Korn in the early 1980s), and the C shell (developed by Bill Joy, based on Thompson's earlier shells).

This history is particularly evident in the syntax allowed for if expressions. Bash allows interprets each syntax properly — but do not try to mix and match the different versions. The following examples illustrate acceptable Bash syntax, following two different ancesters.

Task Bourne shell style Korn shell style
numeric test
a > 0
if [ $a -gt \0 ]; then
    echo positive
fi
if (( $a > 0 )); then
    echo positive
fi
numeric test
a ≤ 0
if [ $a -le 0 ]; then
    echo non-positive
else
    echo positive
fi
if (( $a <= 0 )); then
    echo non-positive
else
    echo positive
fi
numeric test
a < 0 and a == 0
if [ $a -lt 0 ]; then
    echo negative
elif [ $a -eq 0 ]; then
    echo zero
else
    echo positive
fi
if (( $a <= 0 )); then
    echo non-positive
elif (( $a == 0 )); then
    echo zero
else
    echo positive
fi
numeric test
0 ≤ a &le 10
if [ 0 -le $a -a $a -le 10 ]; then
    echo between 0 and 10
else
    echo not between 0 and 10
fi
if (( (0 <= $a) && ($a <= 10) )); then
    echo between 0 and 10
else
    echo not between 0 and 10
fi

For reference, all of these code segnments are available in the Bash shell bash-example-1


This document is available on the World Wide Web as

http://www.walker.cs.grinnell.edu/courses/161.sp09/readings/reading-linux-scripts.shtml

created 21 April 2008
last revised 6 May 2009
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.