筆試題:遞歸打印文件夾內所有文件中包含xxx的行

遇到一個筆試題,直接放棄了,看來我對shell是真不熟練呀……回來後,通過查詢,實現了一個版本。
#!/bin/bash

function dealxml()
{
    for file in $1/*
        do  
            if [ -d "$file" ];
            then
                dealxml $file
            else
                echoxml $file
            fi  
        done
}

function echoxml()
{
    while read line
    do
        if [[ "$line" =~ xml$ ]]  #if you want to find the line with the end of 'xml', the regex pattern is xml$
            then
            echo $line
        fi  
    done < $1
}

if [ -d "$1" ];
then
    dealxml $1
else
    dealxml .
fi
第一個方法是遞歸遍歷文件夾
第二個方法是處理文件中的內容,把包含“XXX”的行打印出來。
 




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章