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 enhanced and generalized version of above script.

   1 #!/bin/bash
   2 # cleanup, version 2
   3 # Run as root, of course.
   4 
   5 if [ -n $1 ]
   6 # Test if command line argument present.
   7 then
   8   lines=$1
   9 else  
  10   lines=50
  11   # default, if not specified on command line.
  12 fi  
  13 
  14 
  15 cd /var/log
  16 tail -$lines messages > mesg.temp
  17 # Saves last section of message log file.
  18 mv mesg.temp messages
  19 
  20 # cat /dev/null > messages
  21 # No longer needed, as the above method is safer.
  22 
  23 cat /dev/null > wtmp
  24 echo "Logs cleaned up."
  25 
  26 exit 0
  27 # A zero return value from the script upon exit
  28 # indicates success to the shell.

Since you may not wish to wipe out the entire system log, this variant of the first 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 " magic number", a special marker that designates an executable shell script (man magic gives more info 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 enables the specific commands and directives embedded in the shell or program called.

   1 #!/bin/sh
   2 #!/bin/bash #!/bin/awk #!/usr/bin/perl #!/bin/sed
   3 #!/usr/bin/tcl

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. Using #!/bin/sh, the default Bourne Shell in most commercial variants of UNIX, makes the script portable to non-Linux machines, though you may have to sacrifice a few bash-specific features (the script will conform to the POSIX 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. Example 2, above, requires the initial #!, since the variable assignment line, lines=50, uses a shell-specific construct. Note 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` whatever"
   4   exit $WRONG_ARGS
   5 fi

2.1. Invoking the script

Having written the script, you can invoke it by sh scriptname, or alternately 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 by

Either:

chmod 755 scriptname (gives everyone execute permission)

or

chmod +x scriptname (gives everyone execute permission)

chmod u+x scriptname (gives only the script owner execute permission)

In this case, you could try calling the script by ./scriptname.

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 [return] from the command line.