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. Numeric value 1 and numeric value 2 need to be specified within the range of 2147483648 to 2147483647.
Conditional expression using an operator |
Evaluation |
---|---|
numeric-value-1 -eq numeric-value-2 |
True if numeric-value-1 is equal to numeric-value-2. |
numeric-value-1 -ne numeric-value-2 |
True if numeric-value-1 is not equal to numeric-value-2. |
numeric-value-1 -ge numeric-value-2 |
True if numeric-value-1 is equal to or greater than numeric value-2. |
numeric-value-1 -gt numeric-value-2 |
True if numeric-value-1 is greater than numeric-value-2. |
numeric-value-1 -le numeric-value-2 |
True if numeric-value-1 is equal to or less than numeric-value-2. |
numeric-value-1 -lt numeric-value-2 |
True if numeric-value-1 is less than numeric-value-2. |
Conditional expression using an operator |
Evaluation |
---|---|
numeric-value-1 == numeric-value-2 |
True if numeric-value-1 is equal to numeric-value-2. |
numeric-value-1 != numeric-value-2 |
True if numeric-value-1 is not equal to numeric-value-2. |
numeric-value-1 >= numeric-value-2 |
True if numeric-value-1 is equal to or greater than numeric value-2. |
numeric-value-1 > numeric-value-2 |
True if numeric-value-1 is greater than numeric-value-2. |
numeric-value-1 < numeric-value-2 |
True if numeric-value-1 is less than numeric-value-2. |
numeric-value-1 <= numeric-value-2 |
True if numeric-value-1 is equal to or less than numeric-value-2. |
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.Operators for comparing character string can be used with the test command [[ ]] and [ ]. Operators cannot be used with the let command, (( )).
Conditional expression using an operator |
Evaluation |
---|---|
Character string |
True if the length of character string is one or more characters. This operator cannot be used in [[ ]] commands. |
-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. |
-z character string |
True if the length of character string is 0. |
-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 5.6.1 Shell options that can be specified with the set command. |
character string = pattern |
True if the character string matches pattern. |
character string == pattern |
True if the character string matches pattern. |
character string != pattern |
True if the character string does not match pattern. |
character string 1 < character string 2# |
character string 1 and character string 2 are compared in the order of ASCII codes. True if character string 2 is greater than character string 1. |
character string 1 > character string 2# |
character string 1 and character string 2 are compared in the order of ASCII codes. True if character string 1 is greater than character string 2. |
- #
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 fi
For details about wildcard characters, see (5) Wildcards.
(3) File attributes
The following table lists and describes the operators used for evaluating file attributes, such as file formats and permissions.Operators for file attribute evaluation can be used with the test command, [[ ]], and [ ]. Operators cannot be used with the let command, (( )).
Conditional expression using an operator |
Evaluation |
---|---|
-a file |
True if file exists. |
-b file |
True if file exists and it is a block type device. |
-c file |
True if file exists and it is a character type device. |
-d file |
True if file exists and it is a directory. |
-e file |
True if file exists. |
-f file |
True if file exists and it is a regular file. |
-g file |
True if file exists and the setgid bit is set. |
-h file |
True if file exists and it is a symbolic link. |
-k file |
True if file exists and the sticky bit is set. |
-p file |
True if file exists and it is a pipe file. |
-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. |
-s file |
True if the following conditions are all satisfied: [In Windows]
[In UNIX]
As the conditions for true differ between UNIX and Windows for -s, use -d to check if file is a directory or folder. |
-t fd |
True if this is fd whose terminal is open. |
-u file |
True if file exists and the setuid bit is set. |
-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. |
-x file |
[In Windows] 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. |
-G file |
True if file exists and the group to which file belongs matches the ID of the group executing the calling process. |
-L file |
True if file exists and it is a symbolic link. |
-O file |
True if file exists and its owner has a valid user ID for the process. |
-S file |
True if file exists and it is a socket. |
file1 -ef file2 |
True if file1 and file2 exist and the entities of file1 and file2 are the same (their symbolic link or hard link targets are the same). |
file1 -nt file2 |
True if file1 and file2 exist and the last modified date and time of file1 is more recent than the last modified date and time of file2. Also true if file1 exists and file2 does not exist. |
file1 -ot file2 |
True if file1 and file2 exist and the last modified date and time of file1 is earlier than the last modified date and time of file2. Also true if file2 exists and file1 does not exist. |
-H file |
Always false. |
- #:
-
For details on the CHILDJOB_SHEBANG parameter, see 7.3.7 CHILDJOB_SHEBANG parameter (defines an executable program path for job definition script files that are to be executed as child jobs).
For details on the CHILDJOB_EXT parameter, see 7.3.5 CHILDJOB_EXT parameter (defines an extension for job definition script files that are to be executed as child jobs).
Notes when using an operator of the file attribute are as follows:
-
If a symbolic link is specified for file, the link destination is evaluated. However, the subject of evaluation is different for the following operators:
-
For -h and -L, the symbolic link is evaluated.
-
For -x, both the symbolic link and the link destination are evaluated [Windows edition].
-
-
In a Windows environment, the result is always false because this is determined for nonexistent file types and flags [Windows edition].
-b, -c, -g, -k, -p, -u, -S
-
However, the following operator can issue a message and result in an error or be treated as normal by specifying the UNSUPPORT_TEST parameter. For details of the parameter, see 7.3 Environment setting parameters > 7.3.49 UNSUPPORT_TEST parameter (defines the handling of an unsupported conditional expression) (Windows only) [Windows edition].
-G, -O
-
The following operator returns the result according to the specification of the parameter if the UNSUPPORT_TEST parameter is specified. Do not use the UNSUPPORT_TEST parameter for the following operators when executing the check as an operator [Windows edition].
-h, -L, -ef
-
Do not specify a value of 10 or above for the argument of the operator -t. If a value of 10 or above is specified, the value cannot be guaranteed.
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.Ternary operator can be used with the let command (()).
Conditional expression using an operator |
Evaluation |
---|---|
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. |
The following shows an example of the ternary operator.
VAR1=3 VAR2=2 ANSWER=0 ((ANSWER=VAR1>VAR2?8+VAR1:8*VAR2)) echo $ANSWER