Bash conditional | getting logic wrong?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash conditional | getting logic wrong?
# 1  
Old 03-14-2013
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?
# 2  
Old 03-14-2013
cmd || true will always return 0, since either cmd succeeds or true does. In your snippet #1, cmd is grep && false. That is always false, since either grep fails or false does.

Your snippet #1 therefore reduces to false || true. That will always return 0.

Since your grep command succeeds:
Code:
(grep && false) || true
(true && false) || true
(false) || true
true

Regards,
Alister

Last edited by alister; 03-14-2013 at 02:45 AM..
This User Gave Thanks to alister For This Post:
# 3  
Old 03-14-2013
Is there a way to shorten snippet 2?
# 4  
Old 03-14-2013
Quote:
Originally Posted by the_gripmaster
Is there a way to shorten snippet 2?
Looks like you just want to invert the status:
Code:
! grep -q 'one' /tmp/test

Regards,
Alister
This User Gave Thanks to alister For This Post:
# 5  
Old 03-14-2013
Cool. Thanks.
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Conditional bash/mysql query help

I think(hope) I've got a simple one - I just need to send an email if a mysql query returns any results (ideally - it will never match). Currently I just pipe the mysql query output to the mail program, but of course that emails regardless of the output( and I check this every 10 minutes from... (5 Replies)
Discussion started by: jcass78
5 Replies

2. Shell Programming and Scripting

What's wrong with my bash code?

I want to let sleep 3 in the background and echo $i pkglists="a b c d e f g" f() { local i set -- $pkglists && ((i +=2)) && sleep 3 &;echo $i } f (3 Replies)
Discussion started by: yanglei_fage
3 Replies

3. UNIX for Advanced & Expert Users

Bash conditional prompt?

Hi, Does anyone know any way of making bash prompt extended with conditional content? Example: export PS1="] && echo '#' || echo '\$'" # This won't work - prompt is not executed # export PS1="\$" # This is an existing but also working equivalent I would like to use more complex... (8 Replies)
Discussion started by: adderek
8 Replies

4. UNIX for Dummies Questions & Answers

Conditional statement in bash

I want to combine 2 conditional statements by using -o in bash, but it won't work. if ; then echo "The number needs to be between 0 and $nr" fi Each time i execute the file it says: ./selectCitaat: line 10: syntax error near unexpected token `$1' (3 Replies)
Discussion started by: doc.arne
3 Replies

5. Shell Programming and Scripting

Please help to fingure out what wrong with my tomcat restarting bash script

Hi, I am a nbee to Unix, I have used following script to check my tomcat is running or not and restart if it down. but actually it restart my tomcat each time running even my tomcat still running fine: Script that can run a check and perform an action if the check fails ... (1 Reply)
Discussion started by: quyennd
1 Replies

6. Shell Programming and Scripting

what's wrong with my bash script?

hi, please point out what's wrong with my script. im feeding it a list containing fqdn, sit should ssh into each and verify that atleast one of its virtual backup ip resolves into one of its virtual hostnames .. anyway the objective shows in the script... however, im having problems in the ... (4 Replies)
Discussion started by: ikk
4 Replies

7. Shell Programming and Scripting

Bash passes flags to shell wrong

Hi, first post, so hello to all. I have a Bash scripting problem that is driving me a bit nutty. It involves a program called 'convert' which is part of the ImageMagick collection. Normal usage from the commandline is: $ convert -resize 120x120 inputfile.jpg outputfile.jpg This is... (7 Replies)
Discussion started by: andyj
7 Replies
Login or Register to Ask a Question