linux shell- while

如果需要在shell 中对很多行的数据进行处理,while 太合适了

基本结构

while  command   ; do
done

简单的例子

 从demofile 中读取每一行,然后显示在屏幕上
while read i ; do   #每次处理一行, 将该行内容存储在i 中
   echo "read line: $i"
done <demofile  # 将demofile 内容交友while 处理

组合应用

while 和find

  #在最近3天的日志中查找包含错误的日志
  find ./log -atime -3 -type f | while read i; do
       #只输出exception的文件
       grep "exception" $i && echo "foundfile $i";
  done 

  #统计最近三天日志文件中订单数目
  find ./log -atime -3 -type f | while read i; do
       #只输出exception的文件
       printf "file: $i, order count: ";
       grep "submitorder" $i |wc -l ;
       echo ;
  done 

while 和文件

#输出文件中大于200个字符的行
#写法1
cat filedemo | while read i;do
   //数i这个字符串存了多少个字符
   charcount=`echo $i |wc -c `
   if [ "$charcount" > 200 ]; then
       print $i
   fi
done

#写法2
while read i;do
   //数i这个字符串存了多少个字符
   charcount=`echo $i |wc -c `
   if [ "$charcount" > 200 ]; then
       print $i
   fi
done < filedemo

小彩蛋

#猜猜是个什么
while :; do :; done




#":"在shell 里也是个命令
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值