LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 10-31-2017, 07:21 PM   #1
ep7network0819
Member
 
Registered: May 2015
Posts: 31

Rep: Reputation: Disabled
If Then Else Statement not Working?


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.
 
Old 10-31-2017, 07:47 PM   #2
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
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.)
 
1 members found this post helpful.
Old 10-31-2017, 11:37 PM   #3
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Exclamation

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

Code:
find . -type f -print | xargs grep -l foo > files.txt; echo $?
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.
 
1 members found this post helpful.
Old 10-31-2017, 11:48 PM   #4
ep7network0819
Member
 
Registered: May 2015
Posts: 31

Original Poster
Rep: Reputation: Disabled
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.
 
Old 11-01-2017, 12:08 AM   #5
ep7network0819
Member
 
Registered: May 2015
Posts: 31

Original Poster
Rep: Reputation: Disabled
At Turbocapitalist, It did not work.
 
Old 11-01-2017, 12:09 AM   #6
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by ep7network0819 View Post
At Turbocapitalist, It did not work.
What did not work?
 
1 members found this post helpful.
Old 11-01-2017, 04:41 AM   #7
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
For a reliable exit code you need to place the grep outside the find
Code:
if find ~/Desktop -type f -name "*.log" | xargs grep -q "Failed"
then
Or test the printed lines
Code:
if [ -n "$(find ~/Desktop -type f -name "*.log" -exec grep -l "Failed" {} +)" ]
then

Last edited by MadeInGermany; 11-01-2017 at 08:43 AM. Reason: Added missing xargs in 1st solution
 
1 members found this post helpful.
Old 11-01-2017, 05:57 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
another approach:
Code:
if grep -l -R Failed ~/Desktop | grep -q '\.log$'; then
   do something
else
   echo not found
fi
 
1 members found this post helpful.
Old 11-01-2017, 07:56 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP: Let's assume you don't have white-spaces and special characters in your file-names. Then the start of your script should be like this:

Code:
FileList=$(find ~/Desktop -name '*.log' -type f | xargs grep -l -- 'Failed' | tr '\n' ' ')
if [ -n $FileList ]; then 
    echo "Do Something With $FileList (eg scp $FileList user@host:)"
fi

Last edited by NevemTeve; 11-01-2017 at 11:00 AM.
 
Old 11-01-2017, 09:20 AM   #10
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,779

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by ep7network0819 View Post
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 ... ".
 
Old 11-08-2017, 10:06 PM   #11
ep7network0819
Member
 
Registered: May 2015
Posts: 31

Original Poster
Rep: Reputation: Disabled
@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.
 
Old 11-08-2017, 10:43 PM   #12
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,307
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by ep7network0819 View Post
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.
 
1 members found this post helpful.
  


Reply

Tags
bash scripting, centos7, scripts, shell scripting



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
[SOLVED] bash if statement not working trscookie Programming 6 06-22-2016 06:09 AM
[SOLVED] Awk: if statement not working shivaa Linux - Newbie 7 12-14-2012 01:43 PM
[SOLVED] less than condition in if statement not working as desired samasat Linux - Newbie 11 06-09-2012 05:33 PM
BASH: IF statement not working? EdinburghLad Programming 6 05-15-2012 08:26 PM
Case statement in function not working cmosentine Programming 8 02-14-2012 12:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:33 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration