取目前2小时前的每小时产生的文件脚本

#!/bin/sh

filedirpath=/tmp
logfile=/tmp/getfile.log
stampfile=/tmp/stampfile


# set filename

if [ -f $stampfile ]
then
timestamp=$(cat $stampfile)
                if [ -z "$timestamp"    ]
                then
                     timestamp=$(date +%Y/%m/%d\ %H -d "-2hour now")
                     echo "read a null value ,get new vale =>$timestamp"
                else
                    filename=$(date +%Y%m%d%H -d "$timestamp")
                     echo "read a timestamp from stamp file value is =>$filename"
                fi
else
     touch $stampfile
     timestamp=$(date +%Y/%m/%d\ %H -d "-2hour now")
     echo $timestamp>$stampfile
     echo "make a new stamp file set vale=>$(cat $stampfile)"
     filename=$(date +%Y%m%d%H -d "$timestamp")
     echo "set filename =>$filename"
fi

lastfilename=$(date +%Y%m%d%H -d "-2hour now")
echo "lastfilename =>$lastfilename"

echo "enter loop !"
doflag=0

while test "$doflag" -eq 0
do

                if [ "$filename" -le "$lastfilename"    ]
                then
                                echo " to get    file !"
                                #log
                                echo "get file =>$filename"        #>>$logfile
                                #get file name is filename
                                            touch $filedirpath/$filename

                                if [ -f $filedirpath/$filename    ]
                                then
                                                #get next filename
                                                timestamp=$(date +%Y/%m/%d\ %H -d "1hour $timestamp")
                                                filename=$(date +%Y%m%d%H -d "$timestamp")
                                                echo    "$timestamp" > $stampfile
                                                echo "get file sucess! get next filename is =>$filename"
                                else
                                                echo "get file faile! retry "
                                fi

                else
                             echo "$filename !<= $lastfilename .exit loop! "
                             doflag=1
                fi

done
 
 
实际应用
 
#!/bin/sh    

#thepath=/home/gather/$1/billhisun
#only for    test
thepath=/tmp
stampfile=$thepath/stampfile

logfile=$thepath/getlog.txt
dest_path_temp=/y:/bill/0144/$1/BILL/



# init timestamp value    and set    filename tempDir

if [ -f $stampfile ]    
then    
timestamp=$(cat $stampfile)    
         if [ -z "$timestamp"        ]    
         then    
                    timestamp=$(date +%Y/%m/%d\ %H -d "-2hour now")    
                    echo "read a null value ,get new vale =>$timestamp"     #>>$logfile    
                                                filename=$(date +%Y%m%d%H -d "$timestamp")    
                                                tempDir=$(date +%Y%m -d "$timestamp")
                        
         else    
                 filename=$(date +%Y%m%d%H -d "$timestamp")    
                 tempDir=$(date +%Y%m -d "$timestamp")
                    echo "read a timestamp from stamp file value is =>$filename . tempdir is =>$tempDir"        #>>$logfile    
         fi    
else    
         touch $stampfile    
         timestamp=$(date +%Y/%m/%d\ %H -d "-2hour now")    
         echo $timestamp>$stampfile        
         echo "make a new stamp file set vale=>$(cat $stampfile)"        #>>$logfile    
         filename=$(date +%Y%m%d%H -d "$timestamp")    
         tempDir=$(date +%Y%m -d "$timestamp")
         echo "set filename =>$filename , tempdir =>$tempDir"         #>>$logfile    
fi    

lastfilename=$(date +%Y%m%d%H -d "-2hour now")    
echo "lastfilename =>$lastfilename"        #>>$logfile    

getnextfilename()
{

     timestamp=$(date +%Y/%m/%d\ %H -d "1hour $timestamp")
     filename=$(date +%Y%m%d%H -d "$timestamp")
     tempDir=$(date +%Y%m -d "$timestamp")
     echo        "$timestamp" > $stampfile        
}


echo "enter loop !"     #>>$logfile    
doflag=0    
i=0

while test "$doflag" -eq 0    
do    

     if [ "$filename" -le "$lastfilename"        ]    
     then    

                dest_path=$dest_path_temp
                tempFile="$1_"$filename".bill.hisun"                    

                    #log
                    echo ""
                    echo ""    
                    echo "$(date) to get file    => $tempFile"        #>>$logfile    
                    #only test
                     touch $thepath/$tempFile    

                #    sh /home/gather/shell/feegetftp $dest_path$tempDir $tempFile $1    
                        
                                                
                    if [ -f $thepath/$tempFile        ]    
                    then    
                                                    #get next filename    
                                                    getnextfilename                
                                                    echo "get file sucess! get next filetimestamp is =>$filename . tempdir is =>$tempDir"
                    else    
                                                    sleep 1
                                                    i=$(expr $i + 1)
                                                    if [ "$i" -eq 11 ]
                                                    then
                                                                    echo " retry get file $(expr $i - 1) times ,but do not sucess.give up \n\n"    
                                                                    echo ""
                                                                    echo ""
                                                                    i=0
                                                                    echo "i=$i, "
                                                                    getnextfilename
                                                                    continue

                                                    fi    
                                                    echo "get file faile! retry $i"    
                    fi    

     else    
                                echo ""
                                echo ""
                                echo "$filename !<= $lastfilename .exit loop! "    
                                doflag=1    
     fi    

done
 
 

你可能感兴趣的:(职场,休闲)