LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Grep in bash script returns "No such file or directory", works manually (http://www.linuxquestions.org/questions/programming-9/grep-in-bash-script-returns-no-such-file-or-directory-works-manually-791583/)

gizza23 02-25-2010 04:12 PM

Grep in bash script returns "No such file or directory", works manually
 
Hello,

To start, I'm writing a script to check a file's date via FTP. Here's the portion that I'm having problems with:

Code:

function checkRemote
{
FILENAME="UPDATE_SUCCESSFUL_$(date +%m_%d_%y)"
TEMP="/home/smn/backup-reporter/file.txt"
GETDATE=""
FTPHOST="HOST"
        sudo -u nms echo "\$ date" | ftp -v $FTPHOST > $TEMP
        $GETDATE="grep $FILENAME $TEMP"
        if [ "$GETDATE" == "$FILENAME" ];then
                        echo "updated"
                        else
                        echo "not up to date"
        fi
#      rm $TEMP
}

The problem is with "$GETDATE="grep $FILENAME $TEMP"". When bash executes this command I receive the error "No such file or directory"

Quote:

smn@smn-host:~ $ backupReport
/home/smn/bin/backupReport: line 97: =grep UPDATE_SUCCESSFUL_02_25_10 /home/smn/backup-reporter/file.txt: No such file or directory
However I can ls the directory and see that it is there. I can cat the file manually. I can even run the grep command MANUALLY and it will run. Anyone have any clues as to why this may not work?

mattca 02-25-2010 04:22 PM

Quote:

Originally Posted by gizza23 (Post 3876876)
However I can ls the directory and see that it is there. I can cat the file manually. I can even run the grep command MANUALLY and it will run. Anyone have any clues as to why this may not work?

I suspect you're not executing the grep command manually exactly as it is in the script. That is, I suspect you're typing what's between the quotes in that line, rather than the whole line. You're getting the results you're getting because the problem lies outside of the quotes.

First of all, you don't use '$' when you are assigning variables, only when accessing.

ie:

Code:

variable="this is a variable"
echo $variable

Second of all, in order to execute a command within another command, you have to use backticks.

ie:

Code:

variable=`grep pattern file`
echo $variable

That's what I see off the bat.. hope that helps!

neonsignal 02-25-2010 04:23 PM

You have a dollar sign at the beginning of that line.

Also, you can't call grep from within a quoted expression like this. I think you are actually just trying to test the result of the grep?

Sergei Steshenko 02-25-2010 04:25 PM

Quote:

Originally Posted by gizza23 (Post 3876876)
Hello,

To start, I'm writing a script to check a file's date via FTP. Here's the portion that I'm having problems with:

Code:

function checkRemote
{
FILENAME="UPDATE_SUCCESSFUL_$(date +%m_%d_%y)"
TEMP="/home/smn/backup-reporter/file.txt"
GETDATE=""
FTPHOST="HOST"
        sudo -u nms echo "\$ date" | ftp -v $FTPHOST > $TEMP
        $GETDATE="grep $FILENAME $TEMP"
        if [ "$GETDATE" == "$FILENAME" ];then
                        echo "updated"
                        else
                        echo "not up to date"
        fi
#      rm $TEMP
}

The problem is with "$GETDATE="grep $FILENAME $TEMP"". When bash executes this command I receive the error "No such file or directory"



However I can ls the directory and see that it is there. I can cat the file manually. I can even run the grep command MANUALLY and it will run. Anyone have any clues as to why this may not work?

I am not a bash guru, but I think you should rather have

Code:

GETDATE=`grep $FILENAME $TEMP`
.

evo2 02-25-2010 04:26 PM

Perhpas you want something like:
Code:

grep -q $FILENAME $TEMP
if [ "$?" = "0"  ] ; then #
      echo "updated"
else
      echo "not up to date"
fi

$? is the return value of the last command. If grep finds $FILENAME in $TEMP, $? will be 0.

Evo2.

gizza23 02-25-2010 04:26 PM

Well, I guess I answered my own problem. The script didn't work properly when I used it directly on a file but this workaround seemed to do the trick. I still don't know why the prior didn't work though :(

[code]cat /home/smn/backup-reporter/file.txt | grep UPDATE_SUCCESSFUL_02_25_10 | awk '{print $9}'[code]

Interesting...

mattca 02-25-2010 04:28 PM

Quote:

Originally Posted by gizza23 (Post 3876888)
I still don't know why the prior didn't work though :(

The previous responses should answer that for you :)

gizza23 02-25-2010 04:37 PM

Responded to my own question before i pressed refresh haha!

Ah so now it works thanks to the suggestions of tick marks and the $ issue :)

Quote:

function checkRemote
{
FILENAME="UPDATE_SUCCESSFUL_$(date +%m_%d_%y)"
TEMP="/home/smn/backup-reporter/file.txt"
GETDATE=""
FTPHOST="HOST"
sudo -u nms echo "\$ date" | ftp -v $FTPHOST > $TEMP
GETDATE=`grep $FILENAME $TEMP | /usr/bin/awk '{print $9}'`
echo $GETDATE
if [ "$GETDATE" == "$FILENAME" ];then
echo "updated"
else
echo "not up to date"
fi
# rm $TEMP
}

This returns as desired...

Quote:

smn@smn-host:~ $ !!
backupReport
UPDATE_SUCCESSFUL_02_25_10
updated
smn@smn-host:~ $

Thank you all for your speedy replies!


All times are GMT -5. The time now is 11:30 AM.