Chinaunix首页 | 论坛 | 博客
  • 博客访问: 706066
  • 博文数量: 112
  • 博客积分: 3889
  • 博客等级: 少校
  • 技术积分: 1448
  • 用 户 组: 普通用户
  • 注册时间: 2010-10-19 16:35
个人简介

追求卓越,成功就会在不经意间追上你

文章分类

全部博文(112)

文章存档

2015年(1)

2014年(2)

2013年(1)

2012年(16)

2011年(86)

2010年(6)

分类: Python/Ruby

2011-05-07 19:42:02

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

阅读(1401) | 评论(3) | 转发(0) |
0

上一篇:for loop 备忘

下一篇:让你的echo更出彩

给主人留下些什么吧!~~

xiaozhenggang2011-05-12 16:16:34

jamesbert: while case与其他开发语言是一致的.....
万变不离其宗

jamesbert2011-05-12 13:21:50

while case与其他开发语言是一致的

jamesbert2011-05-12 13:21:46

while case与其他开发语言是一致的