Shell Script Notes
Using `expr`
NUM=6.6
VAL=`expr $NUM \> 5`
echo "VAL is $VAL" # returns 1
VAL=`expr $NUM \< 5`
echo "VAL is $VAL" # returns 0
Do something if files don't exist
if [ ! -f foob* ]; then
echo "no foob* files exist"
fi
Infinite loop
while :
do
echo -n "still going... "
date
sleep 1
done
Count elements in a variable
PACKAGES="one two three"
NUM_PACKAGES=`echo ${PACAKGES} | wc -w | awk '{print $1}'`
echo "There are $NUM_PACKAGES packages to install for this system"
passing parameters to subroutines
do_something() {
count=0
#num_args=$#
num_args=`echo ${*} | wc -w | awk '{print $1}'`
echo "I just recieved $num_args args"
echo "The first variable just passed to me: $1"
echo "This was just passed to me: $*"
for arg in $*; do
count=`expr $count + 1`
echo "arg $count: $arg"
done
}
VAR="1 2 3"
do_something $VAR
Variable test
if [ ${1}x = 'x' ]; then
echo "You must include a valid environment abbreviation:"
echo " $0 sapbw"
echo " $0 bi"
exit 1
fi
A simple for loop
LOOP="1 2 3 4"
I=1
for ITEM in ${LOOP}; do
echo $ITEM:$I
I=`expr $I + 1`
done