1. 条件测试命令: test or [
  逻辑判断跟普通的相反,当条件为真时,返回0,否则返回1.
number=2
test $number -gt 1
echo $?
0

[ $number -gt 3 ]
echo $?
1

note: [ ] 是个命令,括号中间是参数,命令和参数之间要有空格。
常见的测试命令:
[ -d dir ]
[-f file ]
[-z string ]   String 长度为0时为真
[ -n string ]  String 长度非0时为真
[arg1 op arg2 ]  op: -eq, -ne, -lt, -gt, -ge, -le

[ expr1 -a|-o expr2 ] -a=&&  -o=||
 
2. if/then

if [ -d tmp ] ; then
   echo "tmp is a directory"
fi

这里有三条命令, if [-d tmp ] 是第一条,then是的而条,fi是第三条。两条命令在一行上,必须用;隔开。如果then令起一行,则不用分号。

3. && ||
shell中的&&相当于 if..then.., ||则相当于if not ... then...,
上面的if/then语句也可写成 [ -d tmp ] && echo "tmp is a directory"

4. case
shell的case不仅能匹配整形和字符型,还能匹配字符串和wildcard, 每条分支必须以;;结束, 不需要break语句跳出。
echo "input your choice:"
read choice
case $choice in
Yes|yes|y)
   echo "you choose Yes";;
[N|n]*)
   echo "you choose No";;
esac




posted on 2011-02-22 09:06 Aaron.Chu 阅读(1274) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航:
 
<2011年2月>
303112345
6789101112
13141516171819
20212223242526
272812345
6789101112

留言簿(5)

随笔档案(57)

相册

友情链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜