使用grep判断是否需要通过sed往文件中增加数据。

如下需求: 有一个名称为c的文件,先判断是否有b数据,如果没有,则往c中加入b,否则不处理。

假设文件c的内容为:

  1. #!/bin/sh
  2. #filename: c
  3. the first is 1
  4. the second is 2
  5. the third is 3

实现的脚本如下:

  1. #!/bin/sh
  2. #filename:usersed 
  3. #description: if the datas don't exist in file:c,then add it to c
  4. addText[0]="the four is 4"
  5. addText[1]="the five is 5"
  6. count=0
  7. while [ $count -lt 2 ]
  8. do
  9.    if ! grep "${addText[$count]}" c >/dev/null 2>
  10.    then
  11.       sed '$a/'"${addText[$count]}"'' c > e
  12.       mv e c
  13.    fi
  14.    count=`expr $count + 1 `
  15. done
  16. exit 1

执行脚本后,文件c中的内容为:

  1. #!/bin/sh
  2. #filename: c
  3. the first is 1
  4. the second is 2
  5. the third is 3
  6. the four is 4
  7. the five is 5

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值