2013-11-20 17 views
2

我有一個文件夾包含日誌文件,所有這些文件名都遵循相同的模式,唯一的變化是例如的日期:Sample_file_20131108.txt我有一個bash腳本需要年,月和日期作爲輸入並處理在該特定日期生成的文件。使用特定的一組文件

對於例如./myscript.sh 2011將處理在2011年./myscript.sh 201108將處理在2011年一年中的第8個月產生的我也可以指定一個特定的日期,以及文件生成的所有腳本。這是我卡住的地方,我希望能夠指定一個月的18-25的日期。我試過這個作爲輸入myscript.sh 201108{12..25},但沒有奏效。所以我想弄清楚如何將它加入腳本。

這裏是我

if [ $# -ne 1 ] 
then 
    echo $# 
    exit 1 
fi 

month=$1 

#2. Find files of the month to process 
LISTFILES=$TEMPDIR/listfiles.txt.$$ 
echo '' 
echo '#2. Finding the files to process' 
for i in `find . -name "sample_file*$month*.txt"` 
do 
echo $i >> $LISTFILES 
done 
echo 'done' 

回答

1

將構建myscript.sh 201108{12..25}將擴大至myscript.sh 20110812 20110813 20110814 ...所以你的腳本必須處理幾個輸入參數,通常用while循環和shift命令。

while [[ -n "$1" ]] ; do 
    month=$1 
    for i in `find . -name "sample_file*$month*.txt"` 
    do 
    echo $i >> $LISTFILES 
    done 
    shift 
done 
相關問題