There are two switches for the if
condition which check for a file: -e
and -f
.
What is the difference between those two?
The first form if-if-if test all conditions, whereas the second if-elif-else tests only as many as needed: if it finds one condition that is True, it stops and doesn't evaluate the rest. In other words: if-elif-else is used when the conditions are mutually exclusive. Hope this answers your question!!!
With the if statement, a program will execute the true code block or do nothing. With the if/else statement, the program will execute either the true code block or the false code block so something is always executed with an if/else statement.
IF AND ONLY IF, is a biconditional statement, meaning that either both statements are true or both are false. So it is essentially and “IF” statement that works both ways. Note that IF AND ONLY IF is different than simply ONLY IF.
In if, the statements inside the if block will execute, if the condition is true and the control is passed to the next statement after the if block. In the if else, if the condition is true, the statements inside the if block will execute and if the condition is false the statements in the if else block will execute.
See: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_07_01.html
I believe those aren't "if switches", rather "test switches" (because you have to use them inside [] brackets.
But the difference is:
[ -e FILE ]
True if FILE exists.
This will return true for both /etc/hosts
and /dev/null
and for directories.
[ -f FILE ]
True if FILE exists and is a regular file. This will return true for /etc/hosts
and false for /dev/null
(because it is not a regular file), and false for /dev
since it is a directory.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With