免费注册 查看新帖 |

Chinaunix

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

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

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

(3)for语句
for语法有两种
语法1:
for var in list
do
  commands
done
语法2:
for var
do
  commands
done

例3:for应用范例
[jun_xu@www ~]$ vi for_test
#!/bin/bash
for file in *.txt            //对目录中的每一个txt文件
do
echo "**************************************"
echo $file                 //显示文件名称
echo "**************************************"
cat $file                  //显示文件内容
done

[jun_xu@www ~]$ for_test
**************************************
myrouter.txt
**************************************
#!/bin/bash
my_router=$PWD
echo $my_router

**************************************
readme.txt
**************************************

(4)while及until语句
语法:
while expression            //当expression为真
do
  commands              //执行这些动作
done

until expression            //当expression为假
do
  commands              //执行这些动作
done

(5)break及continue语句
while expression1
do
  while expression2                        
  do
  :
   continue 2
  :
  done
done

例4:范例综合应用
[jun_xu@www ~]$ vi jobcontrol
#!/bin/bash
for x in a b c d e f g h i
do
  for y in 1 2 3 4 5 6 7 8 9
  do
     echo "current job is $x$y"
     echo "input 'n' to do next job"
     echo "      's' to skip the other jobs in current level"
     echo "      'x' to terminate all jobs"
     read action
     if [ $action = n ]
     then
        echo "do next job"
        continue
     elif [ $action = s ]
     then
       echo "skip the other jobs in current level"
       continue 2
     elif [ $action = x ]
     then
       echo "terminate all jobs"
       break 2
     fi
   done
done
[jun_xu@www ~]$ chmod u+x jobcontrol
[jun_xu@www ~]$ ./jobcontrol
current job is a1
input 'n' to do next job
      's' to skip the other jobs in current level
      'x' to terminate all jobs
n
do next job
current job is a2
input 'n' to do next job
      's' to skip the other jobs in current level
      'x' to terminate all jobs
s
skip the other jobs in current level
current job is b1
input 'n' to do next job
      's' to skip the other jobs in current level
      'x' to terminate all jobs
x
terminate all jobs
You have new mail in /var/spool/mail/jun_xu


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

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP