Grep in bash script returns "No such file or directory", works manually
ProgrammingThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
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.
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
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.