很多人都是先学习 C/C++ 语言, 然后再学习 shell script 的。 如果大家有一点点编程基础, 那么就很容易理解所谓的顺序、选择和循环。顺序结构其实没什么好说的, 之前早就接触过了, 在本文中, 我们来介绍一下选择结构。 虽然简单, 但还是要熟练正确地使用, 毕竟和 C/C++ 的语法还是有一些出入的。 一不小心, 就容易出错。
1. 先看个入门级别的:
- [taoge@localhost learn_shell] $ cat a.sh# ! /bin/bash echo "enter your bank password"read passwd
- if [$passwd = "3.1415"];
- then echo yes
- else echo no fi[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $. / a.sh enter your bank password 1 no[taoge@localhost learn_shell] $. / a.sh enter your bank password 3.1415 yes[taoge@localhost learn_shell] $. / a.sh enter your bank password 12 34. / a.sh: line 5 : [: too many arguments no[taoge@localhost learn_shell] $
上面的程序, 有如下几点值得注意:
(1) $passwd 两边最好加上引号, 免得用户输入 12 34 的时候, 出现 12 34 = "3.1415" 这样的错误。
(2) if 后面必须有空格, 好恶心的规定啊。
(3) [ ] 里面该加空格的地方一定要加, 否则会有错误。
(4) 如果 then 和 if 处在同一行, 那么 then 前面的那个分号绝对不能少。 当然, then 可以放到下面独立成行
(5) 在比较相等的时候, 最好用 ==, 而不是 =, 尽管后者也可以, 但是会让很多 c/c++ 程序猿感觉难以适应, 感觉不舒服。
下面, 我来改一下, 使得上面程序更好:
- [taoge@localhost learn_shell] $ cat a.sh# ! /bin/bash echo "enter your bank password"read passwd
- if ["$passwd" == "3.1415"]#pay special attention to this line,
- my friends ! then echo yes
- else echo no fi[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $. / a.sh enter your bank password 1 no[taoge@localhost learn_shell] $. / a.sh enter your bank password 3.1415 yes[taoge@localhost learn_shell] $. / a.sh enter your bank password 12 34 no[taoge@localhost learn_shell] $
和 C/C++ 一样, linux shell script 也会进行短路求值, 如下(我认为这个程序非常精妙):
- [taoge@localhost learn_shell] $ cat a.sh# ! /bin/bash# ! /bin/bash echo "enter your bank password"read passwd["$passwd" == "3.1415"] && echo yes["$passwd" == "3.1415"] || echo no[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $. / a.sh enter your bank password 3.1415 yes[taoge@localhost learn_shell] $. / a.sh enter your bank password 3 no[taoge@localhost learn_shell] $
2. 看个稍微复杂一点点的, 其实也很简单:
- [taoge@localhost learn_shell] $ cat a.sh# ! /bin/bash echo "enter your login password"read passwd
- if ["$passwd" == "yaoming"] then echo "hello yaoming, you are so tall"elif["$passwd" == liuxiang] then echo "hello liuxiang, you are so fast"elif["$passwd" == wangliqin] then echo "hello wangliqin, you play pingpong so well"
- else#no then in this line echo "you do not belong here"fi[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $. / a.sh enter your login password yaoming hello yaoming,
- you are so tall[taoge@localhost learn_shell] $. / a.sh enter your login password liuxiang hello liuxiang,
- you are so fast[taoge@localhost learn_shell] $. / a.sh enter your login password wangliqin hello wangliqin,
- you play pingpong so well[taoge@localhost learn_shell] $. / a.sh enter your login password zhangyining you do not belong here[taoge@localhost learn_shell] $
3. 再看 case:
需要注意: ;; 就类似于 C/C++ 语言中的 break, 只不过, 在此处绝不可少, 否则运行 shell 的时候, 会出问题。
- [taoge@localhost learn_shell] $ cat a.sh# ! /bin/bash echo "enter your login password"read passwd
- case "$passwd" in "yaoming") echo "hello yaoming, you are so tall";;
- "liuxiang") echo "hello liuxiang, you are so fast";;
- "wangliqin") echo "hello wangliqin, you play pingpong so well";; * ) echo "you do not belong here";;esac[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $. / a.sh enter your login password yaoming hello yaoming, you are so tall[taoge@localhost learn_shell] $. / a.sh enter your login password liuxiang hello liuxiang, you are so fast[taoge@localhost learn_shell] $. / a.sh enter your login password wangliqin hello wangliqin, you play pingpong so well[taoge@localhost learn_shell] $. / a.sh enter your login password zhangyining you do not belong here[taoge@localhost learn_shell] $
另外, 最后的 *) 位置相当重要, 必须放在最后, 因为 linux shell script 中的 case 是逐条前后逐条匹配的, 而 C/C++ 中的 default 放置的位置则没有限制, 放在最开始的位置也没有任何关系。
下面,我们来一起验证一下, *) 放在前面会导致逻辑错误, 如下:
- [taoge@localhost learn_shell] $ cat a.sh# ! /bin/bash echo "enter your login password"read passwd
- case "$passwd" in *) echo "you do not belong here";;
- "yaoming") echo "hello yaoming, you are so tall";;
- "liuxiang") echo "hello liuxiang, you are so fast";;
- "wangliqin") echo "hello wangliqin, you play pingpong so well";;esac[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $. / a.sh enter your login password yaoming you do not belong here[taoge@localhost learn_shell] $
4. test 命令和 [ ] 命令
先说明一下, test 命令完全等价于 [] 命令, 可以任选其一, 当然, 每个人有不同的爱好, 我个人就比较喜欢 [] 命令, 下面, 我们来一起看一下:
我看着 test 就感觉挺别扭的, 还是用 [] 吧。在用 [] 的时候一定要注意: [后面如果没有空格, 那将是语法错误, 但是, 如果 == 左右没有空格, 那就是致命的逻辑错误, 此时 if 会永远为真, 千千万万要注意啊。 linux shell script 就是这样, 到处都是蛇, 小心它咬你。
- [taoge@localhost learn_shell] $ cat a.sh# ! /bin/bash echo "enter your login password"read passwd
- if test "$passwd" == "linux";
- then echo yes
- else echo no fi[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $. / a.sh enter your login password linux yes[taoge@localhost learn_shell] $. / a.sh enter your login password LINUX no[taoge@localhost learn_shell] $
最后, 我们以一个稍微复杂一点的程序来结束本文:
好吧, 先说到这里。
- [taoge@localhost learn_shell] $ cat a.sh# ! /bin/bash#compare string name = ""
- if ["$name" == ""];
- then echo log1
- else echo log2 fi
- if [ - z "$name"];
- then echo log3
- else echo log4 fi name = "taoge"
- if [ - z "$name"];
- then echo log5
- else echo log6 fi
- if [ - n "$name"];
- then echo log7
- else echo log8 fi
- if ["$name" == "taoge"];
- then echo log9
- else echo log10 fi#compare number
- if ["$#" - eq 0];
- then echo log11
- else echo log12 fi
- if ["$#" - lt 1];
- then echo log13
- else echo log14 fi
- if [1 - lt 2 - a 2 - lt 3];
- then echo log15
- else echo log16 fi
- if [1 - lt 2] && [2 - lt 3];
- then echo log17
- else echo log18 fi#test directory or file pwd
- if [ - e / home / taoge / Desktop / learn_shell];
- then echo log19
- else echo log20 fi
- if [ - e / home / taoge / Desktop / learn_shell / test.html];
- then echo log21
- else echo log22 fi touch / home / taoge / Desktop / learn_shell / test.html
- if [ - e / home / taoge / Desktop / learn_shell / test.html];
- then echo log23
- else echo log24 fi[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $[taoge@localhost learn_shell] $. / a.sh log1 log3 log6 log7 log9 log11 log13 log15 log17 / home / taoge / Desktop / learn_shell log19 log22 log23[taoge@localhost learn_shell] $
来源: http://lib.csdn.net/article/linux/37198