解析ls,不推荐 [英] Parsing ls, not recommended

查看:91
本文介绍了解析ls,不推荐的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到了关于不解析ls的建议,如本网站中所述:不解析ls .
我一直在目录中寻找DAILY文件,所以我就是这样做的:

I received a advice for do not parse ls, like describes in this website: Don't parse ls.
I was looking for DAILY files in my directory so that's what I did then:

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的文件.

Ok, that's works well, I've two files A.DAILY and B.DAILY, with the both archives I can get what is inside it, but when I changed a little bit the loop, it doesn't iterated with all files with .DAILY extension in my directory.

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  

当我在循环内调用一个函数时,循环仅对第一个文件有效,而对第二个文件无效.

when I called a function inside the loop, the loop just does for the first file, but not to the second.

推荐答案

由于外部循环又将f设置到每个文件,因此内部循环似乎没有任何作用.

Since the outer loop sets f to each file in turn, your inner loop doesn't seem to serve any purpose.

for f in *.DA*; do
    [[ -e $f ]] || continue     
    echo "The file that you are working on: $f"    
    archiveContent=$( sed -n -e 1p "$f" )
    echo "$archiveContent"  
    COMPRESS "$archiveContent"
done  

这篇关于解析ls,不推荐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆