[SOLVED] Two conditions in "if" statement: if first fails, does it check second condition?
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Two conditions in "if" statement: if first fails, does it check second condition?
I hope I don't embarrass myself with the question, and I presume the answer, but wanted to verify. I tried to look this up on Google, but wasn't sure how to properly describe it, and so couldn't find anything that gave an answer on it.
Question: when there are two conditions in an if / then statement with certain required matches, and the first match is not met, does the script then make a conclusion at that point? Or does it always check both conditions regardless?
So, for example:
Code:
if [ "$item1" = 'A' ] && [ "$item2" = 'B' ]; then
echo "Success"
else
echo "Fail"
fi
and $item1 does not equal A, does the script stop without checking the other condition and put me to "else"? Or will it always check the second condition [ "$item2" = 'B' ]?
Maybe more detail than anyone wants, but my reason: $item1 = From my computer, I want to check whether my phone is connected to the network - and this can be any network:
Code:
nmap -T4 -A -v -oG - `/sbin/ip -o -4 addr list ***MY WIRELESS DEVICE*** | awk '{print $4}' | cut -d/ -f1 | grep -Po '.*(?=\.)'`.USUAL PORT RANGE -Pn -p PORT NUMBER WHERE PHONE WILL LISTEN | grep 'MY PHONE NAME' | grep -Pom 1 '[0-9.]{7,15}' | sort -u
$item 2 = If it is, I verify with a test SSH. If $item1 fails, I don't want to send a test login to some random IP address on the network. For reference that test login:
(I have a bunch of other protections in place - I only turn on my phone's Wifi and SSH server, check if I'm on a non-standard IP, check if on VPN, etc. etc. My script works now, just trying to clean up and simplify. I'll probably post the final code in case it benefits someone else.)
Question: when there are two conditions in an if / then statement with certain required matches, and the first match is not met, does the script then make a conclusion at that point?
After the if normal shell code follows until it reaches a then, at that point the $? (last exit status) decides if the following normal shell code up to a fi or else is run.
The following demonstrates it (I hope)
Code:
if
echo "now running test command(s)"
[ "$item1" = 'A' ] && [ "$item2" = 'B' ]
then
echo "Success"
else
echo "Fail"
fi
So, the question is about normal shell code rather than a specific if-then behavior.
Indeed && and || are logical operators.
If the $? is not 0 (false) then a && command is skipped.
If the $? is 0 (true) then a || command is skipped.
After the if normal shell code follows until it reaches a related then, at that point the $? (last exit status) decides if the following normal shell code up to a related fi or else is run.
The following demonstrates it (I hope)
Code:
if
echo "now running test command(s)"
[ "$item1" = 'A' ] && [ "$item2" = 'B' ]
then
echo "Success"
else
echo "Fail"
fi
So, the question is about normal shell code rather than a specific if-then behavior.
Indeed && and || are logical operators.
If the $? is not 0 (false) then a && command is skipped.
If the $? is 0 (true) then a || command is skipped.
Note the "inverse" values for true and false, this is specific to the shell.
Other programming languages have non-0 for true and 0 for false
Last edited by MadeInGermany; 03-04-2020 at 11:57 PM.
If $item1 fails, I don't want to send a test login to some random IP address on the network.
Speaking of feedback, in that case you really ought to use an SSH key for authentication instead of sending out passwords for harvesting if you somehow manage to contact the wrong server. The key can be locked down on the server end so that it does nothing:
Code:
command="true" ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIHjK/gl+HLaqkwoUCxksnqueQmETAz6GwSMYsQD36VV6 network test
Then it will return success but disconnect immediately if it gets through.
The official name for this idea is "short-circuit" expression evaluation – routinely used for if statements and other conditionals. As soon as it can be determined that the branch will or will not be taken, further evaluation is not performed. This very expressive notion is used a lot, because it makes "clean" code.
Contrast this to bitwise evaluation within, say, an assignment statement.
Last edited by sundialsvcs; 12-07-2023 at 08:02 AM.
The official name for this idea is "short-circuit" expression evaluation – routinely used for if statements and other conditionals. As soon as it can be determined that the branch will or will not be taken, further evaluation is not performed. This very expressive notion is used a lot, because it makes "clean" code.
The problem is that many people don't know about it, so they don't use it properly (that's why we have this thread too).
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.