免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 840 | 回复: 0
打印 上一主题 下一主题

shell script基础(四) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-01-18 11:44 |只看该作者 |倒序浏览

程序流程控制:
(1)if语句
if语句根据运算表达式的结果执行不同程序段代码。
if expression1             //若expression1条件为真
then
   commands            //则执行这些指令
elif experssion2           //否则experssion2为真
   commands            //则执行这些指令
else                     //若以上都不成立
  commands             //执行这条指令
fi

例1:if用法范例
[jun_xu@www ~]$ vi if_test
#!/bin/bash
if [ -f readme ]
then
   echo "there is a readme file in current directory!"
else
   echo "no readme file in current directory!"
fi
[jun_xu@www ~]$ chmod u+x if_test
[jun_xu@www ~]$ if_test
there is a readme file in current directory!

(2)case语句
case  string in              //测试string字符串
st1)                      //若包含str1
    commands;;            //执行这些指令
st2)                       //若包含str2
   commands;;             //执行这些指令
*)                        //以上都不包含
  Commands;;             //执行这些指令
esac

例2:case用法范例
  [jun_xu@www ~]$ vi install
#!/bin/bash
case $l in                                    //取得指令参数
  -i)
  echo "beginning of installation process...."       //若为i则开始安装,提示信息
  cp   bin/*   /usr/bin -r –f                   //复制执行文件
  cp   doc/*   /usr/share/doc  -r –f            //复制说明文件
  echo  "Congratulations!  Installation finished."  //显示安装结束信息

  ;;

  -h)                                     //若为h,则显示指令帮助信息
  echo "*Benny's instal lation utility*"
  echo "Use `$0 -i` to install."
  echo "Use `$0 -h` to show this help message."
  ;;

  *)          //若输入其它参数或不输入参数,则提示取得进一步的操作信息
  echo " $0: you must specify one of the options."

  echo "Try '$0 -h' for more information."
;;

esac

[jun_xu@www ~]$ chmod u+x install
[jun_xu@www ~]$ ./install
./install: you must specify one of the options.
Try './install -h' for more information.


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/56188/showart_467022.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP