Listing 1: Function examples

iteration_level=100
x=0

# yesorno:  This function takes one argument and if the
# value is Y or y returns 1 else it returns 0.  Pressing
# <CR> returns 0;
# usage: $(yesorno $answer)
function yesorno {
   case $1 in
      y|Y) echo 1;;
      *)
      echo 0;;
   esac
} # end yesorno

printf "Answer the question: Y/N: "
read answer
if [ $(yesorno $answer) -eq 0 ]
then
    echo "ansered negative"
else
    echo "ansered positive"
fi

# user_exist:  This function takes an argument which is the
# user id, checks the first field of /etc/passwd and sets
# the exit code, 1 if user exists and 0 if not.
function user_exist
{
    if grep "^$1:" /etc/passwd > /dev/null 2>&1
    then return 1
    else return 0
    fi

}

loginame=root
user_exist $loginame
if [ "$?" -eq 0 ] 
then printf "User %s does NOT exist\n" $loginame
else printf "User %s exists\n" $loginame
fi

# downshift: this function return the argument as downshifted string
# usage: cmmd=$(down_shift $cmmd)
function down_shift {

# alternate translate command
#echo $1 | tr "[:upper:]" "[:lower:]"
echo $1 | tr '[A-Z]' '[a-z]'
} # end down_shift

string="ALLLOWERCASE"
string=$(down_shift $string)
printf "%s\n" $string

# new_down_shift: This function down shifts all positional
# parameters to lowercase and returns the string
function new_down_shift {

echo $@ | tr '[A-Z]' '[a-z]'
} # end new_down_shift

string="STRING OF LOWER CASE WORDS"
string=$(new_down_shift $string)
echo $string

# is_digit:  This function matches an all digits regular expression
# against an argument and returns 1 if the argument is digits else 0 
# if isn't.
# Note the use of nawk
function is_digit
{
echo $1|nawk ' { if ($0 ~ /^[0-9]+$/) 
                    print 1 
                  else
                    print 0 } ' 
} # end is_digit

num="345"
ret_val=$(is_digit $num)
echo $ret_val

# is_regex_set:  This function takes two arguments:
# argument 1 is the object to check
# argument 2 is a regular expression to match arg 1 against.  
# arg 2 is passed to the awk interpreter as a variable.
# if the match is true return 1 else return 0.
# Check for the argument count not equal 2.
function is_regex_set {
local pattern

if [ "$#" -ne 2 ]
then
   printf "ARGCNTNE2:"
fi

echo $1|nawk ' { if ($0 ~ pattern) 
                    print 1 
                 else
                    print 0 } ' pattern="$2"
} # end is_regex_set

# pattern is a decimal with optional sign and fraction
pattern="^[+-]?[0-9]+[.]?[0-9]*$"
num="-345.03"
ret_val=$(is_regex_set $num $pattern)
echo $ret_val

# readkey:  This function saves the current terminal settings and
# sets the terminal to raw mode with the unix stty command.  Retrieve
# 1 character with the dd command and reset original terminal settings
# with stty before returning the character to calling script.
function readkey {
local anykey oldstty

oldstty=`stty -g`
stty -icanon -echo min 1 time 0
anykey=`dd bs=1 count=1 <&0 2>/dev/null`
stty $oldstty
echo $anykey
}

printf "Get one character from the keyboard: "
anychar=$(readkey)
echo $anychar
if [ $anychar = 'Y' ]
then
   printf "Upper case Y was pressed\n"
fi

# set the global iteration limit
# iteration_level=100

#function recursion_test {
#local x

#   x=$(($1+1))
#   if [[ x -ge $iteration_level ]]
#   then
#      echo $x
#      exit 1
#   fi
#   x=$(recursion_test $x)
#   echo $x
#} # end recursion_test

#recursion_test $x
