Academic and Research Computing

On-line Course Materials

Introduction to Shell Programming

[In order to understand these materials, you must already know enough about Unix to be comfortable using it interactively, and you must already know how to use a Unix editor such as vi, emacs, or nedit.]

A shell script is a program written using Unix commands and shell programming constructs. Shell scripts can be used to automate repetitive command sequences and to perform actions on multiple files.

These materials cover the following topics, illustrated by simple Bourne shell examples:

  • what is shell programming?
  • review of basic Unix pipelines of commands about shell scripts
  • some new commands
  • variables
  • parameters and shift cut
  • if-then-else-fi for-in-do-done
  • sed
  • gotchas

Work through the Lecture file pdf.

It describes these example shell scripts you can download and run:

  • hi (prints Hello, world!)
  • himike (prints Hello, Mike!)
  • rem (removes junk and reports return code)
  • quiet (removes "junk" sending errors to /dev/null)
  • pars (reports $#, #@, #0, $1, $2)
  • shifter (illustrates parameter shifting)
  • sorter (alphabetizes a file in place)
  • hiyou (illustrates command substitution to variab)
  • hiyou2 (illustrates command substitution in-line)
  • compile (compiles from a default directory)
  • countem (counts the lines in a file)
  • finder (checks "text" for a given word)
  • compares (shows results of various comparisons)
  • empty (reports whether a file has contents)
  • adder (adds integers)
  • fixfor (changes . for suffixes to .f)
  • suffix (changes one suffix to another)

Other files referred to in the example scripts:

  • names (names sorted by sorter)
  • text (poem used by countem, printem, empty)
  • a.for (renamed by suffix)
  • b.for (renamed by suffix)
  • pgm.for (renamed by suffix)
  • xyz.w.for (renamed by suffix)

Hints for exercises at the end:

Help Desk Services, Academic and Research Computing