Washington State University HomeWSU AdmissionsWSU CampusesWSU HomeWSU Search Tools*
edge graphic
Home Syllabus Notes Labs Grades

Shell scripting


Shell scripting
        The basics
        Variables
        Quoting
        Pre-defined variables
        'If' statements
        Conditionals
        Case statement
        Loops
        Advanced stuff

The basics

        #! /bin/sh
        #       $OpenBSD: true.sh,v 1.2 1996/06/26 05:41:53 deraadt Exp $

        exit 0

Variables

        var="This is a variable"
        var1="This is var1"
        echo $var
        echo $var1
        echo ${var}1
        This is a variable
        This is var1
        This is a variable1

Quoting

        today=`date`
        echo 'Running on `uname`: The value of today is $today.'
        echo "Running on `uname`: The value of today is $today."
        Running on `uname`: The value of today is $today.
        Running on Linux: The value of today is Tue Feb 22 18:19:27 PST 2000.

Pre-defined variables


        echo "\$$ is '$$'"
        echo "\$0 is '$0'"
        echo "\$# is '$#'"
        echo "\$* is '$*'"
        echo "\$1 is '$1'"
        echo "\$2 is '$2'"
        echo "\$3 is '$3'"
        shift
        echo "after shift...."
        echo "  \$* is '$*'"
        echo "  \$# is '$#'"
        echo "  \$1 is '$1'"
        echo "  \$2 is '$2'"
        echo "  \$3 is '$3'"
With the command: ./vartest one "two three" four 'five six'
        $$ is '11300'
        $0 is './vartest'
        $# is '4'
        $* is 'one two three four five six'
        $1 is 'one'
        $2 is 'two three'
        $3 is 'four'
        after shift....
          $* is 'two three four five six'
          $# is '3'
          $1 is 'two three'
          $2 is 'four'
          $3 is 'five six'

'If' statements

        if <<condition>>
        then
            <<one or more statements>>
        fi
        if [ $var -eq 0 ]
        then
            echo "Don't try to divide by \$var!"
        fi
        if <<condition>>
        then
            <<one or more statements>>
        else
            <<one or more statements>>
        fi
        if [ $var -eq 0 ]
        then
            echo "Don't try to divide by \$var!"
        else
            echo "It's ok to divide by \$var."
        fi
        if <<condition>>
        then
            <<one or more statements>>
        elif <<condition>>
            <<one or more statements>>
        else
            <<one or more statements>>
        fi
        if [ $var -eq 0 ]
        then
            echo "Don't try to divide by \$var!"
        elif [ $var -eq 1 ]
            echo "There's no point in dividing by \$var."
        else
            echo "It's ok to divide by \$var."
        fi

Conditionals

Case statement

    case $var in
      <<pattern1>>)
        <<things to do for pattern 1>>
        ;;
      <<pattern2>>)
        <<things to do for pattern 1>>
        ;;
      *)  # The default condition
        <<things to do in the default case>>
        ;;
     esac
    #!/bin/sh

    . /etc/rc.d/init.d/functions

    # See how we were called.
    case "$1" in
      start)
          echo -n "Starting misc daemons: "

          daemon /usr/local/sbin/wwwoffled

          echo
          ;;
      stop)
          echo -n "Stopping misc daemons: "

          killproc wwwoffled

          echo
          ;;
      status)
          # this is way overkill, but at least we have some status output...
          echo "Status of misc daemons"

          status wwwoffled
          ;;
      restart|reload)
          # do not do anything; this is unreasonable
          $0 stop; $0 start
          ;;
      *)
          # do not advertise unreasonable commands that there is no reason
          # to use with this device
          echo "Usage: misc {start|stop|status|restart|reload}"
          exit 1
    esac

    exit 0

Loops

        for var in <<item1>> <<item2>> <<item3>>
        do
            <<statements -- some using $var>>
        done
    #!/bin/sh

    # Bring down all unneeded services that are still running (there shouldn't
    # be any, so this is just a sanity check)

    for i in /var/lock/subsys/*; do
        # Check if the script is there.
        [ ! -f $i ] && continue

        # Get the subsystem name.
        subsys=${i#/var/lock/subsys/}

        # Bring the subsystem down.
        if [ -f /etc/rc.d/init.d/$subsys.init ]; then
            /etc/rc.d/init.d/$subsys.init stop
        else
            /etc/rc.d/init.d/$subsys stop
        fi
    done
        while <<condition>>
        do
            <<statements>>
        done
        until <<condition>>
        do
            <<statements>>
        done

Advanced stuff


Part of the CptS 302 Website
Instructor: Geoff Allen , geoff@wsu.edu
Source Modified: Sun Oct 7 21:04:47 2001
HTML Generated by WML 2.0.6 (25-Oct-2000): Sun Oct 7 21:05:13 2001
Disclaimer