LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Quick grep question (https://www.linuxquestions.org/questions/linux-newbie-8/quick-grep-question-4175512608/)

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!):
  1. Open a session to an SFTP Server, take a list of it's contents and store in $FTPLIST
  2. Check to see whether a file containing yesterday's date is present, file should look like MF_BAT_BBDDMMYYHHMMSS.csv.pgp
  3. If that file(s) are present, it should download them.
  4. 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

Guttorm 07-29-2014 08:56 AM

Hi

Consider this line:
Code:

grep MF_BAT_BB$D*.csv.pgp
The shell will first convert the variable D to value, then, because there's a * in the parameter, the shell will "glob" the parameter - meaning it will search for files in currenct directory for files that mach. If no mach, it shouldn't make a difference. But if there is such a file, it will change it. Put double quotes around the parameter to avoid this "globbing".

Code:

grep "MF_BAT_BB$D*.csv.pgp"

jonnybinthemix 07-29-2014 09:04 AM

Ah nice one.. I'll do that. Thanks.

I think I found another problem also, I missed the "\r" when changing to the output directory on the remote server.. but only on the download part.. not on the ls part.

It seems to be working okay now, but will do some more testing.


All times are GMT -5. The time now is 04:41 PM.