Benjamin Scott said:
> If you are not interested in portability to older shells, here are some
>optimizations:
>
>
> math=$(( 1 + 1 )) # internal, easier, nestable
>
> if [[ a = b ]]; ... # internal
I've had problems with [[ ]] on pdksh in the past. [ ] is also
internal on modern unixen.
>
> Anyone else have some tips or tricks they would like to share?
>
I've done lots of cross platform scripting. I find it's better to set
PATH in the top of the script rather then do things like:
X=$(/usr/bin/ls)
PATH=/usr/bin:$PATH
X=$(ls)
I've also created something like an include file that I . at the top of
my scripts. It has functions I use & defines variables for thing like
mail. For instance:
date | $MAILER -s 'feedback' user@site
On linux, MAILER=mail. Solaris, MAILER=mailx.
Scripts attached.
#!/bin/ksh
. /shared/scripts/setup.ksh
echo "Variables set in setup.ksh"
echo "--------------------------------------"
echo "PATH:"
echo " $PATH"
echo ""
echo "- Current running program & logfile ----"
echo "PROG = $PROG"
echo "LOGFILE = $LOGFILE"
echo ""
echo "- time -------------------------------"
echo "set_HOUR = $set_HOUR"
echo "set_MINUTE = $set_MINUTE"
echo "set_YEAR = $set_YEAR"
echo "set_MONTH = $set_MONTH"
echo "set_DAY = $set_DAY"
echo ""
echo "- ID the system ----------------------"
echo "set_HOST = $set_HOST"
echo "set_SYSREV = $set_SYSREV"
echo "set_SYSTEM = $set_SYSTEM"
echo "set_ipaddr = $set_ipaddr"
echo "set_mask = $set_mask"
echo "set_ipmask = $set_ipmask"
echo "set_broadaddr = $set_broadaddr"
echo ""
echo "- ID the user ------------------------"
echo "set_USER = $set_USER"
echo "set_TRUEUSER = $set_TRUEUSER"
echo "EDITOR = $EDITOR"
echo ""
echo "- commands that change ---------------"
echo "set_pscmd = $set_pscmd"
echo "set_RSH = $set_RSH"
echo "set_MAILER = $set_MAILER"
echo ' ex: $set_MAILER -s "subject line" dest1,dest2 < $FILE'
echo "PAGER = $PAGER"
echo "set_lsargs = $set_lsargs"
echo 'set_HOSTS - list of hosts /etc/host w/o localhost & comments'
echo $set_HOSTS
echo ""
echo '- useful *functions* -------------------'
echo 'pushd, popd - push & pop directories'
echo 'set_DF - df in k, not half k'
echo 'set_DFL - df local disks'
echo ''
echo 'set_psg - ps | egrep $1 # this is a function, put arg in quotes'
echo 'set_psk - ps | egrep $1 | egrep -v egrep | awk out $2 | xargs kill'
echo 'set_psk9 - set_psk w/ kill -9'
#! /bin/ksh
# file to . to get all setups
# use:
# . /shared/scripts/setup.ksh
# run /shared/scripts/setup to see what is set
# These are the maximum PATHs
PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/etc:/etc:/usr/ucb:/usr/openwin/bin:/usr/bin/X11:/shared/scripts:/usr/local/bin:/usr/local/sbin:/usr/ccs/bin
# IFF you need to add to the PATH, do this:
# PATH=$PATH:/usr/local/bin # etc...
# because the above paths will not hang. /usr/local might IFF it's NFS mounted
# and of course /shared/scripts will be available if this is being sourced
PROG=$(basename $0)
LOGFILE="/tmp/${PROG}log.$$"
set_HOST=$(uname -n |cut -d. -f1)
set_SYSREV=$(uname -r)
set_SYSTEM=$(uname -s)
case $set_SYSTEM in
HP-UX)
# hp-UX
set_RSH=remsh
iparg=lan0
;;
SunOS)
set_RSH=rsh
iparg=-a
case $set_SYSREV in
5*) # Solaris
set_lsargs=-l
;;
4*) # SunOS
set_lsargs=-lg
;;
esac
esac
# Whats the proper ps command?
# test BSD and System V style
psax=$(ps ax 2>&1 | wc -l)
pse=$(ps -ef 2>&1 | wc -l)
if [[ $psax -gt $pse ]]
then
set_pscmd=auxww # BSDish ps
else
set_pscmd=-ef # SYSVish ps (btw - OSF picks this one)
fi
unset psax
unset pse
#
# SYS-V echo?
#
if test "`/bin/echo 'blah\c'`" = "blah\c"
then
EFLAG="-n"
ENDER=""
else
EFLAG=""
ENDER="\c"
fi
ECHO="/bin/echo ${EFLAG}"
# example use:
# ${ECHO} "some message: ${ENDER}"
# which MAILER do I need to use?
if [[ -f /usr/bin/mailx ]]
then
set_MAILER=/usr/bin/mailx
elif [[ -f /usr/ucb/mail ]]
then
set_MAILER=/usr/ucb/mail
else
set_MAILER=mail
fi
# set the pager
if [ $PAGER ]
then
: #preserve user's idea of a pager
else # use our idea
if [[ -x /usr/bin/less ]]
then
export PAGER=/usr/bin/less
elif [[ -x /usr/local/bin/less ]]
then
export PAGER=/usr/local/bin/less
else
export PAGER=more
fi
fi
if [ $EDITOR ]
then
if [[ $EDITOR != 'vi' ]]
then
:
fi
else
EDITOR=vi
fi
set_USER=$(whoami)
set_TRUEUSER=$(logname)
# if $set_TRUEUSER != $set_USER, user SU'd
# The time
set_HOUR=$(date +%H)
set_MINUTE=$(date +%M)
set_YEAR=$(date | awk '{print $NF}')
set_DAY=$(date +%d)
set_MONTH=$(date +%m)
set_HOSTS=$(cat /etc/hosts | egrep -v '^#|localhost' | awk '{print $2}' | cut -d. -f1
| tr '\r' ' ')
####################################
# all the functions
# pushd, popd, dirs functions
set_DIRSTACK=""
export set_DIRSTACK
function dirs {
print "$set_DIRSTACK"
}
function pushd {
dirname=$1
set_DIRSTACK="$dirname ${set_DIRSTACK:-$PWD}"
cd ${dirname:?"missing directory name"}
dirs
}
function popd {
set_DIRSTACK=${set_DIRSTACK#* }
cd ${set_DIRSTACK%% *}
print "$PWD"
}
# do a df in k, not half k
set_DF () {
case $set_SYSTEM in
HP-UX)
bdf $@
;;
SunOS)
case $set_SYSREV in
5*)
df -k $@
;;
4*)
df $@
;;
esac
esac
}
# do df of the local file systems
set_DFL () {
case $set_SYSTEM in
HP-UX)
bdf -t hfs
;;
SunOS)
case $set_SYSREV in
5*)
df -kl | egrep -v ^mvfs\|^/proc\|^fd
;;
4*)
df | egrep ^/dev\|File\|^swap
;;
esac
esac
}
# ps | grep
set_psg () {
ps $ps_cmd | egrep $1
}
set_psk () {
ps $ps_cmd | egrep $1 | egrep -v grep | awk '{print $2}' | xargs kill
}
set_psk9 () {
ps $ps_cmd | egrep $1 | egrep -v grep | awk '{print $2}' | xargs kill -9
}
-------
Tom Buskey