Previous Section  < Day Day Up >  Next Section

2.4. The Bourne Shell Syntax and Constructs

The basic Bourne shell syntax and constructs are listed in Table 2.2.

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

}



2.4.1 The Bourne Shell Script

Example 2.4.

1   #!/bin/sh

2   # The Party Program––Invitations to friends from the "guest" file

3   guestfile=/home/jody/ellie/shell/guests

4   if [ ! –f "$guestfile" ]

5   then

6        echo "`basename $guestfile` non–existent"

7        exit 1

8   fi

9   PLACE="Sarotini's"; export PLACE

10  Time=`date +%H`

    Time=`expr $Time + 1`

11  set cheese crackers shrimp drinks "hot dogs" sandwiches

12  for person in `cat $guestfile`

    do

13       if [ $person =~ root ]

         then

14             continue

15       else

16       #     mail –v –s "Party" $person <<- FINIS

17             cat <<-FINIS

               Hi ${person}! Please join me at $PLACE for a party!

               Meet me at $Time o'clock.

               I'll bring the ice cream. Would you please bring $1 and

               anything else you would like to eat? Let me know if you

               can make it. Hope to see you soon.

                   Your pal,

                   ellie@`hostname`

               FINIS

18             shift

19             if [ $# –eq 0 ]

               then

20                set cheese crackers shrimp drinks "hot dogs" sandwiches

               fi

         fi

21   done

     echo "Bye..."


EXPLANATION

  1. This line lets the kernel know that you are running a Bourne shell script.

  2. This is a comment. It is ignored by the shell, but important for anyone trying to understand what the script is doing.

  3. The variable guestfile is set to the full pathname of a file called guests.

  4. This line reads: If the file guests does not exist, then print to the screen "guests nonexistent" and exit from the script.

  5. The then is usually on a line by itself, or on the same line as the if statement if it is preceded by a semicolon.

  6. The UNIX basename command removes all but the filename in a search path. Because the command is enclosed in backquotes, command substitution will be performed and the output displayed by the echo command.

  7. If the file does not exist, the program will exit. An exit with a value of 1 indicates that there was a failure in the program.

  8. The fi keyword marks the end of the block of if statements.

  9. Variables are assigned the values for the place and time. PLACE is an environment variable, because after it is set, it is exported.

  10. The value in the Time variable is the result of command substitution; i.e., the output of the date +%H command (the current hour) will be assigned to Time.

  11. The list of foods to bring is assigned to special variables (positional parameters) with the set command.

  12. The for loop is entered. It loops through until each person listed in the guest file has been processed.

  13. If the variable person matches the name of the user root, loop control will go to the top of the for loop and process the next person on the list. The user root will not get an invitation.

  14. The continue statement causes loop control to start at line 12, rather than continuing to line 16.

  15. The block of statements under else are executed if line 13 is not true.

  16. The mail message is sent when this line is uncommented. It is a good idea to comment this line until the program has been thoroughly debugged, otherwise the e-mail will be sent to the same people every time the script is tested.

  17. The next statement, using the cat command with the here document, allows the script to be tested by sending output to the screen that would normally be sent through the mail when line 7 is uncommented.

  18. After a message has been sent, the food list is shifted so that the next person will get the next food on the list. If there are more people than foods, the food list will be reset, ensuring that each person is assigned a food.

  19. The value of $# is the number of postional parameters left. If that number is 0, the food list is empty.

  20. The food list is reset.

  21. The done keyword marks the end of the block of statements in the body of the for loop.

    Previous Section  < Day Day Up >  Next Section