【发布时间】:2013-11-03 15:42:54
【问题描述】:
我收到了关于不要解析 ls 的建议,如本网站所述:Don't parse ls。
我在我的目录中寻找 DAILY 文件,这就是我当时所做的:
for f in *.DA*; do
[[ -e $f ]] || continue
for file in $f; do
echo "The file that you are working on: "$file
archiveContent=$( sed -n -e 1p $file )
echo $archiveContent
done
done
好的,这很好用,我有两个文件 A.DAILY 和 B.DAILY,通过这两个档案我可以得到其中的内容,但是当我稍微更改循环时,它不会迭代我的目录中所有扩展名为 .DAILY 的文件。
for f in *.DA*; do
[[ -e $f ]] || continue
for file in $f; do
echo "The file that you are working on: "$file
archiveContent=$( sed -n -e 1p $file )
echo $archiveContent
COMPRESS $archiveContent;
done
done
当我在循环中调用函数时,循环只对第一个文件执行,而不是对第二个文件执行。
【问题讨论】:
-
$f是单个文件名,因此内循环只有一次迭代,其中$file与$f具有相同的值。 -
你建议做什么?我认为循环遍历文件,因为没有命令 COMPRESS $archiveContent,它向我显示:
The file that you are working on: A.DAILY the content of the A.DAILY The file that you are working on: B.DAILY the content of the B.DAILY -
删除内循环?它没有任何用途。
-
如果您的脚本输出
The file that you are working on: B.DAILY,那么循环正在执行第二个文件。你期望什么输出,你得到什么输出? -
我得到了我想要的输出,如果我评论
COMPRESS函数,如果没有,循环只适用于第一个文件。