1

This is my script :

    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

When I run the script this is what I get:

    + 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

This clearly shows that this script is not getting further than the case statement. Is there anything wrong with my case construct. Kindly help. Also suggestions on overall development of the script is also welcome.

I tried using the range in my case construct like this:

    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 this doesn't help as well.

4

2 回答 2

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" \;
于 2012-12-20T11:17:36.297 回答
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

虽然有点冗长,但它的工作方式正是我想要的。感谢大家的支持。

于 2012-12-22T06:57:46.267 回答