Conditional expressions are evaluated at script execution time, based on results, It executes speci fic command blocks.
thes
There are different types of conitional expressions in Bash
- String Comparision Operators
- Numric Comparision Operator
- File Operators
- Logical Operators
File Operators
Bash provides logical operators on FIle and directories to test condtional expressions. It allows you to check different operations such as existence, and permissions, size. It is used conditional expressions in conditional statements such as if else and case.
Syntax:
if [[ conditiona_expressions]]; then
# code to handle
fi
conditiona_expressions contains options, and filepath, that always returns true or false.
following are options provided
Operator | Description |
---|---|
-e file | Returns true if given file exists, file can be normal file or directory |
-f file | Returns true if given file exists and a file(not directory) |
-d file | Returns true if file is an directory |
-r file | Returns true if file exists and has readable permission |
-w file | Returns true if file exists and has writable permission |
-x file | Returns true if file exists and has executable permission |
-s file | Returns true if file exists and size is not empty |
-G file | Returns true if file exists and isowned by a Group ID that matches |
-O file | Returns true if file exists and owned by a user ID that matches |
-N file | Returns true if file exists and modified by last read date |
-L file | Returns true if file exists and and is an symbolic Link |
file1 -ot file2 | Returns true if file1 is older than file2 or file2 exists, file1 does not exist |
file1 -ne file2 | Returns true if file1 is newer than file2,file1 exists, file2 does not exists |
file1 -ef file2 | Returns true if file1 and file2 pointed to same device and inode |