(M)  s i s t e m a   o p e r a c i o n a l   m a g n u x   l i n u x ~/ · documentação · suporte · sobre

 

2.2. Shell wrapper, self-executing script

A sed or awk script (see Appendix B) would normally be invoked from the command line by a sed -e 'commands' or awk -e '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 2-3. shell wrapper

#!/bin/bash

# This is a simple script
# that removes blank lines
# from a file.
# No argument checking.

# Same as
# sed -e '/^$/d $1' filename
# invoked from the command line.

sed -e /^$/d $1
# '^' is beginning of line,
# '$' is end,
# and 'd' is delete.

Example 2-4. A slightly more complex shell wrapper

#!/bin/bash

# "subst", a script that substitutes one pattern for
# another in a file,
# i.e., "subst Smith Jones letter.txt".

if [ $# -ne 3 ]
# Test number of arguments to script
# (always a good idea).
then
  echo "Usage: `basename $0` old-pattern new-pattern filename"
  exit 1
fi

old_pattern=$1
new_pattern=$2

if [ -f $3 ]
then
    file_name=$3
else
    echo "File \"$3\" does not exist."
    exit 2
fi

# Here is where the heavy work gets done.
sed -e "s/$old_pattern/$new_pattern/" $file_name
# 's' is, of course, the substitute command in sed,
# and /pattern/ invokes address matching.
# Read the literature on 'sed' for a more
# in-depth explanation.

exit 0
# Successful invocation of the script returns 0.

Example 2-5. A shell wrapper around an awk script

#!/bin/bash

# Adds up a specified column (of numbers) in the target file.

if [ $# -ne 2 ]
# Check for proper no. of command line args.
then
   echo "Usage: `basename $0` filename column-number"
   exit 1
fi

filename=$1
column_number=$2

# Passing shell variables to the awk part of the script is a bit tricky.
# See the awk documentation for more details.

# A multi-line awk script is invoked by   awk ' ..... '


# Begin awk script.
# -----------------------------
awk '

{ total += $'"${column_number}"'
}
END {
     print total
}     

' $filename
# -----------------------------
# End awk script.


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 can be effectively embedded 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 HOWTO remains skeptical).

Example 2-6. Perl embedded in a bash script

#!/bin/bash

# Some shell commands may precede the Perl script.

perl -e 'print "This is an embedded Perl script\n"'
# Like sed and awk, Perl also uses the "-e" option.

# Some shell commands may follow.

exit 0

Exercise. Write a shell script that performs a simple task.