Linux程式设计-11.Shell Script(bash)--(6)流程控制case
2004-09-13 14:10
465 查看
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
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
相关文章推荐
- Linux程式设计-11.Shell Script(bash)--(7)流程控制select
- Linux程式设计-11.Shell Script(bash)--(5)控制圈for
- Linux进阶之bash编程(流程控制-case,select)
- Linux Bash Shell学习(十一):流程控制——case
- Linux程式设计-11.Shell Script(bash)--(1)简介
- Linux程式设计-11.Shell Script(bash)--(2)教学例
- Linux程式设计-11.Shell Script(bash)--(3)用於自动备份的Shell Script
- Linux程式设计-11.Shell Script(bash)--(4)档案系统检查
- 流程控制语句,if,for,while,case等
- Linux学习之道:linux中流程控制语句if if else case
- 工作流模式详解之基本流程控制模式的应用与设计(1、2、3、4、5)
- asp.net 设计中的页面流程控制的一点想法(讨论贴)
- J2me程序设计之界面流程控制模式设计
- sql控制流程语句case when/if/ifnull/null if
- Linux进阶之bash编程(流程控制if-else)
- Bash学习笔记(4)----流程控制与函数
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例 --转载
- linux shell 流程控制(条件if,循环【for,while】,选择【case】语句实例
- DDD 领域驱动设计-如何控制业务流程?
- YbSoftwareFactory 代码生成插件【十六】:Web 下灵活、强大的审批流程实现(含流程控制组件、流程设计器和表单设计器)