LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-25-2010, 04:12 PM   #1
gizza23
Member
 
Registered: Jun 2005
Location: Chicago, IL, USA
Distribution: Fedora Core, CentOS
Posts: 188

Rep: Reputation: 31
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?

Last edited by gizza23; 02-25-2010 at 04:19 PM.
 
Old 02-25-2010, 04:22 PM   #2
mattca
Member
 
Registered: Jan 2009
Distribution: Slackware 14.1
Posts: 321

Rep: Reputation: 52
Quote:
Originally Posted by gizza23 View Post
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!

Last edited by mattca; 02-25-2010 at 04:25 PM.
 
1 members found this post helpful.
Old 02-25-2010, 04:23 PM   #3
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,363
Blog Entries: 52

Rep: Reputation: 353Reputation: 353Reputation: 353Reputation: 353
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?
 
1 members found this post helpful.
Old 02-25-2010, 04:25 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
Quote:
Originally Posted by gizza23 View Post
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`
.
 
1 members found this post helpful.
Old 02-25-2010, 04:26 PM   #5
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Debian, SL
Posts: 5,319

Rep: Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142Reputation: 1142
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.

Last edited by evo2; 02-25-2010 at 04:27 PM.
 
Old 02-25-2010, 04:26 PM   #6
gizza23
Member
 
Registered: Jun 2005
Location: Chicago, IL, USA
Distribution: Fedora Core, CentOS
Posts: 188

Original Poster
Rep: Reputation: 31
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...
 
Old 02-25-2010, 04:28 PM   #7
mattca
Member
 
Registered: Jan 2009
Distribution: Slackware 14.1
Posts: 321

Rep: Reputation: 52
Quote:
Originally Posted by gizza23 View Post
I still don't know why the prior didn't work though
The previous responses should answer that for you
 
Old 02-25-2010, 04:37 PM   #8
gizza23
Member
 
Registered: Jun 2005
Location: Chicago, IL, USA
Distribution: Fedora Core, CentOS
Posts: 188

Original Poster
Rep: Reputation: 31
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!

Last edited by gizza23; 02-25-2010 at 04:40 PM.
 
  


Reply

Tags
file, grep, manual


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
how can I "cat" or "grep" a file to ignore lines starting with "#" ??? callagga Linux - Newbie 7 08-16-2013 06:58 AM
LFS 6.2 Ch. 6.14 - 2nd Coreutils test returns "/bin.bash: No such file or directory" 0graham0 Linux From Scratch 2 08-23-2007 06:32 PM
How to write a bash script to replace all "KH" to "K" in file ABC??? cqmyg5 Slackware 4 07-24-2007 09:00 AM
fsck returns "run manually" ursahoribl Linux - General 4 10-13-2006 11:40 PM
"grep: /etc/issue: No such file or directory" hunter_one Linux - Software 6 10-15-2003 03:27 PM


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

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration