34.2. Shell Wrappers

A "wrapper" is a shell script that embeds a system command or utility, that saves a set of parameters passed to that command. Wrapping a script around a complex command line simplifies invoking it. This is expecially useful with sed and awk.

A sed or awk script would normally be invoked from the command line by a sed -e 'commands' or awk 'commands'. Embedding such a script in a Bash script permits calling it more simply, and makes it "reusable". This also enables combining the functionality of sed and awk, for example piping the output of a set of sed commands to awk. As a saved executable file, you can then repeatedly invoke it in its original form or modified, without the inconvenience of retyping it on the command line.


Example 34-1. shell wrapper

   1 #!/bin/bash
   2 
   3 # This is a simple script that removes blank lines from a file.
   4 # No argument checking.
   5 #
   6 # You might wish to add something like:
   7 # if [ -z "$1" ]
   8 # then
   9 #  echo "Usage: `basename $0` target-file"
  10 #  exit 65
  11 # fi
  12 
  13 
  14 # Same as
  15 #    sed -e '/^$/d' filename
  16 # invoked from the command line.
  17 
  18 sed -e /^$/d "$1"
  19 #  The '-e' means an "editing" command follows (optional here).
  20 #  '^' is the beginning of line, '$' is the end.
  21 #  This match lines with nothing between the beginning and the end,
  22 #+ blank lines.
  23 #  The 'd' is the delete command.
  24 
  25 #  Quoting the command-line arg permits
  26 #+ whitespace and special characters in the filename.
  27 
  28 exit 0


Example 34-2. A slightly more complex shell wrapper

   1 #!/bin/bash
   2 
   3 # "subst", a script that substitutes one pattern for
   4 # another in a file,
   5 # i.e., "subst Smith Jones letter.txt".
   6 
   7 ARGS=3
   8 E_BADARGS=65   # Wrong number of arguments passed to script.
   9 
  10 if [ $# -ne "$ARGS" ]
  11 # Test number of arguments to script (always a good idea).
  12 then
  13   echo "Usage: `basename $0` old-pattern new-pattern filename"
  14   exit $E_BADARGS
  15 fi
  16 
  17 old_pattern=$1
  18 new_pattern=$2
  19 
  20 if [ -f "$3" ]
  21 then
  22     file_name=$3
  23 else
  24     echo "File \"$3\" does not exist."
  25     exit $E_BADARGS
  26 fi
  27 
  28 # Here is where the heavy work gets done.
  29 sed -e "s/$old_pattern/$new_pattern/g" $file_name
  30 # 's' is, of course, the substitute command in sed,
  31 # and /pattern/ invokes address matching.
  32 # The "g", or global flag causes substitution for *every*
  33 # occurence of $old_pattern on each line, not just the first.
  34 # Read the literature on 'sed' for a more in-depth explanation.
  35 
  36 exit 0    # Successful invocation of the script returns 0.


Example 34-3. A shell wrapper around an awk script

   1 #!/bin/bash
   2 
   3 # Adds up a specified column (of numbers) in the target file.
   4 
   5 ARGS=2
   6 E_WRONGARGS=65
   7 
   8 if [ $# -ne "$ARGS" ] # Check for proper no. of command line args.
   9 then
  10    echo "Usage: `basename $0` filename column-number"
  11    exit $E_WRONGARGS
  12 fi
  13 
  14 filename=$1
  15 column_number=$2
  16 
  17 # Passing shell variables to the awk part of the script is a bit tricky.
  18 # See the awk documentation for more details.
  19 
  20 # A multi-line awk script is invoked by   awk ' ..... '
  21 
  22 
  23 # Begin awk script.
  24 # -----------------------------
  25 awk '
  26 
  27 { total += $'"${column_number}"'
  28 }
  29 END {
  30      print total
  31 }     
  32 
  33 ' "$filename"
  34 # -----------------------------
  35 # End awk script.
  36 
  37 
  38 #   It may not be safe to pass shell variables to an embedded awk script,
  39 #   so Stephane Chazelas proposes the following alternative:
  40 #   ---------------------------------------
  41 #   awk -v column_number="$column_number" '
  42 #   { total += $column_number
  43 #   }
  44 #   END {
  45 #       print total
  46 #   }' "$filename"
  47 #   ---------------------------------------
  48 
  49 
  50 exit 0

For those scripts needing a single do-it-all tool, a Swiss army knife, there is Perl. Perl combines the capabilities of sed and awk, and throws in a large subset of C, to boot. It is modular and contains support for everything ranging from object-oriented programming up to and including the kitchen sink. Short Perl scripts lend themselves to embedding in shell scripts, and there may even be some substance to the claim that Perl can totally replace shell scripting (though the author of this document remains skeptical).


Example 34-4. Perl embedded in a Bash script

   1 #!/bin/bash
   2 
   3 # Shell commands may precede the Perl script.
   4 echo "This precedes the embedded Perl script within \"$0\"."
   5 echo "==============================================================="
   6 
   7 perl -e 'print "This is an embedded Perl script.\n";'
   8 # Like sed, Perl also uses the "-e" option.
   9 
  10 echo "==============================================================="
  11 echo "However, the script may also contain shell and system commands."
  12 
  13 exit 0

It is even possible to combine a Bash script and Perl script within the same file. Depending on how the script is invoked, either the Bash part or the Perl part will execute.


Example 34-5. Bash and Perl scripts combined

   1 #!/bin/bash
   2 # bashandperl.sh
   3 
   4 echo "Greetings from the Bash part of the script."
   5 # More Bash commands may follow here.
   6 
   7 exit 0
   8 # End of Bash part of the script.
   9 
  10 # =======================================================
  11 
  12 #!/usr/bin/perl
  13 # This part of the script must be invoked with -x option.
  14 
  15 print "Greetings from the Perl part of the script.\n";
  16 # More Perl commands may follow here.
  17 
  18 # End of Perl part of the script.

 bash$ bash bashandperl.sh
 Greetings from the Bash part of the script.
 
 
 bash$ perl -x bashandperl.sh
 Greetings from the Perl part of the script.