Linux程式设计-11.ShellScript(bash)--(9)流程控制if
ReadyGo!技术成就梦想
网络搜索
efish
2008-2-24 2:50:08
iflistthenlist...fi
几种可能的写法
--------------------------------------------------------------------------------
第一种
iflistthen
dosomethinghere
fi
当list表述返回值为True(0)时,将会执行"dosomethinghere"。
例一:当我们要执行一个命令或程式之前,有时候需要检查该命令是否存在,然後才执行。
if;then
echo"TurningonQuotaforrootfilesystem"
/sbin/quotaon/
fi
例二:当我们将某个档案做为设定档时,可先检查是否存在,然後将该档案设定值载入。
#Filename:/etc/ppp/settings
PHONE=1-800-COLLECT
#!/bin/sh
#Filename:phonebill
if;then
source/etc/ppp/settings
echo$PHONE
fi
执行
#./phonebill
1-800-COLLECT
--------------------------------------------------------------------------------
第二种
iflistthen
dosomethinghere
else
dosomethingelsehere
fi
例三:Hostname
#!/bin/sh
if;then
HOSTNAME=`cat/etc/HOSTNAME`
else
HOSTNAME=localhost
fi
--------------------------------------------------------------------------------
第三种
iflistthen
dosomethinghere
eliflistthen
doanotherthinghere
fi
例四:如果某个设定档允许有好几个位置的话,例如crontab,可利用iftheneliffi来找寻。
#!/bin/sh
if;then
CRONTAB="/etc/crontab"
elif;then
CRONTAB="/var/spool/cron/crontabs/root"
elif;then
CRONTAB="/var/cron/tabs/root"
fi
exportCRONTAB
--------------------------------------------------------------------------------
第四种
iflistthen
dosomethinghere
eliflistthen
doanotherthinghere
else
dosomethingelsehere
fi
例五:我们可利用uname来判断目前系统,并分别做各系统状况不同的事。
#!/bin/sh
SYSTEM=`uname-s`
if;then
echo"Linux"
elif;then
echo"FreeBSD"
elif;then
echo"Solaris"
else
echo"What?"
fi
-
相关文章
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)算术表述
Linux程式设计-11.ShellScript(bash)--(17)重导Redi…
Linux程式设计-11.ShellScript(bash)--(17)重导Redirection
Linux程式设计-11.ShellScript(bash)--(18)语法
Linux程式设计-11.ShellScript(bash)--(18)语法
Shell递归程序设计-目录列表
Shell递归程序设计-目录列表
2秒记住本站域名
玩过泡泡龙吗?Readygo?Go! 再加上.Com.Cn的后缀,那就是大名小顶的ReadyGo.com.cn
