Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shell Scripting If [ -f ./file ]

Tags:

I am learning shell Scripting on my own, and I was investigation how to do the If, and I didn’t understand an example that had:

if [ -f ./$NAME.tar ]; then     //do something else     //something else 

Now I did some experimenting, and I gave NAME the name of a file I had on my directory. When I executed without the -f, it was entering in the else condition, but with -f it enters the //do something condition So I presume -f is for file. Is this correct? I just couldn’t find information to confirm this.

like image 555
Alessandroempire Avatar asked Mar 19 '13 22:03

Alessandroempire


People also ask

Can we use if in shell script?

Conditions in Shell ScriptsAn if-else statement allows you to execute iterative conditional statements in your code. We use if-else in shell scripts when we wish to evaluate a condition, then decide to execute one set between two or more sets of statements using the result.

How do you check if a file is a directory in shell script?

Checking If a Directory Exists In a Bash Shell Script -h "/path/to/dir" ] && echo "Directory /path/to/dir exists." || echo "Error: Directory /path/to/dir exists but point to $(readlink -f /path/to/dir)." The cmd2 is executed if, and only if, cmd1 returns a non-zero exit status.

What is in if condition in shell script?

If specified condition is not true in if part then else part will be execute. To use multiple conditions in one if-else block, then elif keyword is used in shell. If expression1 is true then it executes statement 1 and 2, and this process continues. If none of the condition is true then it processes else part.


1 Answers

From bash manual:

  -f file - True if file exists and is a regular file. 

So yes, -f means file (./$NAME.tar in your case) exists and is a regular file (not a device file or a directory for example).

like image 102
kamituel Avatar answered Oct 07 '22 10:10

kamituel