[colug-432] advanced preview

Brian bnmille at gmail.com
Tue Jul 26 08:54:16 EDT 2011


Thought I would give you a chance to see the script in advance.

#!/bin/bash
#set -x
# script to decrypt files from BWC
# should be set in Control M to run cyclically (every 10 to 15 minutes or so)

# $1 should be set (in Control M) to the area that is receiving the files
# e.g, eric, aware, ohp.  Use lower case letters.  Don't forget to create
# appropriate directories under /srv/chroot/bwcftp, /wtec, and
/crypt/decrypt/bwcftp.

# written by Brian Miller in July 2011
PATH=/usr/bin:/bin:/sbin:/usr/sbin

# set up an extension to add to some files when they need to be renamed
EXT=PrevRun

# sanity check to ensure we have been given one argument:
if [ "$#" != "1" ]
        then
        echo "No directory given"
        exit 1
fi

# if there are no files in the target directory, then there are no
files to decrypt,
# and we will exit 0.
if [ "`ls /srv/chroot/bwcftp/$1`" != "" ]
then

# since there are files in the target directory, check to see if there
are files in the
# working directory.  If not, record the size of each file in the
target directory,
# using a separate file in the working directory to record the size of
each file in the
# target directory, and then exit 0.
        if [ "`ls /wtec/$1`" = "" ]
        then
                for EACH in `ls /srv/chroot/bwcftp/$1`
                do
                SIZE=`ls -l /srv/chroot/bwcftp/$1/$EACH |grep -v
total|awk '{print $5}'`
                echo $SIZE > /wtec/$1/$EACH
                done
                exit 0

        else
#               since there are files in the working directory, rename
the previous run's files
#               by adding an  extension, then write out the current
size of the files in the
#               target directory.
                cd /wtec/$1
                for FILE in `ls |grep -v $EXT`
                do
                mv -f $FILE $FILE-$EXT
                done

                for EACH in `ls /srv/chroot/bwcftp/$1`
                do
                SIZE=`ls -l /srv/chroot/bwcftp/$1/$EACH |grep -v
total|awk '{print $5}'`
                echo $SIZE > /wtec/$1/$EACH
                done
        fi

# Compare the number of files in the working directory from the
current run to the
# number of files from the previous run.  If they are not the same, then we are
# still receiving files/data, so exit 0.
        if (( `ls /wtec/$1 |grep -v $EXT |wc -l` != `ls /wtec/$1 |grep
$EXT |wc -l` ))
        then
        exit 0

        else
#       since the number of files from the previous run is the same as
the current run, we might
#       be done receiving files.  Now we need to compare the size of
the files in the target
#       directory from the current run to the size of those files from
the previous run.  If they
#       are not the same, we may still be receiving data, and we should exit 0.
        cd /wtec/$1
        for FILE in `ls |grep -v $EXT`
        do
                if (( `cat $FILE` != `cat $FILE-$EXT` ))
                then
                exit 0
                fi
        done

#       all checks are complete
#       we need to decrypt the files in the target directory, and
place the output in the
#       destination directory.  the output should lose the .pgp/.gpg extension.
                cd /srv/chroot/bwcftp/$1
                for CRYPT in `ls`
                do
                NAME=`echo $CRYPT | sed s/.gpg//`
                NAME=`echo $NAME | sed s/.pgp//`
                gpg --output /crypt/decrypt/bwcftp/$1/$NAME --decrypt
/srv/chroot/bwcftp/$1/$CRYPT
                done

# run cleanup
#               rm -f /srv/chroot/bwcftp/$1/*
                mv /srv/chroot/bwcftp/$1/* /srv/saved
                rm -f /wtec/$1/*

#       set an appropriate exit code. We need a different exit code
for each program area, in case we
#       receive files for different areas on the same day.  Control M
will use the exit code to set
#       a condition, which will be used to trigger an SFTP job that
will copy the decrypted files
#       to the final destination.

                if [ "$1" = "eric" ]
                        then
                        exit 15
                elseif [ "$1" = "aware" ]
                        exit 25
                elseif [ "$1" = "ohp" ]
                        exit 35
                else
#               we should never get here
                        exit 1
                fi


#       this fi finishes the "if (( `ls /wtec/$1 |grep -v .2 |wc -l`
!= `ls /wtec/$1 |grep .2 |wc -l` ))" if statement
        fi

else
# there are no files in the target directory, nothing to do
 exit 0
fi


More information about the colug-432 mailing list