如下需求: 有一个名称为c的文件,先判断是否有b数据,如果没有,则往c中加入b,否则不处理。
假设文件c的内容为:
- #!/bin/sh
- #filename: c
- the first is 1
- the second is 2
- the third is 3
实现的脚本如下:
- #!/bin/sh
- #filename:usersed
- #description: if the datas don't exist in file:c,then add it to c
- addText[0]="the four is 4"
- addText[1]="the five is 5"
- count=0
- while [ $count -lt 2 ]
- do
- if ! grep "${addText[$count]}" c >/dev/null 2>
- then
- sed '$a/'"${addText[$count]}"'' c > e
- mv e c
- fi
- count=`expr $count + 1 `
- done
- exit 1
执行脚本后,文件c中的内容为:
- #!/bin/sh
- #filename: c
- the first is 1
- the second is 2
- the third is 3
- the four is 4
- the five is 5