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.
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.
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.
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.
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).
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