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.
I cannot get my script to work using if-then-else statements. The script seems to always evaluate as true. Therefore, if the word Failed doesn't exist it still uploads a file to FTP server. I thought by using if-then-else statements the script will perform different computations depending on condition true or false. The script is designed to search for the word Failed, and if not found then close program and do nothing. The script seems to always evaluate as true, and as a result uploads file to FTP server whether the word Failed exist or not.
My script is below:
Code:
!/bin/bash
# Seach for word Failed
if find ~/Desktop -type f -name "*.log" -exec grep -l "Failed" {} \+
then
# Enter FTP command to upload file to xxxxx
HOST="x.x.x.x"
USERNAME="xxxxx"
PASSWORD="xxxxx"
# Directory to pick up file
FILE="~/Desktop/xxxxx.txt"
# Login to remote server
lftp $HOST <<END_SCRIPT
user $USERNAME $PASSWORD
cd xxxxx
put $FILE
bye
END_SCRIPT
else
echo "Failed not found"
fi
Any suggestions in making my if-then-else statements work? The script runs without error, I think maybe the if-then-else
are wrong. Thanks for your time.
Last edited by ep7network0819; 10-31-2017 at 07:30 PM.
Reason: Didn't add my entire script correctly.
The find command does not pass back the return code from a command run by "-exec". The only times find exits with a non-zero return code is if there is a syntactical error in its arguments, if a "-delete" action fails, or under the uncommon condition that a file disappears after its name has been read from a directory but before it can be processed.
Plus, your script does not actually do anything with the list of names returned by grep. It's not apparent just what you are trying to do. Perhaps you need to save that list of names in a variable. (Be careful doing that if there is any chance that the names might contain embedded space characters.)
I see that GNU find 4.7.0 does pass an exit code based on -exec, but it would not be safe to rely on other versions of find acting that way. Perhaps a portable option would be to pass everything to xargs
But that does not address the FTP problem. FTP is not safe and cannot be made safe. So a priority ought to be made to remove it, and quickly. SFTP is part of the package OpenSSH-server and uploading can be automated through the use of keys and the batch mode.
Hi rknichols. Thanks for taking the time to reply to my post.
You mention
Quote:
Plus, your script does not actually do anything with the list of names returned by grep.
Well, one thing is for sure! If I run this line of code "find ~/Desktop -type f -name "*.log" -exec grep -l "Failed" {} \+" by itself, it locates the word failed and outputs the name of the file that contains the word failed. If the word failed doesn't exist, program ends.
Well, one thing is for sure! If I run this line of code "find ~/Desktop -type f -name "*.log" -exec grep -l "Failed" {} \+" by itself, it locates the word failed and outputs the name of the file that contains the word failed. If the word failed doesn't exist, program ends.
That program ends regardless of whether anything was printed on the screen. Other than what you see on the screen, there is nothing to indicate whether or not the word was found. The return code is always zero, so there is nothing useful to test with "if ... ".
@pan64 and @MadeInGermany both your solutions worked. But when "Failed" isn't found echo command doesn't output "Failed not found". I tried putting echo command in a variable and it did not work.
Last edited by ep7network0819; 11-08-2017 at 10:08 PM.
Reason: Miss spelled a word.
I tried putting echo command in a variable and it did not work.
Again, what didn't work? Please show the exact code that is not working and be sure to remember to wrap it in [code] [/code] tags so that it is readable.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.