Hitachi

JP1 Version 11 JP1/Advanced Shell Description, User's Guide, Reference, and Operator's Guide 


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.

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.

Table 5‒22: Operators that are used for comparing values with the test command

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.

Table 5‒23: Operators that are used for comparing values with the let command

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

Table 5‒24: Operators used for comparing character strings

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" ]] [Figure] The wildcard character is invalid. The character string "ads?" is compared.
[[ $str1 != $str3 ]] [Figure] 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, (( )).

Table 5‒25: Operators used for evaluating file attributes, such as file formats and permissions

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]

  • file exists

  • file is not a folder

  • The file size is at least 1 byte

[In UNIX]

  • file exists

  • The file size is at least 1 byte or file is a directory

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:

  • True if file is other than a symbolic link

    - The extension is ".com", ".exe", ".cmd", or ".bat"

    - This is a directory

    The file satisfies the condition specified in the CHILDJOB_EXT or CHILDJOB_SHEBANG parameter (including the default definition) of the environment file.#

  • True if file is a symbolic link

    - The extension of the symbolic link and link destination file is ".com", ".exe", ".cmd", or ".bat"

    file is a symbolic link for a directory and the reference destination is a directory.

[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:

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.

Table 5‒26: 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 (()).

Table 5‒27: Ternary operator that can be used by JP1/Advanced Shell

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