Visit Jeremy's Blog.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 07-29-2014, 09:28 AM   #1
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..

D=$(date -d 'yesterday' +%d%m%y)

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
        echo -e "$TIMESTAMP File not downloaded successfuly.. mail sent." >> $LOG

#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

#If today's file does not exist on the remote server, do nothing.
        echo "On $TIMESTAMP no file was available for download." >> $LOG
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:

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.

Old 07-29-2014, 09:56 AM   #2
Senior Member
Registered: Dec 2003
Location: Trondheim, Norway
Distribution: Debian and Ubuntu
Posts: 1,295

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335

Consider this line:
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".

grep "MF_BAT_BB$D*.csv.pgp"
Old 07-29-2014, 10:04 AM   #3
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.


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 02:10 PM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 03:07 PM
quick grep question provkitir Linux - Software 4 11-25-2004 10:32 PM
samba smb.config question (quick question) TheDOGG Linux - Networking 1 03-02-2004 08:19 AM
Quick grep question player_2 Linux - Software 2 10-10-2003 07:38 AM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 06:45 PM.

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