Linux Training

Linux training for private, public & voluntary sector.

0793 572 8612

City LinUX Training Courses

Section 11.
Shell programming 2

"There are two different areas of functionality in shells. First is interactive use and the second is scripting. Much of the debate about shells has focused on interactive use only, For example, tcsh is an acceptable shell for interactive use but practically unusable for scripting."

David Korn - 2001.

11. Shell programming 2.

11.1. Shell scripts.

A shell script is a series of commands, lists or pipes executed in a shell non-interactively. A shell script does not need to be saved to a file. e.g.

sa101$ if [ -f /etc/shadow ];then echo "got it! Now what?";\
else echo "Search me!";fi

The above is a perfectly valid shell script. Note that no file was required. We can re-execute the script by using the shell history.

NB. the escape character (\) is used to indicate that the line continues without a line feed, you should not include it when typing this script at the terminal.

11.2. Shell history.

The shell can keep a record of your previous commands. There are various mechanisms for accessing the shell history. Bash can use both the old csh ! history syntax, emacs or vi. The editor for shell history can be set with the environment variable FC_EDIT.

If you know vi the easy way to access history is to set the shell option vi.

sa101$ set -o vi

Having set the option <esc>k puts you into vi mode with the last command issued, available as if at the bottom of a text file. We can now use the usual vi commands to edit the line or just hit return to re-execute it.

Hitting k will step us back through the history (i.e. go up the history file), j will step us back down the lines of the history file.

11.3. Exercise

sa101$ mkdir test
sa101$ cd test
sa101$ touch file1
sa101$ set -o vi
sa101$ ^[k
sa101$ k
sa101$ f1r2
sa101$ ls -l

NB: ^[ is the notation for a keyboard <esc> key.

If you are using vi mode you can search the history for a string with the same command that is used in vi and in less. i.e. /<string>.

sa101$ history
  468  vi train_news
  469  sudo cp /var/tmp/t train_news
  470  vi train_news
  471  sudo bash
  472  pwd
  474  cd ~
  475  pwd
  476  cd clients
  477  cd fulford/clients
  478  ls
  479  cd po*
  480  cd sa101
  481  make xhtml
  482  sudo make xhtml
  483  pwd
  484  ls
  485  d
  486  cd /usr/local/web
  487  cd cl
  488  set -o vi
  489  vi train_news
  490  vi sa101_news
  491  sudo bash
  492  history
  493  set -o vi
  494  make xhtml
  495  history
sa101$ set -o vi
sa101$ ^[K/web
sa101$ cd /usr/local/web

After issuing the /<string> command it is possible to step back through each instance of the string <string> by hitting the next key (n).

11.4. History substitution.

A list of recent commands issued can be obtained with the history command. This will list the number of previous commands set by the environment variable HISTSIZE. The default value for the bash shell is 500. (This can be changed by users setting a different value in their home directory .profile.)

The commands are numbered and can be re-executed by using the ! character, also known as bang or shriek.


sa101$ history 5
  492  man history
  493  history|wc
  494  history|less
  495  history -5
  496  history 5
sa101$ !493
    497    1626  492910

This is known as history substitution and can be incorporated into shell scripts.

If you are going to use history frequently, it is useful to use the alias command to reduce the keystrokes required to obtain the history list.

sa101$ alias h=history
sa101$ h 5
  502  ls -l 1*
  503  date
  504  grep fulford /etc/passwd
  505  df
  506  h 5

11.5. Exercises.

Read the man page for history.

Read the man page for bash and find the references to HISTSIZE, HISTFILE and HISTFILESIZE.

Set the value of HISTSIZE in $HOME/.profile.

Use the table of commands and metanotation below to revise and consolidate your learning so far.

11.6. Tools and metanotation:

Image imgs/sa101-10.png

The layout and associated style sheets for this page are taken from the World Wide Web Consortium and used here under the W3C software licence.