LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 07-29-2014, 08:28 AM   #1
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Rep: Reputation: Disabled
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
 
Old 07-29-2014, 08:56 AM   #2
Guttorm
Senior Member
 
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,267

Rep: Reputation: 298Reputation: 298Reputation: 298
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"
 
Old 07-29-2014, 09:04 AM   #3
jonnybinthemix
Member
 
Registered: May 2014
Location: Bristol, United Kingdom
Distribution: RHEL 5 & 6
Posts: 169

Original Poster
Rep: Reputation: Disabled
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[Quick question]Problem with execl and GREP pfpietro Programming 6 09-03-2012 01:10 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
quick grep question provkitir Linux - Software 4 11-25-2004 09:32 PM
samba smb.config question (quick question) TheDOGG Linux - Networking 1 03-02-2004 07:19 AM
Quick grep question player_2 Linux - Software 2 10-10-2003 06:38 AM


All times are GMT -5. The time now is 09:23 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration