COMP421
Unix Environment for Programmers
Lecture 05: Shell Scripts_______________________________________

Jeff Wiegley, Ph.D.
Computer Science
jeffw@csun.edu

09/12/2005

     

“Go away or I will replace you with a very small shell script”–ThinkGeek T-Shirt

1


Motivation____________________________________________

2


Shell Scripts:_________________________________________

It would be much easier to wrap up long, complicated sequences in single “script” that can be executed and edited on demand.

    $ cat loggedin  
    #!/bin/sh  
 
    who | cut -c1-8 | sort -u | pr -l1 -8 -w78 -t  
 
    $ cat poormanssearch  
    #!/bin/sh  
 
    for file in ‘ls -1‘; do  
      if grep -q quote $file; then  
        echo "File $file contains what you seek"  
      fi  
    done

Such files are called “shell scripts”. They are interpreted and are the basis for rapid prototyping and task automation in Unix.

They can be very powerful and are used as the foundation for controlling how operating system services are stopped and started.

3


The $PATH to enlightenment:_____________________

4


Shell tricks:___________________________________________

There are few handy syntax tricks to know when dealing with the Shell.

5


Which shell?:_________________________________________

6


Variables: a cursed blessing:________________________

7


Conditional branchings :____________________________

Conditional branching is handled a number of ways:

  1. if statements
    if conditional
    then
      One or more commands...
    else
      One or more commands
    fi
  2. boolean construction
    cp src dest || echo "failed to copy"
  3. case statements, the syntax for which is covered later.

8


Conditionals :________________________________________

The only “test” that the shell knows about is the exit value of a command.7

For example if the grep command finds any matches then it exits with a return value of 0, otherwise it exits with a non-zero value.

This makes prototyping fast and efficient:

if grep -q file1 file2 filen  
then  
   echo "Something was found"  
else  
   echo "Give up!"  
fi

This makes testing the contents of a variable rather challenging. how do you do $mylist == "hello"?

9


All hail test!, the almighty tester!________________

10


All hail []! the almighty imposter?:_____________

11


Loops:_________________________________________________

There are two looping constructs that test the exit status of command

  1. until, which executes a command until its command returns 0:
    until who | grep "ˆbarb"; do  
      sleep 60  
    done

  2. while, which executes its command until its command returns non-zero. (Same syntax)

A third looping construct exists called a for loop that does not test an exit status but rather processes a list of things:

      for varname in "one" "two" "three" "four"; do  
        echo $varname  
      done

If the “in list” portion is omitted then the for loop iterates over the command line arguments.

12


Automatic variables:_________________________________

Certain variables exist automatically.

$@: consists of the entire command line passed to the shell.
$0: the command name used to invoke the shell.
$1$9: The first nine individual command line arguments.9
$? the exit value of the last command executed.

13