LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-15-2008, 03:57 PM   #1
jstu
Member
 
Registered: Jan 2002
Distribution: slackware
Posts: 193

Rep: Reputation: 30
bash if statement


Very simple question, but it is stumping me. Could anyone explain why this if statement prints the value of $a?

Code:
a="hey"
if [[ $a != "hey" || $a != "what" ]]; then
        echo $a
fi

Thanks
 
Old 02-15-2008, 04:15 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
The if statement contains the boolean "OR" operator (||). Thus if either expression is true, then the if is true, and it goes to the echo command.

$a != "what" is true.....therefore the if is true.
 
Old 02-15-2008, 04:18 PM   #3
jstu
Member
 
Registered: Jan 2002
Distribution: slackware
Posts: 193

Original Poster
Rep: Reputation: 30
Thanks. I just didnt understand the logic behind the or. I figured that sicne the first one was false it should have stopped.
 
Old 02-15-2008, 04:42 PM   #4
dive
Senior Member
 
Registered: Aug 2003
Location: UK
Distribution: Slackware
Posts: 3,211

Rep: Reputation: 292Reputation: 292Reputation: 292
|| = or
&& = and
 
Old 02-15-2008, 04:43 PM   #5
urka58
Member
 
Registered: Nov 2003
Distribution: slackware 14
Posts: 530

Rep: Reputation: 30
Logical operations (and/or) are somehow confusing..
You should preferably refer to them in terms of exit status of the command not in terms of the common meaning of yes/no. In this case test exits 0 (true) , so the script continues to the further line (echo bal_bla).
Ciao
 
Old 02-15-2008, 05:35 PM   #6
jstu
Member
 
Registered: Jan 2002
Distribution: slackware
Posts: 193

Original Poster
Rep: Reputation: 30
Thanks. I guess I just didnt know when the if statement exited.
 
Old 02-15-2008, 07:34 PM   #7
JWPurple
Member
 
Registered: Feb 2008
Posts: 67

Rep: Reputation: 17
Further, "short-cut" evaluation is used (as in C):

In an "or" condition, if the first term is true, there's no reason to evaluate the rest, and it's skipped.

In an "and" condition, if the first term is false, there's no reason to evaluate the rest, and it's skipped.
 
Old 02-15-2008, 07:48 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by jstu View Post
Very simple question, but it is stumping me. Could anyone explain why this if statement prints the value of $a?

Code:
a="hey"
if [[ $a != "hey" || $a != "what" ]]; then
        echo $a
fi

Thanks
Code:
a="hey"
case $a in 
   hey|what ) echo "no";;
   * ) echo "yes";;
esac
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Bash if statement noir911 Programming 2 08-20-2006 05:43 AM
BASH IF statement kinetik Programming 10 05-07-2006 02:48 AM
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
bash statement os2 Programming 2 03-20-2005 10:13 PM
bash for statement with 2 arrays? Noerr Linux - General 10 05-27-2002 12:58 PM


All times are GMT -5. The time now is 12:05 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration