![[ 永远的UNIX::UNIX技术资料的宝库 ]](/images/title.gif)
|
| 首页 > 编程技术 > Shell > 正文 |
 |
| Linux程式设计-11.Shell Script(bash)--(6)流程控制case |
| http://www.openchess.org/noitatsko/programming/ (2001-05-25 13:04:01) |
case word in [ pattern [ | pattern ] ... ) list ;; ] ... esac
case/esac的标准用法大致如下:
case $arg in
pattern | sample) # arg in pattern or sample
;;
pattern1) # arg in pattern1
;;
*) #default
;;
esac
arg是您所引入的参数,如果arg内容符合pattern项目的话,那麽便会执行pattern以下的程式码,而该段程式码则以两个分号";;"做结尾。
可以注意到"case"及"esac"是对称的,如果记不起来的话,把"case"颠倒过来即可。
--------------------------------------------------------------------------------
例一 : paranoia
#!/bin/sh
case $1 in
start | begin)
echo "start something"
;;
stop | end)
echo "stop something"
;;
*)
echo "Ignorant"
;;
esac
执行
[foxman@foxman bash]# chmod 755 paranoia
[foxman@foxman bash]# ./paranoia
Ignorant
[foxman@foxman bash]# ./paranoia start
start something
[foxman@foxman bash]# ./paranoia begin
start something
[foxman@foxman bash]# ./paranoia stop
stop something
[foxman@foxman bash]# ./paranoia end
stop something
--------------------------------------------------------------------------------
例二 : inetpanel
许多的daemon都会附上一个管理用的Shell Script,像BIND就附上ndc,Apache就附上apachectl。这些管理程式都是用shell script来写的,以下示一个管理inetd的shell script。
#!/bin/sh
case $1 in
start | begin | commence)
/usr/sbin/inetd
;;
stop | end | destroy)
killall inetd
;;
restart | again)
killall -HUP inetd
;;
*)
echo "usage: inetpanel [start | begin | commence | stop | end | destory | restart | again]"
;;
esac
--------------------------------------------------------------------------------
例三 : 判断系统
有时候,您所写的Script可能会跨越好几种平台,如Linux、FreeBSD、Solaris等等,而各平台之间,多多少少都有不同之处,有时候需要判断目前正在那一种平台上执行。此时,我们可以利用uname来找出系统资讯。
#!/bin/sh
SYSTEM=`uname -s`
case $SYSTEM in
Linux)
echo "My system is Linux"
echo "Do Linux stuff here..."
;;
FreeBSD)
echo "My system is FreeBSD"
echo "Do FreeBSD stuff here..."
;;
*)
echo "Unknown system : $SYSTEM"
echo "I don't what to do..."
;;
esac
(http://www.fanqiang.com)
进入【UNIX论坛】
|
|
| 相关文章 |
Linux程式设计-31.工作群资讯管理(grp) (2001-05-27 22:08:00) Linux程式设计-30.使用者资讯管理(pwd) (2001-05-27 21:04:00) Linux程式设计-29.时间处理 (2001-05-27 20:10:01) Linux程式设计-28.GNU Make (2001-05-27 19:00:00) Linux程式设计-27.GNU Debugger (2001-05-27 18:08:01) Linux程式设计-26.PIPE (2001-05-27 17:04:00) Linux程式设计-25.Message Queues (2001-05-27 16:10:00) Linux程式设计-24.Semaphores (2001-05-27 15:00:00) Linux程式设计-23.共享记忆体(Shared Memory) (2001-05-27 14:08:00) Linux程式设计-20.getopt (2001-05-27 13:04:00)
|
===更多相关=== |
|
|
 |
★ 樊强制作 欢迎分享 ★ |