用于在每小时文件夹中移动日志文件的脚本

时间:2012-12-20 09:30:30

标签: linux bash shell unix scripting

这是我的剧本:

    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语句更进一步。我的案例构造有什么问题吗?请帮助。此外,欢迎对脚本整体开发提出建议。

我尝试在我的案例结构中使用这样的范围:

    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

这也没有帮助。

2 个答案:

答案 0 :(得分: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" \;

答案 1 :(得分: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

虽然有点冗长,但它的工作方式与我想要的完全一致。谢谢大家的支持。