Bourne Shell Scripting: An Introduction

Mark Walsh and Andrew Cornes

Level One Associates Limited



 

Press the Enter key to continue....
Press the T key to toggle between the slides and the course notes....

Navigating This Presentation

Shell Scripts

[any material that should appear in print but not on the slide]

Shell Variables (1)

Shell Variables (2)

Shell Variables (3)

Shell Script Parameters

Alternative Variable Assignment

Flow Control - Status Variables

Flow Control (1) if

        if exit_status
        then
           no_error/true commands
        fi

        if exit_status
        then
           no_error/true commands
        else
           error/false commands
        fi

The test Command


        test –flag file

The testCommand flags

Flow Control (2) while and until

        while exit_status
        do
          commands
        done
        until exit_status
        do
          commands
        done

Flow Control (3) for

        for variable in wordlist
        do
          commands
        done

Flow Control (4) case

         case string in 
           expression_1)
             commands_1
             ;;
           expression_2)
             commands_2
             ;;
           *)
             default_commands
             ;;
         esac

Flow Control (4) Reference


EXIT Status
String
Choice


    if exit_status
    then
      true_commands
    else
      false_commands
    fi
case string in
  exp_1)
    commands_1
    ;;
  exp­_2)
    commands_2
    ;;
  *)
    default_cmds
    ;;
esac
Loops
while exit_status
do
  commands
done
until exit_status
do
  commands
done
for var in list
do
  commands
done

Trapping signals


       trap "commands" signal_list

Execute Permission

Shell Variables (1) - In Depth

Shell Variables (3) - In Depth

Shell Variables (4) - In Depth

Shell Variables (5) - In Depth

Shell Variables (6) - In Depth

Shell Variables (7) - In Depth

Shell Variables (8) - In Depth

Shell Variables (8) - In Depth

Shell Variables (9) - In Depth

Simple shell Scripts and Shell Variables

Shell Script Parameters

Shell Scripts Parameters

Shell Scripts Parameters

Alternative Variable Assignment

Alternative Variable Assignment

Alternative Variable Assignment

Flow control - Status Variables

Flow control - Status Variables

Flow control - Status Variables

Flow control - Status Variables

Flow Control (1) - In Depth

Flow Control (1) - In Depth

The test Command - In Depth

The test Command - In Depth

The test Command - In Depth

Practical

Flow Control (2) - In Depth

Flow Control (2) - In Depth

Flow Control (2) - In Depth

Practical

Practical

Flow Control (3) - In Depth

Flow Control (3) - In Depth

Flow Control (3) - In Depth

Flow Control (4) - In Depth

Flow Control (4) - In Depth

Practical

Trapping Signals - In Depth

Trapping Signals - In Depth

Trapping Signals - In Depth

Solutions to the Practical Exercises

  • Simple Shell Scripts and Shell Variables
  • Write a shell script called varval which displays the values of PATH, PS1, PWD and UID…
  • 
    echo PATH=$PATH
    echo PS1=$PS1
    echo PWD=$PWD
    echo UID=$UID
    
  • Parameters and Status Variables
  • Write a shell script called chkpath which displays "0" (a no-error exit status), if the pathname included on its command line appears in the PATH variable, or a "1" (an error exit status) otherwise…
  • 
    echo $PATH | grep "$1" > /dev/null
    echo $?
    
  • Flow Control using Exit Status
  • Modify the previous chkpath script so that no output is displayed, but whether the parameter-specified pathname appears in the PATH variable can be determined by its exit status…
  • 
    echo $PATH | grep "$1" > /dev/null

    exit $?

  • Write a shell script called addpath which appends a parameter-specified pathname onto the end of the PATH variable if the path doesn"t already appear. Use chkpath to determine whether the new pathname needs appending…
  • 
    if chkpath 1
    then 
      exit 1
    else
      PATH=$PATH:$1
      exit 0
    fi
    
  • If this script is run in the normal way, it doesn"t work. This is because, when a shell script is to be executed, a new shell is created to run it in. This is done so that shell scripts can be put into the background like ordinary processes can. It also means that the addpath script changes PATH variable for the new shell, not for the original. Because of this the shell offers an alternative execution mechanism. If addpath is executed with the dot-command, ".", the original shell will interpret the script itself, and it will work as expected:
  • 
    $ . addpath
    
  • Write a shell script called doubleln which reads lines of standard input until end-of-file, and outputs those line with a blank line immediately afterwards, thus having the effect of double line spacing the text…
  • 
    while read line
    do
      echo $line
      echo
    done
    
  • Flow Control Using String Values
  • Add code to both chkpath and addpath so that if a single pathname is not specified on the command line, then they have an exit status of –1.
  • The following code placed at the beginning of either chkpath or addpath. It simply checks to see whether the first parameter is empty:
  • 
    if test –z $1
    then
       echo Usage: chkpath pathname
       exit –1
    fi
    
  • This would seem a reasonable attempt. However, even though it would prevent:
  • $ chkpath it will allow: $ chkpath /bin /usr/bin
  • A better job is made if information regarding the number of arguments on the command line is used:
  • 
    # chkpath
       case $# in
       1)
          echo $PATH | grep "$1" > /dev/null
          exit $?
          ;;
       *)
         echo Usage: chkpath pathname
         exit -1
          ;;
       esac
    
  • and:
  • 
    # addpath
      case $# i
      1)
         if chkpath $1
         then
           exit 1
         else
           PATH=$PATH:$1
           exit 0
         fi
         ;;
      *)
        echo Usage: . addpath pathname
        exit –1
         ;;
       esac
    
  • As with our previous version of addpath, it will need to be executed using "." to work as expected.
  • Write a shell script called fsize which lists the name and size, (in bytes), of all the files listed on its command line…
  • 
    for i in $*
    do
       echo –n $i
       echo –n " "
       echo `wc –c < $i`
    done
    

    Exploring User Environment Files

    Exploring User Environment Files

    Exploring User Environment Files

    Advanced Shell Features, Process Control and Jobs

    Advanced Shell Features, Process Control and Jobs

    Advanced Shell Features, Process Control and Jobs