5.2.2 Conditional expressions
Numeric value comparisons, character string comparisons, file attributes, logical operators, and ternary operators are used in conditional expressions. The following explains the specifications common to all conditional expressions.
-
The test or let command is used to evaluate conditions.
The test command includes the [[]] substitution format. The let command includes the (()) substitution format.
-
When you use the test command to evaluate conditions, insert a space between a variable and an operator. When you use [[]] instead of the test command, insert a space immediately after [[ and immediately before ]].
The following shows an example of a conditional using [[]].
if [[ $arg1 -eq $args ]]; then echo TRUE fi -
If an argument of the test command (such as -eq) is specified as an argument of the let command, the let command interprets that argument of the test command as a variable.
- Organization of this subsection
(1) Numeric value comparison
The following table lists and describes the operators used for comparing numeric values.
|
Conditional expression using an operator |
Evaluation |
Usage in test command or [[]] |
Usage in let command or (()) |
|---|---|---|---|
|
numeric-value-1 -eq numeric-value-2 |
True if numeric-value-1 is equal to numeric-value-2 |
Y |
N |
|
numeric-value-1 -ne numeric-value-2 |
True if numeric-value-1 is not equal to numeric-value-2 |
Y |
N |
|
numeric-value-1 -ge numeric-value-2 |
True if numeric-value-1 is equal to or greater than numeric-value-2 |
Y |
N |
|
numeric-value-1 -gt numeric-value-2 |
True if numeric-value-1 is greater than numeric-value-2 |
Y |
N |
|
numeric-value-1 -le numeric-value-2 |
True if numeric-value-1 is equal to or less than numeric-value-2 |
Y |
N |
|
numeric-value-1 -lt numeric-value-2 |
True if numeric-value-1 is less than numeric-value-2 |
Y |
N |
|
numeric-value-1 == numeric-value-2 |
True if numeric-value-1 is equal to numeric-value-2 |
Y#1 |
Y |
|
numeric-value-1 != numeric-value-2 |
True if numeric-value-1 is not equal to numeric-value-2 |
Y#1 |
Y |
|
numeric-value-1 >= numeric-value-2 |
True if numeric-value-1 is equal to or greater than numeric-value-2 |
N |
Y |
|
numeric-value-1 > numeric-value-2 |
True if numeric-value-1 is greater than numeric-value-2 |
Y#1, #2 |
Y |
|
numeric-value-1 < numeric-value-2 |
True if numeric-value-1 is less than numeric-value-2 |
Y#1, #2 |
Y |
|
numeric-value-1 <= numeric-value-2 |
True if numeric-value-1 is equal to or less than numeric-value-2 |
N |
Y |
- Legend:
-
Y: Permitted
N: Not permitted
- #1
-
Compared as character strings, not numeric values. For details about character string comparison, see 5.2.2(2) Character string comparison.
- #2
-
Can be used only in [[]]; cannot be used in any other format.
The following shows an example of numeric value comparison.
a=1 b=2 if [[ $a -lt $b ]]; then echo TRUE else echo FALSE fi while (( $a != $b )); do echo LOOP ((a+=1)) done
(2) Character string comparison
The following table lists and describes the operators used for comparing character strings.
|
Conditional expression using an operator |
Evaluation |
Usage in test command or [[]] |
Usage in let command or (()) |
|---|---|---|---|
|
character-string |
True if the length of character-string is one or more characters. This operator cannot be used in [[ ]] commands. |
Y |
N |
|
-n character-string |
True if the length of character-string is one or more characters. If the length of character-string is 0, the command outputs the KNAX6041-E message and terminates with an error with return code 2. If no character-string is specified in the [[ ]] command, a format error results, in which case the command outputs the KNAX6041-E message and terminates with an error with return code 1. |
Y |
N |
|
-z character-string |
True if the length of character-string is zero |
Y |
N |
|
-o character-string |
True if character-string matches the character string of the shell option that is currently valid. For details about the character string of the shell option, see Name in Table 5-34 Shell options that can be specified with the set command in 5.6.1 Shell options that can be specified with the set command. |
Y |
N |
|
character-string = pattern |
True if character-string matches pattern. |
Y |
N |
|
character-string == pattern |
True if character-string matches pattern. |
Y |
N |
|
character-string != pattern |
True if character-string does not match pattern. |
Y |
N |
|
character-string-1 < character-string-2 |
character-string-1 and character-string-2 are compared in the order of ASCII codes. If character-string-1 is less than character-string-2, the result is true. |
Y# |
N |
|
character-string-1 > character-string-2 |
character-string-1 and character-string-2 are compared in the order of ASCII codes. If character-string-1 is greater than character-string-2, the result is true. |
Y# |
N |
- Legend:
-
Y: Permitted.
N: Not permitted.
- #
-
Can be used only in [[]]; cannot be used in any other format.
Because any character string to be compared might contain one or more spaces, we recommend that you always enclose the entire character string in double quotation marks ("). The following shows examples.
str1="aaa" str2="bbb" test "$str1" == "$str2" [[ "$str1" == "$str2" ]]
You can specify the *, ?, and [...] wildcard characters in character strings to be compared. Note that wildcards can be specified only in [[ ]], not in any other format. Note that when a character string containing a wildcard character is enclosed in double quotation marks ("), the use of the wildcard character as a wildcard character becomes invalid. The following shows an example.
str1="adsh" str2="ads?" str3="ad*" [[ "$str1" == "$str2" ]]The wildcard character is invalid. The character string "ads?" is compared. [[ $str1 != $str3 ]]
The wildcard character is valid.
The following shows an example of character string comparison using [[]]. The *, ?, and [...]wildcard characters can be used.
if [[ abc == ab* ]]; then
echo TRUE
fiFor details about wildcard characters, see 5.1.6(5) Wildcards.
(3) File attributes
The following table lists and describes the operators used for evaluating file attributes, such as file formats and permissions.
|
Conditional expression using an operator |
Evaluation |
Usage in test command or [[]] |
Usage in let command or (()) |
|---|---|---|---|
|
-a file |
True if file exists. |
Y |
N |
|
-b file |
True if file exists and it is a block type device.#1 |
Y |
N |
|
-c file |
True if file exists and it is a character type device.#1 |
Y |
N |
|
-d file |
True if file exists and it is a directory. |
Y |
N |
|
-e file |
True if file exists. |
Y |
N |
|
-f file |
True if file exists and it is a regular file. |
Y |
N |
|
-g file |
True if file exists and the setgid bit is set.#1 |
Y |
N |
|
-h file |
True if file exists and it is a symbolic link.#2 |
Y |
N |
|
-k file |
True if file exists and a sticky bit is set.#1 |
Y |
N |
|
-p file |
True if file exists and it is a pipe file.#1 |
Y |
N |
|
-r file |
In Windows, the result is true if file exists; in UNIX, the result is true if file exists and it can be read from the current process. |
Y |
N |
|
-s file |
True if the following conditions are all satisfied: In Windows:
In UNIX:
With -s, because the conditions for true differ between UNIX and Windows, use -d to check if file is a directory or folder. |
Y |
N |
|
-t fd |
True if this is fd whose terminal is open.#3 |
Y |
N |
|
-u file |
True if file exists and the setuid bit is set.#1 |
Y |
N |
|
-w file |
In Windows, the result is true if the read-only attribute is not set or this is a directory; in UNIX, the result is true if file exists and it can be written from the current process. |
Y |
N |
|
-x file |
In Windows, the result is true if one of the following is true:
In UNIX, the result is true if file exists and it can be executed from the current process. |
Y |
N |
|
-G file |
True if file exists and the group to which file belongs matches the ID of the group executing the calling process.#2 |
Y |
N |
|
-L file |
True if file exists and it is a symbolic link.#2 |
Y |
N |
|
-O file |
True if file exists and its owner has a valid user ID for the process.#2 |
Y |
N |
|
-S file |
True if file exists and it is a socket.#1 |
Y |
N |
|
file1 -ef file2 |
True if file1 and file2 exist and the entities of file1 and file2 are the same (if either their symbolic link or hard link targets are the same).#2 |
Y |
N |
|
file1 -nt file2 |
True if file1 and file2 exist and the most recent modification date and time of file1 is more recent than the most recent modification date and time of file2; also true if file1 exists and file2 does not exist. |
Y |
N |
|
file1 -ot file2 |
True if file1 and file2 exist and the most recent modification date and time of file1 is earlier than the most recent modification date and time of file2; also true if file2 exists and file1 does not exist. |
Y |
N |
|
-H file |
Always false |
Y |
N |
- Legend:
-
Y: Permitted
N: Not permitted
- #1
-
In a Windows environment, the result is always false because this check occurs on nonexistent file types and flags.
- #2
-
If the test command is executed in a Windows environment, it always results in an error because the processing is not supported during evaluation. However, you can set it to issue a message and result in an error or to treat it as normal by specifying the UNSUPPORT_TEST parameter. For details about the parameter, see UNSUPPORT_TEST parameter (defines the handling of an unsupported conditional expression) (Windows only) in 7.3 Environment setting parameters.
- #3
-
Do not specify a value greater than 9 in fd. If you do, the value cannot be guaranteed.
- #4
-
For details about the CHILDJOB_SHEBANG parameter, see CHILDJOB_SHEBANG parameter (defines an executable program path for job definition script files that are to be executed as child jobs) in 7.3 Environment setting parameters.
For details about the CHILDJOB_EXT parameter, see CHILDJOB_EXT parameter (defines an extension for job definition script files that are to be executed as child jobs) in 7.3 Environment setting parameters.
The following shows an example of evaluating a file attribute.
FILE="$HOME/script/test.ash" if [[ -a $FILE ]]; then echo "$FILE exists." else echo "$FILE does not exist." fi
(4) Logical operations
The following table lists and describes the operators used for evaluation in logical operations.
|
Conditional expression using an operator |
Evaluation |
Usage in test command or [[]] |
Usage in let command or (()) |
|---|---|---|---|
|
expr1 -a expr2 |
True if the results of expr1 and expr2 are both true |
Y# |
N |
|
expr1 -o expr2 |
True if the result of either expr1 or expr2 is true |
Y# |
N |
|
expr1 && expr2 |
True if the results of expr1 and expr2 are both true |
Y |
Y |
|
expr1 || expr2 |
True if the result of either expr1 or expr2 is true |
Y |
Y |
|
! expr |
True if the result of expr is false |
Y |
Y |
- Legend:
-
Y: Permitted
N: Not permitted
- #
-
Cannot be used in [[]].
The following shows an example of a logical operation.
DIR="/tmp" FILE="/tmp/test.ash" a=2 b=4 if test -d $DIR -a -a $FILE then echo "$DIR is directory and $FILE exists." else echo "$DIR is not directory or $FILE does not exist." fi while ((a*0 || b-3)); do echo LOOP let b-=1 done
If you use && and || in the test command, specify them as follows:
a=1
b=2
c=3
if test "$a" == 1 && test "$b" == 2; then
echo "True"
else
echo "False"
fi
if test "$a" != "$b" || test "$a" != "$c"; then
echo "True"
else
echo "False"
fi
(5) Ternary operator
You can use the ternary operator, which is an abbreviated notation of if-else. The following table explains the ternary operator supported by JP1/Advanced Shell.
|
Conditional expression using an operator |
Evaluation |
Usage in test command or [[]] |
Usage in let command or (()) |
|---|---|---|---|
|
expr1?expr2: expr3 |
If the result of expr1 is true, the result of expr2 is returned. If the result of expr1 is false, the result of expr3 is returned. |
N |
Y |
- Legend:
-
Y: Permitted
N: Not permitted
The following shows an example of the ternary operator.
VAR1=3 VAR2=2 ANSWER=0 ((ANSWER=VAR1>VAR2?8+VAR1:8*VAR2)) echo $ANSWER