这是一个非常简单的bash脚本中,我写道:修改while循环shell变量不能按预期
#!/bin/bash
ITEM_LIST=items.txt
LOG_FILE=log.log
TOTAL_ITEMS=$(wc -l ${ITEM_LIST} | awk '{ print $1 }')
let NOT_FOUND=0
cat ${ITEM_LIST} | while read item; do
grep "${item}" ${LOG_FILE} > /dev/null
FOUND=${?}
if [ ${FOUND} -ne 0 ]; then
let NOT_FOUND=NOT_FOUND+1
echo "Item not found [${item}] Item not found number: ${NOT_FOUND}"
fi
done
echo "Total items: ${TOTAL_ITEMS}"
echo "Total not found items: ${NOT_FOUND}"
我要检查一些项目出现在一个日志文件,看看有多少不存在,并打印某种报告(最后两个回声)。在这一刻,我从cygwin bash shell运行它。
考虑这两个示例文件:
items.txt
first item
second item
third item
fourth item
fifth item
log.log
blahblah blah blah first item blah blah blah
second blah blah item
blah third item blah
脚本的输出:
[17:46:38]:/cygdrive/c/Temp/qpa# ./script2.sh
Item not found [second item] Item not found number: 1
Item not found [fourth item] Item not found number: 2
Total items: 4
Total not found items: 0
问题:
为什么脚本打印“总未找到的项目:0”?它在循环的每个回显上打印当前总数(都是NOT_FOUND变量)。
是否有这个shell脚本一些不好的做法?地点和原因?
由于它的作品!我第一次听到关于子shell :-) – sourcerebels 2009-04-30 16:26:39