【发布时间】:2010-09-24 14:29:14
【问题描述】:
当我为脚本提供参数:hi[123].txt 时,它会完全按照我的意愿行事。 但是如果我指定通配符( hi*.txt ),它将重新读取一些文件。
我想知道如何修改这个脚本来解决这个愚蠢的问题:
#!/bin/sh
count="0"
total="0"
FILE="$1" #FILE specification is now $1 Specification..
for FILE in $@
do
#if the file is not readable then say so
if [ ! -r $FILE ];
then
echo "File: $FILE not readable"
exit 0
fi
# Start processing readable files
while read line
do
if [[ "$line" =~ ^Total ]];
then
tmp=$(echo $line | cut -d':' -f2)
total=$(expr $total + $tmp)
echo "$FILE (s) have a total of:$tmp "
count=$(expr $count + 1)
fi
done < $FILE
done
echo " Total is: $total"
echo " Number of files read is:$count"
【问题讨论】:
-
在第一个 if 块之后,添加:echo $FILE。是否有重复的文件正在打印?
-
删除大部分代码并将其简化为最简单的循环。就像 strager 说的那样,打印出来。从小处着手,然后发展壮大。
-
我会第二个(或第三个)strager 的调试建议。
-
这与@lampshade 昨天提出的stackoverflow.com/questions/326942/… 非常接近。我很想将其作为重复项关闭 - 尽管问题略有不同。
-
似乎@lampshade 还没有阅读上一个问题的答案 - 诸如“使用“$@”而不是 $* 或 $@”之类的点还没有被听过。另外,尾巴y'day's question 谈到了重读文件。
标签: linux bash shell scripting