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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
02-25-2010, 04:12 PM
|
#1
|
|
Member
Registered: Jun 2005
Location: Chicago, IL, USA
Distribution: Fedora Core, CentOS
Posts: 188
Rep:
|
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.
|
|
|
|
02-25-2010, 04:22 PM
|
#2
|
|
Member
Registered: Jan 2009
Distribution: slackware + ratpoison
Posts: 253
Rep:
|
Quote:
Originally Posted by gizza23
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.
|
02-25-2010, 04:23 PM
|
#3
|
|
Senior Member
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Squeeze (Fluxbox WM)
Posts: 1,357
|
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.
|
02-25-2010, 04:25 PM
|
#4
|
|
Senior Member
Registered: May 2005
Posts: 4,420
|
Quote:
Originally Posted by gizza23
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.
|
02-25-2010, 04:26 PM
|
#5
|
|
Senior Member
Registered: Jan 2009
Location: Japan
Distribution: Debian
Posts: 3,716
|
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.
|
|
|
|
02-25-2010, 04:26 PM
|
#6
|
|
Member
Registered: Jun 2005
Location: Chicago, IL, USA
Distribution: Fedora Core, CentOS
Posts: 188
Original Poster
Rep:
|
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...
|
|
|
|
02-25-2010, 04:28 PM
|
#7
|
|
Member
Registered: Jan 2009
Distribution: slackware + ratpoison
Posts: 253
Rep:
|
Quote:
Originally Posted by gizza23
I still don't know why the prior didn't work though 
|
The previous responses should answer that for you 
|
|
|
|
02-25-2010, 04:37 PM
|
#8
|
|
Member
Registered: Jun 2005
Location: Chicago, IL, USA
Distribution: Fedora Core, CentOS
Posts: 188
Original Poster
Rep:
|
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 09:17 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|