Table 2.2. Bourne Shell Syntax and Constructs
The shbang line | The "shbang" line is the very first line of the script and lets the kernel know what shell will be interpreting the lines in the script. The shbang line consists of a #! followed by the full pathname to the shell, and can be followed by options to control the behavior of the shell.
EXAMPLE
#!/bin/sh
|
Comments | Comments are descriptive material preceded by a # sign. They are in effect until the end of a line and can be started anywhere on the line.
EXAMPLE
# this text is not
# interpreted by the shell
|
Wildcards | There are some characters that are evaluated by the shell in a special way. They are called shell metacharacters or "wildcards." These characters are neither numbers nor letters. For example, the *, ?, and [ ] are used for filename expansion. The <, >, 2>, >>, and | symbols are used for standard I/O redirection and pipes. To prevent these characters from being interpreted by the shell they must be quoted.
EXAMPLE
Filename expansion:
rm *; ls ??; cat file[1-3];
Quotes protect metacharacter:
echo "How are you?"
|
Displaying output | To print output to the screen, the echo command is used. Wildcards must be escaped with either a backslash or matching quotes.
EXAMPLE
echo "What is your name?"
|
Local variables | Local variables are in scope for the current shell. When a script ends, they are no longer available; i.e., they go out of scope. Local variables are set and assigned values.
EXAMPLE
variable_name=value
name="John Doe"
x=5
|
Global variables | Global variables are called environment variables. They are set for the currently running shell and any process spawned from that shell. They go out of scope when the script ends.
EXAMPLE
VARIABLE_NAME=value
export VARIABLE_NAME
PATH=/bin:/usr/bin:.
export PATH
|
Extracting values from variables | To extract the value from variables, a dollar sign is used.
EXAMPLE
echo $variable_name
echo $name
echo $PATH
|
Reading user input | The read command takes a line of input from the user and assigns it to a variable(s) on the right-hand side. The read command can accept muliple variable names. Each variable will be assigned a word.
EXAMPLE
echo "What is your name?"
read name
read name1 name2 ...
|
Arguments (positional parameters) | Arguments can be passed to a script from the command line. Positional parameters are used to receive their values from within the script.
EXAMPLE
At the command line:
$ scriptname arg1 arg2 arg3 ...
|
In a script: |
echo $1 $2 $3 | Positional parameters |
echo $* | All the positional paramters |
echo $# | The number of positional parameters |
Arrays (positional parameters) | The Bourne shell does support an array, but a word list can be created by using positional parameters. A list of words follows the built-in set command, and the words are accessed by position. Up to nine positions are allowed.
The built-in shift command shifts off the first word on the left-hand side of the list.
The individual words are accessed by position values starting at 1.
EXAMPLE |
set word1 word2 word3 |
echo $1 $2 $3 | Displays word1, word2, and word3 |
set apples peaches plums | |
shift | Shifts off apples |
echo $1 | Displays first element of the list |
echo $2 | Displays second element of the list |
echo $* | Displays all elements of the list |
Command substitution | To assign the output of a UNIX/Linux command to a variable, or use the output of a command in a string, backquotes are used.
EXAMPLE
variable_name=`command`
echo $variable_name
now=`date`
echo $now
echo "Today is `date`"
|
Arithmetic | The Bourne shell does not support arithmetic. UNIX/Linux commands must be used to perform calculations.
EXAMPLE
n=`expr 5 + 5`
echo $n
|
Operators | The Bourne shell uses the built-in test command operators to test numbers and strings.
EXAMPLE |
Equality: |
= | string |
!= | string |
-eq | number |
-ne | number |
Logical: |
-a | and |
-o | or |
! | not |
Relational: |
-gt | greater than |
-ge | greater than, equal to |
-lt | less than |
-le | less than, equal to |
Conditional statements | The if construct is followed by a command. If an expression is to be tested, it is enclosed in square brackets. The then keyword is placed after the closing parenthesis. An if must end with a fi.
EXAMPLE |
The if construct is: | The if/else construct is: |
if command
then
block of statements
fi
if [ expression ]
then
block of statements
fi
The if/else/else if construct is:
if command
then
block of statements
elif command
then
block of statements
elif command
then
block of statements
else
block of statements
fi
--------------------------
if [ expression ]
then
block of statements
elif [ expression ]
then
block of statements
elif [ expression ]
then
block of statements
else
block of statements
fi
|
if [ expression ]
then
block of statements
else
block of statements
fi
The case command construct is:
case variable_name in
pattern1)
statements
;;
pattern2)
statements
;;
pattern3)
;;
*) default value
;;
esac
case "$color" in
blue)
echo $color is blue
;;
green)
echo $color is green
;;
red|orange)
echo $color is red or orange
;;
*) echo "Not a color" # default
esac
|
Loops | There are three types of loops: while, until and for.
The while loop is followed by a command or an expression enclosed in square brackets, a do keyword, a block of statements, and terminated with the done keyword. As long as the expression is true, the body of statements between do and done will be executed.
The until loop is just like the while loop, except the body of the loop will be executed as long as the expression is false.
The for loop used to iterate through a list of words, processing a word and then shifting it off, to process the next word. When all words have been shifted from the list, it ends. The for loop is followed by a variable name, the in keyword, and a list of words then a block of statements, and terminates with the done keyword.
The loop control commands are break and continue.
EXAMPLE
while command
do
block of statements
done
while [ expression ]
do
block of statements
done
until command for
variable in word1 word2 word3 ...
do do
block of statements
block of statements
done done
until [ expression ]
do
block of statements
done
|
File testing | The Bourne shell uses the test command to evaluate conditional expressions and has a built-in set of options for testing attributes of files, such as whether it is a directory, a plain file (not a directory), a readable file, and so forth. See Example 2.3.
EXAMPLE |
-d | File is a directory |
-f | File exists and is not a directory |
–r | Current user can read the file |
–s | File is of nonzero size |
–w | Current user can write to the file |
–x | Current user can execute the file |
Example 2.3.
#!/bin/sh
1 if [ –f file ]
then
echo file exists
fi
2 if [ –d file ]
then
echo file is a directory
fi
3 if [ -s file ]
then
echo file is not of zero length
fi
4 if [ -r file -a -w file ]
then
echo file is readable and writable
fi
|
Functions | Functions allow you to define a section of shell code and give it a name. The Bourne shell introduced the concept of functions. The C and TC shells do not have functions.
EXAMPLE
function_name() {
block of code
}
-----------------------
lister() {
echo Your present working directory is `pwd`
echo Your files are:
ls
}
|