![[ 永远的UNIX::UNIX技术资料的宝库 ]](/images/title.gif)
|
| 首页 > 编程技术 > Shell > 正文 |
 |
| Linux程式设计-11.Shell Script(bash)--(9)流程控制if |
| http://www.openchess.org/noitatsko/programming/ (2001-05-25 16:10:00) |
if list then list [ elif list then list ] ... [ else list ] fi
几种可能的写法
--------------------------------------------------------------------------------
第一种
if list then
do something here
fi
当list表述返回值为True(0)时,将会执行"do something here"。
例一 : 当我们要执行一个命令或程式之前,有时候需要检查该命令是否存在,然後才执行。
if [ -x /sbin/quotaon ] ; then
echo "Turning on Quota for root filesystem"
/sbin/quotaon /
fi
例二 : 当我们将某个档案做为设定档时,可先检查是否存在,然後将该档案设定值载入。
# Filename : /etc/ppp/settings
PHONE=1-800-COLLECT
#!/bin/sh
# Filename : phonebill
if [ -f /etc/ppp/settings ] ; then
source /etc/ppp/settings
echo $PHONE
fi
执行
[foxman@foxman ppp]# ./phonebill
1-800-COLLECT
--------------------------------------------------------------------------------
第二种
if list then
do something here
else
do something else here
fi
例三 : Hostname
#!/bin/sh
if [ -f /etc/HOSTNAME ] ; then
HOSTNAME=`cat /etc/HOSTNAME`
else
HOSTNAME=localhost
fi
--------------------------------------------------------------------------------
第三种
if list then
do something here
elif list then
do another thing here
fi
例四 : 如果某个设定档允许有好几个位置的话,例如crontab,可利用if then elif fi来找寻。
#!/bin/sh
if [ -f /etc/crontab ] ; then
CRONTAB="/etc/crontab"
elif [ -f /var/spool/cron/crontabs/root ] ; then
CRONTAB="/var/spool/cron/crontabs/root"
elif [ -f /var/cron/tabs/root ] ; then
CRONTAB="/var/cron/tabs/root"
fi
export CRONTAB
--------------------------------------------------------------------------------
第四种
if list then
do something here
elif list then
do another thing here
else
do something else here
fi
例五 : 我们可利用uname来判断目前系统,并分别做各系统状况不同的事。
#!/bin/sh
SYSTEM=`uname -s`
if [ $SYSTEM = "Linux" ] ; then
echo "Linux"
elif [ $SYSTEM = "FreeBSD" ] ; then
echo "FreeBSD"
elif [ $SYSTEM = "Solaris" ] ; then
echo "Solaris"
else
echo "What?"
fi
(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)
|
===更多相关=== |
|
|
 |
★ 樊强制作 欢迎分享 ★ |