Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'if -e' and 'if -f'

There are two switches for the if condition which check for a file: -e and -f.

What is the difference between those two?

like image 357
Ahatius Avatar asked Apr 18 '12 07:04

Ahatius


People also ask

What is the difference between if if and if Elif?

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!!!

What is the difference between if and if else?

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.

What is difference between if and only if?

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.

What is the difference between if if and if else in Python?

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.


1 Answers

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.

like image 72
Emil Vatai Avatar answered Oct 21 '22 13:10

Emil Vatai