Hello,
I often stumble over a common shell coding practise.
Example 1:
#!/bin/sh
#
# Licensed Materials - Property of IBM
# Rational ClearCase
# (C) Copyright IBM Corp. 1999, 2010. All Rights Reserved
# US Government Users Restricted Rights -
# Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp
#
# Start/Stop ClearCase daemons as well as
# mount or umount MVFS file systems.
#
...
INSMOD="/sbin/insmod -f"
MODPROBE="/sbin/modprobe"
RMMOD=/sbin/rmmod
LSMOD=/sbin/lsmod
PIDOF=/sbin/pidof
ECHO="/bin/echo -e "
NAWK=/bin/awk
GREP=/bin/grep
RM=/bin/rm
PS=/bin/ps
CP=/bin/cp
LN=/bin/ln
MKDIR=/bin/mkdir
EXPR=/usr/bin/expr
...
Example 2:
#!/bin/ksh
# OPC_WHAT_STRING="@(#)HP Operations Smart Plug-in for Databases 11.40.000 (10/22/08)"
########################################################################
#
# File: spi_migrate.sh
# Description: determines the DCE / HTTPS agent environment and starts
# migration of all available SPIs
# Language: Bourne Shell
# Package: -none-
#
# (c) Copyright 2004 Hewlett-Packard Development Company, L.P.
#
########################################################################
# set -x
ECHO=echo
CAT=cat
MYSYS=`uname -s`
if [ $MYSYS = "HP-UX" ] ; then
GREP=/usr/bin/grep
AWK=/usr/bin/awk
SED=/usr/bin/sed
DIFF=/usr/bin/diff
elif [ $MYSYS = "SunOS" ] ; then
GREP=/usr/xpg4/bin/grep
AWK=/usr/xpg4/bin/awk
SED=/usr/xpg4/bin/sed
DIFF=diff
elif [ $MYSYS = "AIX" ] ; then
GREP=/usr/bin/grep
AWK=/usr/bin/awk
SED=/usr/bin/sed
DIFF=/usr/bin/diff
elif [ $MYSYS = "OSF" ] ; then
GREP=/usr/bin/grep
AWK=/usr/bin/awk
SED=/usr/bin/sed
DIFF=/usr/bin/diff
elif [ $MYSYS = "Linux" ] ; then
GREP=grep
AWK=awk
DIFF=diff
ECHO='echo -e'
...
Example3:
#! /bin/sh
#
# Log file pattern detector plugin for Nagios
# Written by Ethan Galstad (nagios@nagios.org)
# Last Modified: 07-31-1999
#
...
# Paths to commands used in this script. These
# may have to be modified to match your system setup.
# TV: removed PATH restriction. Need to think more about what this means overall
#PATH=""
ECHO="/bin/echo"
GREP="/bin/egrep"
DIFF="/usr/bin/diff"
TAIL="/usr/bin/tail"
CAT="/bin/cat"
RM="/bin/rm"
CHMOD="/bin/chmod"
TOUCH="/bin/touch"
Do you see what is in common?
Always define all commands with its platform-dependent path and store it in UPPERCASE variables.
Sometimes I even see a for
loop that finds out where a command lives and stores the result - in an UPPERCASE variable.
Always UPPERCASE!
And later in the scripts there is of course
$ECHO ... | $GREP ... | $AWK ... | $WC ...
instead of
echo ... | grep ... | awk ... | wc ...
My Questions:
- have you met that, too?
- Is there a book or a University that teaches this?