I have two options when it is 1 it has do something or 2 it has to do something when the option is not in the range i have to show an error message. Now i understand why the code wasn't running. It was a logical error.
It is not gonna work with AND because your script is breaking the logic.
The funciton AND in your script is telling to compiler that condition inside the brackets will be "TRUE" only when "opt" be ( TRUE and TRUE).
AND is a boolean expression where A * B = Output
AND is multiplier function like (A x B = Output) that multiply binary values 0 and 1.
The Output of AND can return True or False in test conditions
Let's assume the following AND truth table
AND output will be "1=true" only when A=1 and B=1 at same time.
input A * input B = Output
0 * 0 = 0 (false)
0 * 1 = 0 (false)
1 * 0 = 0 (false)
1 * 1 = 1 (true)
Then your Output will be 1= true only when you satisfy both inputs (A, B) with true values. In your script when opt=5 it will always return the second state of our truth table (A=0 * B=1) = 0 (false). To check the logic replace "A" and "B" by opt value.
Atention!!! "IF" also returns (true=1 or false=0)
Now pay attention the condition your script is asking for compiler to check:
IF (opt < 1 AND opt >2 ) = IF (opt <input A AND opt>input B)
Let's considere the truth table with your AND condition
opt=5;
IF (opt < 1 AND opt > 2 ) = output of your expression using AND
IF (opt<5 (false) AND opt>5 (true) ) = false (your output value is false)
your comparation is returning "0" to "IF" -> IF(0=false);
However if you use OR boolean it will work because the output of OR is as following:
OR is a boolean expression where A + B = Output
OR is an SUM function like (A + B = Output) that is summing binary values 0 and 1.
Then OR function can return True or False in tests conditions whenever A or B = 1.
Let's assume the following OR truth table
OR output will be "1=true" whenever A=1 or B=1
input A + input B = Output
0 + 0 = 0 (false)
0 + 1 = 1 (true)
1 + 0 = 1 (true)
1 + 1 = 1 (true)
Now let's considere the truth table with OR condition in your script:
opt=5;
IF(opt < 1 OR opt>2 ) = output of you expression using OR
IF(opt<5 (false) OR opt>5 (true)) = true (your output value is true)
your comparation is returning "1" to "IF" -> IF(1=true)
Then opt=5 it will be > 2 and return "1=true" to your IF test.
I hope have helped you, case you still didn't understand it please let me know and I will try to find a best way to explain...
I am trying to send a picture explaining AND and OR using lamps as examples, however it is not possible to paste a picture inside this current textbox.
7
u/rastamonsta84 Nov 14 '22
Outside of the bash question: How could the variable opt be less than 1 and greater than 2?
3
u/ellipticcode0 Nov 14 '22
$opt < 1 is false, this is why it will not get called inside the if block, you should not use &&, use || instead
1
u/SamuelLira99 Nov 14 '22
I guess you should remove the $ on first line:
opt=51
u/1PredictRiot Feb 11 '23 edited Feb 11 '23
You should use logical operator OR ||
if (($opt < 1 && $opt > 2));
The variable "opt" will never be <1 AND >2 at same time.
It will work if you use opt <1 OR opt >2.
if test $opt -lt 1 -a -gt 2; then
2
u/whetu Nov 14 '22
You don’t need $ inside (( )), and if blocks are closed with fi.
2
u/jrobelen Nov 15 '22
Because an if block ends with "fi" perhaps?
1
u/sched_yield Nov 15 '22
Use standard syntax.
opt=5if test $opt -lt 1 -a $opt -gt 2 ; thenecho "invalid"exitfi0
u/SamuelLira99 Nov 14 '22 edited Nov 14 '22
shell uses if [ ... ] instead of if(...) and integers are declared using
declare -i. also the less than and greater then operators are -lt and -gt-2
u/sunshine-x Nov 14 '22
bash is pain. use anything else, like python