IF-then-elif$cat greetings
#!/bin/bash
#Greetings
#A program sample using the if-then-elif construct
#This program displays greetings according to the time of the day
# Time of day greetings
#
hour=`date +%H`
if [ $hour -le 12 ]
then
echo "Good Morning!"
elif [ $hour -le 17 ]
then
echo "Good Afternoon!"
else
echo "Good Night!"
fi
FOR LOOP
for loopindex in argument-list
do
Commands
done
[wmorales@rc02uxas01 scripts]$ cat fruit.sh
for fruit in apples oranges pears bananas
do
echo "$fruit"
done
echo "Task complete."
[wmorales@rc02uxas01 scripts]$ bash fruit.sh
apples
oranges
pears
bananas
Task complete.
[wmorales@rc02uxas01 scripts]$
For
for loop-index
do
commands
done
Example:
args implies for args in "$@"
$cat for_test
for args
do
print $args
done
$ for_test candy gum chocolate #call to program
candy
gum
chocolate
While loop

$cat count
#!/bin/bash
number=0
while [ "$number" -lt 10 ]
do
echo -n "$number"
let number="$number"+1
done
echo
Arithmetic in bash
In any of the shells, arithmetic may be done using the expr command
x=2
y=`expr $x + 2`
bash also allows you to do arithmetic using the let command
x=3
let y=$x+4
Numeric Test Operators
* Table of The test Command Numeric Test Operators
Operator Meaning
-eq Is number 1 equal to number2 ?
-ne Is number1 not equal to number2 ?
-gt Is number 1 great than number2 ?
-ge Is number1 great than or equal to number2 ?
-lt Is number 1 less than number2 ?
-le Is number1 less than or equal to number2 ?
UNTIL

$cat until
secretname=jenny
name=noname
echo "Try to guess the secret name!"
echo
until [ "$name" = "$secretname" ]
do
echo -n "Your guess: "
read name
done
echo "Very good."
$cat uon
#!/bin/bash
#
# uon: let me know if xxx is on the system
#
until who | grep "$1" > /dev/null #redirect the output
of grep
do
sleep 30 #wait half minute
done
echo "$1 is logged on."
exit 0
CASE

$cat case1
echo -n "Enter A, B, or C: "
read letter
case "$letter" in
A)echo "You entered A"
;; #;; means break out of the case
B)echo "You entered B"
;;
C)echo "You entered C"
;;
*) # * catch all, if all fail
echo "You did not enter A, B, or C"
;;
esac
$cat case2
echo -n "Enter A, B, or C: "
read letter
case "$letter" in
a|A)echo "You entered A"
;;
b|B)echo "You entered B"
;;
c|C)echo "You entered C"
;;
*)echo "You did not enter A, B, or C"
;;
esac
$cat command_menu
#!/bin/bash
# menu interface to simple commands
echo -e "\n COMMAND MENU\n"
echo " a. Current date and time"
echo " b. Users currently logged in"
echo " c. Name of the working directory"
echo -e " d. Contents of the working directory\n"
echo -e "Enter a, b, c, or d: \c"
read answer
echo
case "$answer" in
a)
date
;;
b)
who
;;
c)
pwd
;;
d)
ls
;;
*)
echo "There is no selection: $answer"
;;
esac
File Test Operators
Table of The test Command File Test Operators
Operator Example Meaning
-r -r filename Does filename exist and is it
readable?
-w -w filename Does filename exist and is it
writeable?
-s -s filename Does filename exist and have a
nonzero length?
-f -f filename Does filename exist,but is it not a
directory file?
-d -d filename Does filename exist, and is it a
directory file?
Example:
$cat svi
#!/bin/bash
# The save and invoke vi program
# This program check for the file’s existence
#
if test $# = 1 #check of the number of argument
then
if test –f $1 # check for the file existence
then
cp $1 $HOME/keep #copy and invoke vi
vi $1
else
echo " file not found. Try again"
fi
else
echo " You must specify a file name. Try again."
fi