Linux程式设计-11.ShellScript(bash)--(6)流程控制case
ReadyGo!技术成就梦想
网络搜索
efish
2008-2-24 2:50:10
casewordin...)list;;]...esac
case/esac的标准用法大致如下:
case$argin
pattern|sample)#arginpatternorsample
;;
pattern1)#arginpattern1
;;
*)#default
;;
esac
arg是您所引入的参数,如果arg内容符合pattern项目的话,那麽便会执行pattern以下的程式码,而该段程式码则以两个分号";;"做结尾。
可以注意到"case"及"esac"是对称的,如果记不起来的话,把"case"颠倒过来即可。
--------------------------------------------------------------------------------
例一:paranoia
#!/bin/sh
case$1in
start|begin)
echo"startsomething"
;;
stop|end)
echo"stopsomething"
;;
*)
echo"Ignorant"
;;
esac
执行
#chmod755paranoia
#./paranoia
Ignorant
#./paranoiastart
startsomething
#./paranoiabegin
startsomething
#./paranoiastop
stopsomething
#./paranoiaend
stopsomething
--------------------------------------------------------------------------------
例二:inetpanel
许多的daemon都会附上一个管理用的ShellScript,像BIND就附上ndc,Apache就附上apachectl。这些管理程式都是用shellscript来写的,以下示一个管理inetd的shellscript。
#!/bin/sh
case$1in
start|begin|commence)
/usr/sbin/inetd
;;
stop|end|destroy)
killallinetd
;;
restart|again)
killall-HUPinetd
;;
*)
echo"usage:inetpanel"
;;
esac
--------------------------------------------------------------------------------
例三:判断系统
有时候,您所写的Script可能会跨越好几种平台,如Linux、FreeBSD、Solaris等等,而各平台之间,多多少少都有不同之处,有时候需要判断目前正在那一种平台上执行。此时,我们可以利用uname来找出系统资讯。
#!/bin/sh
SYSTEM=`uname-s`
case$SYSTEMin
Linux)
echo"MysystemisLinux"
echo"DoLinuxstuffhere..."
;;
FreeBSD)
echo"MysystemisFreeBSD"
echo"DoFreeBSDstuffhere..."
;;
*)
echo"Unknownsystem:$SYSTEM"
echo"Idon"twhattodo..."
;;
esac
case/esac的标准用法大致如下:
case$argin
pattern|sample)#arginpatternorsample
;;
pattern1)#arginpattern1
;;
*)#default
;;
esac
arg是您所引入的参数,如果arg内容符合pattern项目的话,那麽便会执行pattern以下的程式码,而该段程式码则以两个分号";;"做结尾。
可以注意到"case"及"esac"是对称的,如果记不起来的话,把"case"颠倒过来即可。
--------------------------------------------------------------------------------
例一:paranoia
#!/bin/sh
case$1in
start|begin)
echo"startsomething"
;;
stop|end)
echo"stopsomething"
;;
*)
echo"Ignorant"
;;
esac
执行
#chmod755paranoia
#./paranoia
Ignorant
#./paranoiastart
startsomething
#./paranoiabegin
startsomething
#./paranoiastop
stopsomething
#./paranoiaend
stopsomething
--------------------------------------------------------------------------------
例二:inetpanel
许多的daemon都会附上一个管理用的ShellScript,像BIND就附上ndc,Apache就附上apachectl。这些管理程式都是用shellscript来写的,以下示一个管理inetd的shellscript。
#!/bin/sh
case$1in
start|begin|commence)
/usr/sbin/inetd
;;
stop|end|destroy)
killallinetd
;;
restart|again)
killall-HUPinetd
;;
*)
echo"usage:inetpanel"
;;
esac
--------------------------------------------------------------------------------
例三:判断系统
有时候,您所写的Script可能会跨越好几种平台,如Linux、FreeBSD、Solaris等等,而各平台之间,多多少少都有不同之处,有时候需要判断目前正在那一种平台上执行。此时,我们可以利用uname来找出系统资讯。
#!/bin/sh
SYSTEM=`uname-s`
case$SYSTEMin
Linux)
echo"MysystemisLinux"
echo"DoLinuxstuffhere..."
;;
FreeBSD)
echo"MysystemisFreeBSD"
echo"DoFreeBSDstuffhere..."
;;
*)
echo"Unknownsystem:$SYSTEM"
echo"Idon"twhattodo..."
;;
esac
-
相关文章
Linux程式设计-11.ShellScript(bash)--(7)流程控制s…
Linux程式设计-11.ShellScript(bash)--(7)流程控制select
Linux程式设计-11.ShellScript(bash)--(8)返回状态E…
Linux程式设计-11.ShellScript(bash)--(8)返回状态Exit
Linux程式设计-11.ShellScript(bash)--(9)流程控制if
Linux程式设计-11.ShellScript(bash)--(9)流程控制if
Linux程式设计-11.ShellScript(bash)--(10)控制圈wh…
Linux程式设计-11.ShellScript(bash)--(10)控制圈while/until
Linux程式设计-11.ShellScript(bash)--(11)参数与变数
Linux程式设计-11.ShellScript(bash)--(11)参数与变数
Linux程式设计-11.ShellScript(bash)--(13)Bash内建…
Linux程式设计-11.ShellScript(bash)--(13)Bash内建指令集
Linux程式设计-11.ShellScript(bash)--(12)函数func…
Linux程式设计-11.ShellScript(bash)--(12)函数function
Linux程式设计-11.ShellScript(bash)--(14)Bash内建…
Linux程式设计-11.ShellScript(bash)--(14)Bash内建参数
Linux程式设计-11.ShellScript(bash)--(15)提示符号
Linux程式设计-11.ShellScript(bash)--(15)提示符号
Linux程式设计-11.ShellScript(bash)--(16)算术表述
Linux程式设计-11.ShellScript(bash)--(16)算术表述
2秒记住本站域名
玩过泡泡龙吗?Readygo?Go! 再加上.Com.Cn的后缀,那就是大名小顶的ReadyGo.com.cn
