jonnybinthemix |
07-29-2014 08:28 AM |
Quick grep question
Hey Guys,
Almost done with these scripts I've been playing with, but I seem to have a problem with a slight amendment that i've made..
The below script should do the following (and it has before!):
- Open a session to an SFTP Server, take a list of it's contents and store in $FTPLIST
- Check to see whether a file containing yesterday's date is present, file should look like MF_BAT_BBDDMMYYHHMMSS.csv.pgp
- If that file(s) are present, it should download them.
- Decrypt them
As far as I can see, I think the error is with the if grep command..
Code:
D=$(date -d 'yesterday' +%d%m%y)
HOST=XX.XX.XX.XX
USER=XXXXX
PASS=XXXXXX
FTPLOG=/tmp/ftplogfile2
FILES=*$D*.csv.pgp
PORT="Port=10022"
FTPLIST=/tmp/ftplist
TIMESTAMP=$(date)
LOG=/var/log/ftpsend.log
echo -e "FTPGET.SH Script Begin -- $TIMESTAMP\n\n" >> $LOG
cd /sitsimp
#Check if todays file exists on the remote server.
/usr/bin/expect <<! > $FTPLIST
spawn sftp -o$PORT $USER@$HOST
expect "password:"
send "$PASS\r"
expect "sftp>"
send "cd output\r"
expect "sftp>"
send "ls MF_BAT_BB*\r"
send "bye\r"
expect eof
!
if grep MF_BAT_BB$D*.csv.pgp $FTPLIST; then
#If today's file exists download all relevant *.csv.pgp
/usr/bin/expect <<! > $FTPLOG
spawn sftp -o$PORT $USER@$HOST
expect "password:"
send "$PASS\r"
expect "sftp>"
send "cd output"
expect "sftp>"
send "get $FILES\r"
send "bye\r"
expect eof
!
#Check to make sure that 100% of the file downloaded.
if fgrep "100%" $FTPLOG ; then
echo -e "$TIMESTAMP - Data downloaded successfuly." >> $LOG
else
echo -e "$TIMESTAMP File not downloaded successfuly.. mail sent." >> $LOG
fi
#Decrypt today's file.
for i in $FILES; do
gpg --batch --passphrase-file /root/.gpgpass --output /storage/${i%.pgp} --decrypt /sitsimp/$i
echo "$i downloaded and decrypted on $TIMESTAMP" >> $LOG
done
#If today's file does not exist on the remote server, do nothing.
else
echo "On $TIMESTAMP no file was available for download." >> $LOG
fi
The file I'm looking for will have yesterdays date in DDMMYY format as it's unique identifier..
I'm convinced the issue is with either $D, $FILES and/or the if grep command which looks or yesterdays file in the variable $FTPLIST.
I've tried many variants of the if grep command, including:
Code:
if grep $D'.\*.csv\.pgp' $FTPLIST; then
if grep *$D'.\*.csv\.pgp' $FTPLIST; then
if grep MF_BAT_BB$D'.\*.csv\.pgp' $FTPLIST; then
If anyone can help, it would be great.
Thanks
Jon
|