LinuxQuestions.org
Help answer threads with 0 replies.
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 03-14-2013, 12:12 AM   #1
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Rep: Reputation: 38
Question Bash conditional | getting logic wrong?


I have a file

Code:
cat <<EOF > /tmp/test
Line one
Line two 
Line three
EOF
Now I am trying to get the exit stat ($?) of 1 if any text is found and 0 if any text is not found using grep, i.e. just reversing the exit status of grep

Code:
# (snippet 1) this one is not working!!! retval $? should be 1
grep -q 'one' /tmp/test && false || true
echo $?


# (snippet 2) but this one is working - retval is 1
grep -q 'one' /tmp/test
if [ $? -eq 0 ]; then 
    false
else 
    true
fi
echo $?
What am I missing in snippet 1?

Last edited by the_gripmaster; 03-14-2013 at 12:14 AM.
 
Old 03-14-2013, 01:42 AM   #2
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,927

Rep: Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320Reputation: 7320
yes, grep sets return value itself, you do not need to check and set it.
grep <something> && <command> means command will be executed if grep was successful, so the return code was 0.
false simple returns 1.
grep <something> && false || <command> means command will be executed if false executed (and failed, but it cannot do anything else).
So grep 'one' will return 0, false and true will not be executed.
grep 'four' will return 1, therefore false will be executed and furthermore true will be executed also. True will return 0.
 
Old 03-14-2013, 09:34 AM   #3
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,784

Rep: Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083Reputation: 2083
Quote:
Originally Posted by the_gripmaster View Post
just reversing the exit status
Consider using !:
Code:
$ ! true ; echo $?
1
$ ! false ; echo $?
0
Quote:
3.2.2 Pipelines

A pipeline is a sequence of simple commands separated by one of the control operators ‘|’ or ‘|&’.

The format for a pipeline is

Code:
[time [-p]] [!] command1 [ [| or |&] command2 …]
...

If the reserved word ‘!’ precedes the pipeline, the exit status is the logical negation of the exit status as described above.
 
Old 03-14-2013, 09:49 AM   #4
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by the_gripmaster View Post
Code:
grep -q 'one' /tmp/test && false || true
Let's think about this code for a little. First of all, we have:
Code:
grep … && false
I wonder what that would evaluate to… What could it be… Could it be that “whatever and false” always yields “false”?

And later, after the above evaluates to false (ie. non-zero exit code), you end up with an equivalent of:
Code:
false || true
Again, what could this result in? Could it be that “whatever or true” always yields “true”?
 
Old 03-15-2013, 09:02 AM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
".. && .. || .." is not exactly equivalent to "if..then..else".

See Bash Pitfall #22


In short, while it's ok to use most of the time, if the second command can ever return false then it's probably not going to do what you want it to.
 
Old 03-16-2013, 03:16 AM   #6
the_gripmaster
Member
 
Registered: Jul 2004
Location: VIC, Australia
Distribution: RHEL, CentOS, Ubuntu Server, Ubuntu
Posts: 364

Original Poster
Rep: Reputation: 38
Quote:
Originally Posted by David the H. View Post
".. && .. || .." is not exactly equivalent to "if..then..else".
Right. What I was looking for is

Code:
! grep -q 'one' /tmp/test
 
  


Reply



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] Conditional statement not working bash script martin083 Programming 2 01-18-2013 06:58 AM
[SOLVED] Scripting logic: where am I going wrong? Raveolution Linux - General 20 06-16-2011 10:01 AM
[SOLVED] Bash conditional help the_gripmaster Linux - General 5 09-08-2010 01:33 AM
BASH script alphabetical conditional operator query. lupusarcanus Linux - Newbie 5 03-15-2010 03:14 AM
bash conditional questions teufelhund Programming 19 04-22-2006 04:13 PM

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

All times are GMT -5. The time now is 12:47 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