2012-12-20 57 views
1

移動日誌文件這是我的腳本:腳本在每小時文件夾

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這不很好的幫助。

+0

只檢查一個HR值:這是'00-23'。這並不代表一個範圍。請參閱[在bash case語句中的正則表達式](http://stackoverflow.com/q/9631335/45249)提示問題。 – mouviciel

回答

1

您需要檢查每一位獨立的,像[0-2][0-9],你也可以使用find而不是遍歷文件,mkdir-p選擇,如果需要創建父目錄,並使用多變量賦值read

#!/bin/bash 

PTH="/data0101/track_logs" 
cd "$PTH" 

if [[ $(ls -lrth | grep IMEI_TRACK | wc -l) -gt 0 ]]; then 
    DATE=$(date +"%Y %m %d %H") 
    DIR=${DATE// /\/} 
    read YEAR MONTH DAY HOUR <<<$DATE 

    case $HOUR in 
     [0-2][0-9]) mkdir -p "$DIR" 
        find ./ -mtime -1 -name "*IMEI_TRACK*" -type f -exec mv "{}" "$DIR" \;;; 

    esac 
fi 

你並不需要檢查時,如果你只是把這個腳本在你/etc/cron.hourly文件夾,然後你的腳本會是這個樣子:

#!/bin/bash 

PTH="/data0101/track_logs" 
cd "$PTH" 

DIR=$(date +"%Y/%m/%d/%H") 
mkdir "$DIR" 

find ./ -mtime -1 -name "*IMEI_TRACK*" -type f -exec mv "{}" "$DIR" \; 
+0

您可能還需要1個班輪版本'cd「/ data0101/track_logs」&& mkdir -p $(date +「%Y /%m /%d /%H」)&& find ./ -mtime -1 -name「 * IMEI_TRACK *「-type f -exec mv」{}「」$ _「\;' – 2012-12-20 11:39:39

0

這是我最後的腳本:

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}'` 
     MONTH=`date | awk '{print $2}'` 
     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 
     fi 

     case $HR in 
       [0-9]) for ((i=1;i<=$FILE_COUNT;i++)) 
          do 
          chk_hr=`ls -lrth | grep IMEI_TRACK | head -$i | tail -1 | awk '{print $8}' | cut -d: -f1` 
          chk_day=`ls -lrth | grep IMEI_TRACK | head -$i | tail -1 | awk '{print $7}'` 
          FILE=`ls -lrth | grep IMEI_TRACK | head -$i | tail -1 | awk '{print $9}'` 
          if [[ $chk_hr -eq $HR && $chk_day -eq $DAY ]]; 
          then 
           mkdir -p $DIR_PATH/$YEAR/$MONTH/$DAY/$HR 
           mv $FILE $DIR_PATH/$YEAR/$MONTH/$DAY/$HR/ 
          fi 
          done ;; 
       [1-2][0-9]) for ((i=1;i<=$FILE_COUNT;i++)) 
           do 
           chk_hr=`ls -lrth | grep IMEI_TRACK | head -$i | tail -1 | awk '{print $8}' | cut -d: -f1` 
           chk_day=`ls -lrth | grep IMEI_TRACK | head -$i | tail -1 | awk '{print $7}'` 
           FILE=`ls -lrth | grep IMEI_TRACK | head -$i | tail -1 | awk '{print $9}'` 
           if [[ $chk_hr -eq $HR && $chk_day -eq $DAY ]]; 
           then 
            mkdir -p $DIR_PATH/$YEAR/$MONTH/$DAY/$HR 
            mv $FILE $DIR_PATH/$YEAR/$MONTH/$DAY/$HR/ 
           fi 
           done ;; 
     esac 
    fi 
enter code here 

雖然有點長,但它的工作正是我想要的方式。非常感謝您的支持。

相關問題