LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 06-30-2023, 12:08 PM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
What is wrong with these If statements ?


hello everyone , i can not figure out why this does not work correctly , need some help .

Example script ahead , execute it and set var1 as 0 and var2 as 1 and the output will say that are both at 1 !!!

Code:
#!/bin/bash
echo -n "Set Var 1 (0 or 1) : "
read -r var1
echo -n "Set Var 2 (0 or 1) : "
read -r var2


if [ "$var1" == "1" ] || [ "$var2" == "1" ]
then
echo "Both at 1"
elif [ "$var1" == "0" ] || [ "$var2" == "1" ]
then
echo "Var1 at 0 | var2 at 1"
elif [ "$var1" == "1" ] || [ "$var2" == "0" ]
then
echo "Var1 at 1 | var2 at 0"
elif [ "$var1" == "0" ] || [ "$var2" == "0" ]
then
echo "Both at 0"
fi
exit 0
 
Old 06-30-2023, 12:14 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,715

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
You need to use and (&&) instead of or (||)
 
1 members found this post helpful.
Old 06-30-2023, 12:18 PM   #3
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
lol , yes , simple as that , i read somewhere that i could use || also , probably it is used for other purposes .

Thank you
 
Old 06-30-2023, 12:31 PM   #4
teckk
LQ Guru
 
Registered: Oct 2004
Distribution: Arch
Posts: 5,138
Blog Entries: 6

Rep: Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827Reputation: 1827
&& and
|| or
 
Old 06-30-2023, 01:11 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,715

Rep: Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899Reputation: 5899
Binary logic
1 || 1 = 1
1 || 0 = 1
0 || 1 = 1
0 || 0 = 0

1 && 1 = 1
1 && 0 = 0
0 && 1 = 0
0 && 0 = 0
 
Old 07-01-2023, 09:17 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,780

Rep: Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212Reputation: 2212
Quote:
Originally Posted by michaelk View Post
Binary logic
1 || 1 = 1
1 || 0 = 1
0 || 1 = 1
0 || 0 = 0

1 && 1 = 1
1 && 0 = 0
0 && 1 = 0
0 && 0 = 0
Unless you're talking about process return codes. For those, 0 indicates success ("True") and non-zero indicates failure ("False"). So,
True && False = False
True || False = True
ends up being effectively
0 && 1 = 1
0 || 1 = 0
Sometimes it's best not to think too much about what is actually happening under the hood.

Some say that the reason the Roman empire fell is that their numbering system did not include a zero, and thus their C programs had no way to indicate "success".
 
  


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] are these two mount statements the same jzoudavy Linux - Newbie 5 01-22-2015 05:51 PM
if statements and case statements not working in bourne shell script mparkhurs Programming 3 06-12-2004 02:41 AM
PHP if statements and variables antken Programming 4 09-24-2003 12:45 PM
if then statements vapor Programming 4 07-16-2003 06:44 AM
IF statements in PHP antken Programming 6 09-17-2002 09:38 AM

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

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