开发者

help with shell script for finding and moving files based on condition

Looking for some help with my bash script. I am trying to write this shell script to do the following:

  1. find files in a dir named: server1-date.done server2-date.done server3-date.done ... server10-date.done

  2. print to a listA

  3. find files in a dir (*.gz) and print to a listB
  4. if listA has a count 开发者_JS百科of 10 (basically found 10 .done files), then proceed with the moving the files in listB to its new directory
  5. after moving the files from listB, then remove the old directory which is similarly named (server1-date, server2-date, ...) and the .done files.

So far, I have this in the works. I can't get the condition for the if section working. I don't think I coded that correctly. Any code suggestions, improvements, etc would be appreciated. Thanks.

#Directories
GZDIR=/mydumps/mytest
FINALDIR=/mydumps/mytest/final
FLGDIR=/backup/mytest/flags
export GZDIR FINALDIR FLGDIR

#lists
FLGLIST="/mydumps/mytest/lists/userflgs.lst"
GZLIST="/mydumps/mytest/lists/gzfiles.lst"
export FLGLIST GZLIST

#Find files
find $FLGDIR -name \*.done -print > $FLGLIST
find $GZDIR -name \*.gz -print > $GZLIST

#Get need all (10) flags found before we do the move
FLG_VAL =`cat $FLGLIST | wc -l`
export $FLG_VAL

if [ "$FLG_VAL" = "10" ]; then
   for FILE in $GZLIST
       do
       echo "mv $GZLIST $FINALDIR" 2>&1
          for FLAG in $FLGLIST
             do
             echo "rmdir -f $FLAG" 2>&1
             done
       done
       else
           echo "Cannot move file" 2>&1
      exit 0
fi


I do not know if this will work, but it will fix all the obvious problems:

#!/bin/sh
#Directories
GZDIR=/mydumps/mytest
FINALDIR=/mydumps/mytest/final
FLGDIR=/backup/mytest/flags
export GZDIR FINALDIR FLGDIR

#lists
FLGLIST="/mydumps/mytest/lists/userflgs.lst"
GZLIST="/mydumps/mytest/lists/gzfiles.lst"

#Find files
find "$FLGDIR" -name '*.done' -print > "$FLGLIST"
find "$GZDIR" -name '*.gz' -print > "$GZLIST"

#Get need all (10) flags found before we do the move
FLG_VAL=$(wc -l <"$FLGLIST")  # Always prefer $( ... ) to backticks.

if [ "$FLG_VAL" -ge 10 ]; then
    for FILE in $(cat "$GZLIST")
        do
        echo "mv $FILE $FINALDIR" 2>&1
    done
    for FLAG in $(cat "$FLGLIST")
        do
        echo "rmdir -f $FLAG" 2>&1
    done
else
    echo "Cannot move file" 2>&1
    exit 0
fi


First of all, I really recommend that you as the default approach always test for exceptions, and do not include the "normal" case inside a test unless that is necessary.

...
FLG_VAL=`wc -l < $FLGLIST`              # no need for cat, and no space before '='
export $FLG_VAL

if [ "$FLG_VAL" != "10" ]; then
      echo "Cannot move file" 2>&1
      exit 0
fi

for FILE in $GZLIST
do
    echo "mv $GZLIST $FINALDIR" 2>&1
    for FLAG in $FLGLIST
    do
         echo "rmdir -f $FLAG" 2>&1
    done
done

See how much easier the code is to read now that the error check is extracted and stands by itself?


FLG_VAL =`cat $FLGLIST | wc -l`

Should be:

FLG_VAL=`cat $FLGLIST | wc -l`
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜