while loop:
Syntax:
while [condition]
do
#do statements or commands untill condition is not ture
{statements|commands}
done
e.g. 1:
[fedora@novice shell_scripts]$ vim while.sh
#!/bin/bash
#
#test while statement
#
#
if [ $# -eq 0 ];then
echo "Error-Number missing form command lline argument"
echo "Syntax : $0 number"
echo "Use to print multiplication table for given number"
exit 1
fi
n=$1
i=1
while [ $i -le 10 ]
do
echo "$n * $i = `expr $i \* $n`"
i=`expr $i + 1`
done
[fedora@novice shell_scripts]$ sh while.sh 3
3 * 1 = 3
3 * 2 = 6
3 * 3 = 9
3 * 4 = 12
3 * 5 = 15
3 * 6 = 18
3 * 7 = 21
3 * 8 = 24
3 * 9 = 27
3 * 10 = 30
CASE statement:
Syntax:
# $variable-name is compared against the patterns untill a match is found or "*)" will be matched .Anyway,"*)" is not necessary for case.
case $variable-name in
pattern1) command
...
..
command;;
pattern2) command
...
..
command;;
patternN) command
...
..
command;;
*) command
...
..
command;;
esac
#Pay attention to the end of every command,you'll find something!
#look at the end of case statement,haven't you found anything?
e.g. 1:
[fedora@novice shell_scripts]$ vim case.sh
#!/bin/bash
#
#test case statement
#
#
if [ -z $1 ];then
rental="***Unknown vehicle***"
elif [ -n $1 ];then
rental=$1
fi
case $rental in
"car") echo "For $rental Rs.20 per k/m";;
"van") echo "For $rental Rs.10 per k/m";;
"jeep") echo "For $rental Rs.5 per k/m";;
"bicycle") echo "For $rental 20 paisa per k/m";;
#while added left parenthesis this script works well.
(*) echo "Sorry,I can not gat a $rental for you";;
esac
[fedora@novice shell_scripts]$ sh case.sh car
For car Rs.20 per k/m
[fedora@novice shell_scripts]$ sh case.sh 1
Sorry,I can not gat a 1 for you
[fedora@novice shell_scripts]$ sh case.sh
Sorry,I can not gat a ***Unknown vehicle*** for you
阅读(1408) | 评论(3) | 转发(0) |