You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
3.0 KiB
53 lines
3.0 KiB
File test operators
|
|
|
|
The test command includes the following FILE operators that allow you to test for particular types of files:
|
|
|
|
-b FILE - True if the FILE exists and is a special block file.
|
|
-c FILE - True if the FILE exists and is a special character file.
|
|
-d FILE - True if the FILE exists and is a directory.
|
|
-e FILE - True if the FILE exists and is a file, regardless of type (node, directory, socket, etc.).
|
|
-f FILE - True if the FILE exists and is a regular file (not a directory or device).
|
|
-G FILE - True if the FILE exists and has the same group as the user running the command.
|
|
-h FILE - True if the FILE exists and is a symbolic link.
|
|
-g FILE - True if the FILE exists and has set-group-id (sgid) flag set.
|
|
-k FILE - True if the FILE exists and has a sticky bit flag set.
|
|
-L FILE - True if the FILE exists and is a symbolic link.
|
|
-O FILE - True if the FILE exists and is owned by the user running the command.
|
|
-p FILE - True if the FILE exists and is a pipe.
|
|
-r FILE - True if the FILE exists and is readable.
|
|
-S FILE - True if the FILE exists and is a socket.
|
|
-s FILE - True if the FILE exists and has nonzero size.
|
|
-u FILE - True if the FILE exists, and set-user-id (suid) flag is set.
|
|
-w FILE - True if the FILE exists and is writable.
|
|
-x FILE - True if the FILE exists and is executable.
|
|
|
|
Comparison operators are operators that compare values and return true or false. When comparing strings in Bash you can use the following operators:
|
|
|
|
-eq - equals, -ne - not equal to
|
|
string1 = string2 and string1 == string2 - The equality operator returns true if the operands are equal.
|
|
Use the = operator with the test [ command.
|
|
Use the == operator with the [[ command for pattern matching.
|
|
string1 != string2 - The inequality operator returns true if the operands are not equal.
|
|
string1 =~ regex- The regex operator returns true if the left operand matches the extended regular expression on the right.
|
|
string1 > string2 - The greater than operator returns true if the left operand is greater than the right sorted by lexicographical (alphabetical) order.
|
|
string1 < string2 - The less than operator returns true if the right operand is greater than the right sorted by lexicographical (alphabetical) order.
|
|
-z string - True if the string length is zero.
|
|
-n string - True if the string length is non-zero.
|
|
|
|
Following are a few points to be noted when comparing strings:
|
|
|
|
A blank space must be used between the binary operator and the operands.
|
|
Always use double quotes around the variable names to avoid any word splitting or globbing issues.
|
|
Bash does not segregate variables by “type”, variables are treated as integer or string depending on the context.
|
|
|
|
There are multiple ways to check if a string contains a substring:
|
|
|
|
One approach is to use surround the substring with asterisk symbols * which means match all characters.
|
|
|
|
EX block:
|
|
|
|
if [ -z "$1" ];then
|
|
echo "Please enter something!"
|
|
else
|
|
echo "Thanks..."
|
|
fi
|
|
|