Chapter 2. Starting Off With a Sha-Bang

In the simplest case, a script is nothing more than a list of system commands stored in a file. At the very least, this saves the effort of retyping that particular sequence of commands each time it is invoked.


Example 2-1. cleanup: A script to clean up the log files in /var/log

   1 # Cleanup
   2 # Run as root, of course.
   3 
   4 cd /var/log
   5 cat /dev/null > messages
   6 cat /dev/null > wtmp
   7 echo "Logs cleaned up."

There is nothing unusual here, just a set of commands that could just as easily be invoked one by one from the command line on the console or in an xterm. The advantages of placing the commands in a script go beyond not having to retype them time and again. The script can easily be modified, customized, or generalized for a particular application.


Example 2-2. cleanup: An improved clean-up script

   1 #!/bin/bash
   2 # Proper header for a Bash script.
   3 
   4 # Cleanup, version 2
   5 
   6 # Run as root, of course.
   7 # Insert code here to print error message and exit if not root.
   8 
   9 LOG_DIR=var/log
  10 # Variables are better than hard-coded values.
  11 cd $LOG_DIR
  12 
  13 cat /dev/null > messages
  14 cat /dev/null > wtmp
  15 
  16 
  17 echo "Logs cleaned up."
  18 
  19 exit # The right and proper method of "exiting" from a script.

Now that's beginning to look like a real script. But we can go even farther . . .


Example 2-3. cleanup: An enhanced and generalized version of above scripts.

   1 #!/bin/bash
   2 # Cleanup, version 3
   3 
   4 #  Warning:
   5 #  -------
   6 #  This script uses quite a number of features that will be explained
   7 #+ later on.
   8 #  By the time you've finished the first half of the book,
   9 #+ there should be nothing mysterious about it.
  10 
  11 
  12 
  13 LOG_DIR=/var/log
  14 ROOT_UID=0     # Only users with $UID 0 have root privileges.
  15 LINES=50       # Default number of lines saved.
  16 E_XCD=66       # Can't change directory?
  17 E_NOTROOT=67   # Non-root exit error.
  18 
  19 
  20 # Run as root, of course.
  21 if [ "$UID" -ne "$ROOT_UID" ]
  22 then
  23   echo "Must be root to run this script."
  24   exit $E_NOTROOT
  25 fi  
  26 
  27 if [ -n "$1" ]
  28 # Test if command line argument present (non-empty).
  29 then
  30   lines=$1
  31 else  
  32   lines=$LINES # Default, if not specified on command line.
  33 fi  
  34 
  35 
  36 #  Stephane Chazelas suggests the following,
  37 #+ as a better way of checking command line arguments,
  38 #+ but this is still a bit advanced for this stage of the tutorial.
  39 #
  40 #    E_WRONGARGS=65  # Non-numerical argument (bad arg format)
  41 #
  42 #    case "$1" in
  43 #    ""      ) lines=50;;
  44 #    *[!0-9]*) echo "Usage: `basename $0` file-to-cleanup"; exit $E_WRONGARGS;;
  45 #    *       ) lines=$1;;
  46 #    esac
  47 #
  48 #* Skip ahead to "Loops" chapter to decipher all this.
  49 
  50 
  51 cd $LOG_DIR
  52 
  53 if [ `pwd` != "$LOG_DIR" ]  # or   if [ "$PWD" != "$LOG_DIR" ]
  54                             # Not in /var/log?
  55 then
  56   echo "Can't change to $LOG_DIR."
  57   exit $E_XCD
  58 fi  # Doublecheck if in right directory, before messing with log file.
  59 
  60 # far more efficient is:
  61 #
  62 # cd /var/log || {
  63 #   echo "Cannot change to necessary directory." >&2
  64 #   exit $E_XCD;
  65 # }
  66 
  67 
  68 
  69 
  70 tail -$lines messages > mesg.temp # Saves last section of message log file.
  71 mv mesg.temp messages             # Becomes new log directory.
  72 
  73 
  74 # cat /dev/null > messages
  75 #* No longer needed, as the above method is safer.
  76 
  77 cat /dev/null > wtmp  #  ': > wtmp' and '> wtmp'  have the same effect.
  78 echo "Logs cleaned up."
  79 
  80 exit 0
  81 #  A zero return value from the script upon exit
  82 #+ indicates success to the shell.

Since you may not wish to wipe out the entire system log, this version of the script keeps the last section of the message log intact. You will constantly discover ways of refining previously written scripts for increased effectiveness.

The sha-bang ( #!) at the head of a script tells your system that this file is a set of commands to be fed to the command interpreter indicated. The #! is actually a two-byte [1] "magic number", a special marker that designates a file type, or in this case an executable shell script (see man magic for more details on this fascinating topic). Immediately following the sha-bang is a path name. This is the path to the program that interprets the commands in the script, whether it be a shell, a programming language, or a utility. This command interpreter then executes the commands in the script, starting at the top (line 1 of the script), ignoring comments. [2]

   1 #!/bin/sh
   2 #!/bin/bash
   3 #!/usr/bin/perl
   4 #!/usr/bin/tcl
   5 #!/bin/sed -f
   6 #!/usr/awk -f

Each of the above script header lines calls a different command interpreter, be it /bin/sh, the default shell (bash in a Linux system) or otherwise. [3] Using #!/bin/sh, the default Bourne shell in most commercial variants of UNIX, makes the script portable to non-Linux machines, though you sacrifice Bash-specific features. The script will, however, conform to the POSIX [4] sh standard.

Note that the path given at the "sha-bang" must be correct, otherwise an error message -- usually "Command not found" -- will be the only result of running the script.

#! can be omitted if the script consists only of a set of generic system commands, using no internal shell directives. The second example, above, requires the initial #!, since the variable assignment line, lines=50, uses a shell-specific construct. Note again that #!/bin/sh invokes the default shell interpreter, which defaults to /bin/bash on a Linux machine.

Important

This tutorial encourages a modular approach to constructing a script. Make note of and collect "boilerplate" code snippets that might be useful in future scripts. Eventually you can build a quite extensive library of nifty routines. As an example, the following script prolog tests whether the script has been invoked with the correct number of parameters.

   1 if [ $# -ne $Number_of_expected_args ]
   2 then
   3   echo "Usage: `basename $0` script_parameters"
   4   exit $E_WRONG_ARGS
   5 fi

2.1. Invoking the script

Having written the script, you can invoke it by sh scriptname, [5] or alternatively bash scriptname. (Not recommended is using sh <scriptname, since this effectively disables reading from stdin within the script.) Much more convenient is to make the script itself directly executable with a chmod.

Either:

chmod 555 scriptname (gives everyone read/execute permission) [6]

or

chmod +rx scriptname (gives everyone read/execute permission)

chmod u+rx scriptname (gives only the script owner read/execute permission)

Having made the script executable, you may now test it by ./scriptname. [7] If it begins with a "sha-bang" line, invoking the script calls the correct command interpreter to run it.

As a final step, after testing and debugging, you would likely want to move it to /usr/local/bin (as root, of course), to make the script available to yourself and all other users as a system-wide executable. The script could then be invoked by simply typing scriptname [ENTER] from the command line.

Notes

[1]

Some flavors of UNIX (those based on 4.2BSD) take a four-byte magic number, requiring a blank after the ! -- #! /bin/sh.

[2]

The #! line in a shell script will be the first thing the command interpreter (sh or bash) sees. Since this line begins with a #, it will be correctly interpreted as a comment when the command interpreter finally executes the script. The line has already served its purpose - calling the command interpreter.

If, in fact, the script includes an extra #! line, then bash will interpret it as a comment.
   1 #!/bin/bash
   2 
   3 echo "Part 1 of script."
   4 a=1
   5 
   6 #!/bin/bash
   7 # This does *not* launch a new script.
   8 
   9 echo "Part 2 of script."
  10 echo $a  # Value of $a stays at 1.

[3]

This allows some cute tricks.

   1 #!/bin/rm
   2 # Self-deleting script.
   3 
   4 # Nothing much seems to happen when you run this... except that the file disappears.
   5 
   6 WHATEVER=65
   7 
   8 echo "This line will never print (betcha!)."
   9 
  10 exit $WHATEVER  # Doesn't matter. The script will not exit here.

Also, try starting a README file with a #!/bin/more, and making it executable. The result is a self-listing documentation file.

[4]

Portable Operating System Interface, an attempt to standardize UNIX-like OSes. The POSIX specifications are listed on the Open Group site.

[5]

Caution: invoking a Bash script by sh scriptname turns off Bash-specific extensions, and the script may therefore fail to execute.

[6]

A script needs read, as well as execute permission for it to run, since the shell needs to be able to read it.

[7]

Why not simply invoke the script with scriptname? If the directory you are in ($PWD) is where scriptname is located, why doesn't this work? This fails because, for security reasons, the current directory, "." is not included in a user's $PATH. It is therefore necessary to explicitly invoke the script in the current directory with a ./scriptname.