移動日誌文件這是我的腳本:腳本在每小時文件夾
set -x
PTH=/data0101/track_logs
cd /data0101/track_logs
if [[ `ls -lrth | grep IMEI_TRACK | wc -l` -gt 0 ]];
then
FILE_COUNT=`ls -lrth | grep IMEI_TRACK | wc -l`
YEAR=`date | awk '{print $6}'`
mkdir $PTH/$YEAR
MONTH=`date | awk '{print $2}'`
mkdir $PTH/$YEAR/$MONTH
DAY=`date | awk '{print $3}'`
HOUR=`date | awk '{print $4}' | cut -d":" -f 1`
if [[ $HOUR -ne 0 ]];
then
HR=$(($HOUR - 1))
else
DAY=$(($DAY - 1))
HR=23
mkdir $PTH/$YEAR/$MONTH/$DAY
fi
case $HR in
00-23) for ((i=1;i<=$FILE_COUNT;i++))
do
chk=`ls -lrth | grep IMEI_TRACK | head -$i | tail -1 | awk '{print $8}' | cut -d: -f1`
FILE=`ls -lrth | grep IMEI_TRACK | awk '{print $9}'`
if [[ $chk -eq $HR ]];
then
mkdir $PTH/$YEAR/$MONTH/$DAY/$HR
mv $FILE $PTH/$YEAR/$MONTH/$DAY/$HR/
else
break
fi
done ;;
esac
fi
當我運行該腳本,這是我得到:
+ PTH=/data0101/track_logs
+ cd /data0101/track_logs
++ ls -lrth
++ grep IMEI_TRACK
++ wc -l
+ [[ 200 -gt 0 ]]
++ ls -lrth
++ grep IMEI_TRACK
++ wc -l
+ FILE_COUNT=200
++ date
++ awk '{print $6}'
+ YEAR=2012
+ mkdir /data0101/track_logs/2012
mkdir: cannot create directory `/data0101/track_logs/2012': File exists
++ date
++ awk '{print $2}'
+ MONTH=Dec
+ mkdir /data0101/track_logs/2012/Dec
mkdir: cannot create directory `/data0101/track_logs/2012/Dec': File exists
++ date
++ awk '{print $3}'
+ DAY=20
++ date
++ awk '{print $4}'
++ cut -d: -f 1
+ HOUR=14
+ [[ 14 -ne 0 ]]
+ HR=13
+ case $HR in
這清楚地表明,這個腳本沒有得到比案例陳述更深。我的病例結構有什麼問題嗎?請幫助。同時也歡迎對劇本全面發展的建議。
我試圖用我的情況下,範圍構造是這樣的:
case $HR in
[00-23]) for ((i=1;i<=$FILE_COUNT;i++))
do
chk=`ls -lrth | grep IMEI_TRACK | head -$i | tail -1 | awk '{print $8}' | cut -d: -f1`
FILE=`ls -lrth | grep IMEI_TRACK | awk '{print $9}'`
if [[ $chk -eq $HR ]];
then
mkdir $PTH/$YEAR/$MONTH/$DAY/$HR
mv $FILE $PTH/$YEAR/$MONTH/$DAY/$HR/
else
break
fi
done ;;
esac
BU這不很好的幫助。
只檢查一個HR值:這是'00-23'。這並不代表一個範圍。請參閱[在bash case語句中的正則表達式](http://stackoverflow.com/q/9631335/45249)提示問題。 – mouviciel