Department of Engineering

IT Services

Shell Programming

For people who have written programs before and have used Unix from the command line. There's a PDF version and a more advanced version

Image shellfig At the heart of Unix is a kernel whose routines aren't easy to use directly. There are also many pre-written programs (ls to list files, etc). They're easier to use, but they don't understand the use of '*' as a wildcard character, and besides, you need some sort of editor when you type commands in if you're going to use the command line. The `shell' is the interface between the user and the system. The shell isn't only a line editor and a command line interpreter that understands wildcards, it's also a language with variables, arrays, functions and control structures. Command lines can be put into a file and executed. These so-called ``shell scripts'' can quickly be written and tested and should be tried in association with other standard unix utilities before embarking on a higher level language, at least for prototyping purposes.

Various text-based shells are in use. sh, the Bourne Shell, is the oldest. The C-shell (csh) has many useful features lacking from sh but isn't that good for programming in. The Korn Shell (ksh) and the (very similar) POSIX shell are developments of sh that incorporates many csh features. bash is similar and is freely available (it's the default on linux and MacOS X). This document is aimed at bash users on CUED's Teaching System, though non-csh users elsewhere shouldn't have any problems.

Writing a shell script is easy - start up your editor with a file called try then type a few harmless commands into this file, one per line. For example

   hostname
   date
   ls

then save it as text. You want to make this file executable, so in the terminal window type `chmod u+x try', adding eXecute permission for the User. Run this script by typing its name (on some systems you may need to type `./try' - the '.' is shorthand for 'the current directory'). You should see the machine's name, the date and a list of files.

Even scripts as simple as this can save on repetitive typing, but much more can be easily achieved.

Wildcard characters

The * and ? characters have a special meaning to the shell when used where filenames are expected. If you have files called bashful, sneezy and grumpy in your directory and you type

  ls *y

you'll list sneezy and grumpy because * can represent any number of characters (except an initial '.'). ? represents a single character, so

  ls *u?

will only print filenames whose penultimate letter is u.

Arguments from the command line

It is easy to write a script that takes arguments (options) from the command line. Type this script into a file called args.

echo this $0 command has $# arguments.
echo They are $*

The echo command echoes the rest of the line to the screen by default. Within a shell script, $0 denotes the script's name, $1 denotes the first argument mentioned on the command line and so on. Make the script executable then try it out with various arguments. For example, if you type

    args first another

then

  • $# would be replaced by 2, the number of arguments,
  • $0 would be substituted by args,
  • $* would be substituted by first another, (`*' having a wildcard meaning)

Constructions

The shell has loop and choice constructs. These are described in the shell's manual page (type man bash on Linux or MacOS). Here are some examples. Type them into a file to see what they do, or copy-paste the programs onto the command line. Note that after a # the rest of the line isn't executed. These comment lines are worth reading.

        # Example 0 : While loop. Keeping looping while i is less than 10
        # The first line creates a variable. Note that to read a 
        # variable you need to put a '$' before its name
           i=0
           while [ $i -lt 10 ]  
           do
             echo i is $i
             let i=$i+1
           done

        # Example 1 : While loop.
        # This script keeps printing the date. 'true' is a command
        # that does nothing except return a true value.
        # Use ^C (Ctrl-C) to stop it.
           while true 
           do
             echo "date is"
             date
           done

        # example 2: For Loop.
        # Do a letter, word and line count of all the files in
        # the current directory. 
        # The `*' below is expanded to a list of files. The 
        # variable `file' successively takes the value of 
        # these filenames. Preceding a variable name by `$' 
        # gives its value.
           for file in *
           do
             echo "wc $file gives"
             wc $file 
           done

        # Example 3: If.
        # like the above, but doesn't try to run wc on directories
           for file in *
           do
             if [ ! -d $file ] #ie: if $file isn't a directory
             then
               echo "wc $file gives"
               wc $file 
             else
               echo "$file is a directory"
             fi
           done

         # Example 4 : Case - a multiple if
         # Move to the user's home directory
         cd
         for file in .?*
         do
            #Now check for some common filenames.
            case $file in
              .kshrc) echo "You have a Korn Shell set-up file";;  
              .bashrc) echo "You have a Bash Shell set-up file";;
              .Xdefaults) echo "You have an X resource file";;
              .profile) echo "You have a shell login file";;
            esac
         done

Quoting and special characters

We've already met a number of symbols that have a special meaning to the shell. Not yet mentioned are braces ({ .. }) which are use to surround variable names when it's not clear where they end. For example, if you want to want to print the value of i with ``ed'' added on the end, you could use echo ${i}ed.

Putting the symbols in single quotes disables their special meaning. Using double quotes disables some but not others. Here are some examples using quotes. Try them and see if they work as you expect.

echo '{  *  $  $xyz  #'
echo "  *  $  $xyz  ' #  /"

Single special characters can be disabled by preceding them with a '\' character, so to print a quotemark you can use echo \"