5.1.6 Metacharacters
Metacharacters are characters that have special meaning in job definition scripts. The following are the JP1/Advanced Shell metacharacters:
|, &, ;, <, >, (, ), $, `, ', \, ", ~, #, *, [, ], ?
If you want to use a metacharacter as a normal character, you must invalidate its metacharacter usage. The following table explains how to invalidate metacharacters.
Invalidation method |
Description |
---|---|
'str' |
The character string str processes all characters other than the single quotation mark (') as normal characters. |
"str" |
The character string str processes all characters other than the dollar sign ($), backslash (\), and grave accent mark (`) as normal characters. However, if \ is immediately followed by $, \, `, or ", then \ is treated as a metacharacter. To include \ as a normal character in the character string str, specify \\. |
\char |
Invalidates (escapes) the special meaning of the characters char. |
When invalidated metacharacters are output, the output processing depends on the specifications of the built-in command used, such as the echo and print commands.
- Contents of job definition script:
echo 'JP1/AS\n' # 1. echo "JP1/AS\n" # 2. echo JP1/AS\\n # 3. echo 'JP1/AS\\n' # 4.
- STDOUT file contents of the executed job:
******** JOB SCOPE STDOUT ******** JP1/AS As a result of 1, the echo command outputs \n as a line break. JP1/AS Result of 2 JP1/AS Result of 3 JP1/AS\n As a result of 4, \ is treated as an escape character and \n is output as distinct characters.
The following subsections explain functions using metacharacters.
- Organization of this subsection
(1) Positional parameters
When you execute a job definition script, you can pass parameters to the job definition script as arguments by specifying run-time parameters following the job definition script file name. JP1/Advanced Shell assigns these arguments to special variables called positional parameters. There are 10 positional parameters, $0 through $9. The file name of the executed job definition script is assigned to $0, and arguments are assigned to $1 through $9 in the order specified. The following table lists and describes the positional parameters of JP1/Advanced Shell and the related special characters.
Positional parameter or special character |
Description |
|
---|---|---|
Positional parameters |
$0 |
File name of the job definition script. |
$n |
Value of argument n specified in the job definition script (n: 1 through 9) |
|
Related special characters |
$# |
Number of arguments specified in the job definition script |
$* |
All arguments specified in the job definition script |
|
$@ |
All arguments specified in the job definition script |
|
"$*" |
Handles all arguments specified in the job definition script as a group. Example: "$1 $2 $3 ... " If the value of the IFS shell variable has been changed, the values are delimited by the new value of the IFS shell variable. |
|
"$@" |
Handles the arguments specified in the job definition script individually. Example: "$1" "$2" ... " If the IFS shell variable has been changed, the values are delimited by the space. |
You use the set standard shell command to change the positional parameters in a job definition script. For details about the set command, see set command (sets shell options, creates an array, or displays variable values) in 9.3 Standard shell commands.
(2) String separators
In JP1/Advanced Shell, the characters specified in the IFS shell variable are treated as string separators. The space and tab characters are treated as string separators because the initial values of the IFS shell variable are the space, tab, and end-of-line characters. Any number of consecutive spaces and tab characters are treated as a single separator. For details about the IFS shell variable, see 5.5 Shell variables.
To use space or tab characters in character strings, you must enclose the character string containing the space or tab character in quotation marks (' or ").
(3) Line continuation
The following explains how to specify a command over multiple lines:
-
Specify a backslash (\) at the end of a line that is to continue onto the next line.
-
Enclose the entire multi-line specification in a single set of quotation marks (but do not specify a quotation mark at the end of each line that is continued onto the next line).
JP1/Advanced Shell treats such a set of lines as continued lines and processes them as a single line.
(4) Comments
You can specify comments in job definition scripts. When a hash mark (#) is specified in a line, the rest of the line from the hash mark through the end of the line is treated as a comment. However, if a hash mark is followed by -adsh, the line is processed as follows:
-
Line that begins with #-adsh
This line is treated as an extended script command.
-
Specification of #-adsh continues onto the next line
The continuation line is treated as a comment line. The following shows an example:
echo ABC \ #-adsh
However, anytime #-adsh is not continued from the previous line or is not at the beginning of a line, an error results.
-
Coding other than #-adsh is specified
The line is treated as a comment line.
Note that a comment cannot be specified on a line that contains an extended script command.
(5) Wildcards
You can use wildcard characters to obtain file names and directory names that satisfy specified conditions and to compare such names with desired character strings.
The following table describes the wildcard characters supported in JP1/Advanced Shell.
Wildcard character |
Description |
---|---|
? |
Matches any single character, except the dot (.) in dot files. |
* |
Matches a character string consisting of any number of characters, except the dot (.) in dot files. |
[...] |
Match is based on the character string enclosed in the square brackets ([]). When ! is specified at the beginning of the character string enclosed in the square brackets, the wildcard matches any character string other than the character string that is enclosed in the square brackets. To specify ] as a character, specify it at the beginning of the character string. If two characters are separated by a hyphen (-), the wildcard matches any character that falls between those two characters, inclusive. To specify the hyphen (-) as a character, specify it at the beginning or end of the character string. For example specifications, see Table 5-8 Specification examples using the square brackets wildcard. |
{str,...} |
Expands the str strings delimited by the comma by using brace expansion. This expansion is not performed in the following cases:
|
The following table shows example specifications of the square brackets wildcard ([]).
Specification example |
Description |
---|---|
[]a] |
Matches the character string ]a. |
[!abc] |
Matches any character string other than abc. |
[0-9] |
Matches one of the numbers from 0 through 9. |
[a-z] |
Matches any lowercase letter. |
[A-Z] |
Matches any uppercase letter. |
[0-9a-zA-Z] |
Matches any alphanumeric character. |
[-abc] |
Matches the character string -abc. |
[!-abc] |
Matches any character string other than -abc. |
(6) Substitution
The following three substitution functions are available:
-
Variable substitution
Replaces a variable's status, the length (in bytes) of a character string constituting the value of a variable, the number of elements in an array variable, or the value of a variable expanded by matching the variable value and a specified pattern.
-
Command substitution
Treats a command's standard output as a variable value.
-
File name substitution
Expands a file name that matches a specified condition by using wildcard characters, such as * and ?. Note that if the noglob shell option is enabled, file names are not replaced. For details about the noglob shell option, see 5.6.1 Shell options that can be specified with the set command.
(a) Variable substitution
Variable substitution includes substitution of a variable based on the status of the variable, substitution of a variable for the length of a character string constituting the value of a variable or for the number of elements in an array, substitution of a variable based on the result of pattern matching, and substring expansion.
-
Variable substitution depending on the status of the variable
The table below lists and describes the formats used to perform variable substitution depending on the status of the variable. In these formats, variable represents a variable name and word represents the variable that is expanded according to the status of variable. In the examples and results, a indicates an undefined variable, b=NULL, and c=1.
Table 5‒9: Formats used to perform variable substitution depending on the status of the variable Format
Description
Examples
Results
${variable:-word}
If variable is defined as a variable and a value is assigned to it, the value of variable is returned. If variable is defined but its value is NULL or undefined, the expansion result of word is returned. The value of variable remains unchanged.
cnt=${a:-7}
7 is assigned to cnt.
cnt=${b:-8}
8 is assigned to cnt.
cnt=${c:-9}
Value of c is assigned to cnt.
${variable-word}
If variable is defined as a variable and a value is assigned to it, the value of variable is returned. If variable is defined and its value is NULL, the value of variable (NULL) is returned. If variable is undefined, the expansion result of word is returned. The value of variable remains unchanged.
cnt=${a-7}
7 is assigned to cnt.
cnt=${b-8}
NULL is assigned to cnt.
cnt=${c-9}
Value of c is assigned to cnt.
${variable:=word}
If variable is defined as a variable and a value is assigned to it, the value of variable is returned. If variable is defined and its value is NULL or undefined, the expansion result of word is assigned to variable, and then the value of variable is returned. Note that this format is applicable only to variables, not to positional parameters.
cnt=${a:=7}
7 is assigned to a and the value of a is assigned to cnt.
cnt=${b:=8}
8 is assigned to b and the value of b is assigned to cnt.
cnt=${c:=9}
Value of c is assigned to cnt.
${variable=word}
If variable is defined as a variable and a value is assigned to it, the value of variable is returned. If variable is defined and its value is NULL, the value of variable (NULL) is returned. If variable is undefined, the expansion result of word is assigned to variable, and then the value of variable is returned. Note that this format is applicable only to variables, not to positional parameters.
cnt=${a=7}
7 is assigned to cnt.
cnt=${b=8}
NULL is assigned to cnt.
cnt=${c=9}
Value of c is assigned to cnt.
${variable:?[word]}
If variable is defined as a variable and a value is assigned to it, the value of variable is returned.
If word is specified and variable is defined and its value is NULL or undefined, the expansion result of word is output to the standard error output, and then the job definition script is terminated.
If word is omitted and variable is defined and its value is NULL or undefined, the KNAX6050-E message indicating that variable is undefined is issued, and then the job definition script is terminated.
The value of variable remains unchanged.
cnt=${a:?7}
The expansion result is output to the standard error output, and then the shell is terminated.
cnt=${a:?}
A message is output, and then the shell is terminated.
cnt=${b:?8}
The expansion result is output to the standard error output, and then the shell is terminated.
cnt=${c:?9}
Value of c is assigned to cnt.
${variable?[word]}
If variable is defined as a variable and a value is assigned to it, the value of variable is returned.
If word is specified and variable is undefined, the expansion result of word is output to the standard error output, and then the job definition script is terminated.
If word is omitted and variable is undefined, the KNAX6050-E message indicating that variable is undefined is issued, and then the job definition script is terminated.
The value of variable remains unchanged.
cnt=${a?7}
The expansion result is output to the standard error output, and then the shell is terminated.
cnt=${a?}
A message is output, and then the shell is terminated.
cnt=${b?8}
NULL is assigned to cnt
cnt=${c?9}
Value of c is assigned to cnt. Value of c is assigned to cnt.
${variable:+word}
If variable is defined as a variable and a value is assigned to it, the expansion result of word is returned. Otherwise, NULL is returned. The value of variable remains unchanged.
cnt=${a:+7}
NULL is assigned to cnt.
cnt=${b:+8}
NULL is assigned to cnt.
cnt=${c:+9}
9 is assigned to cnt.
${variable+word}
If variable is defined as a variable and a value is assigned to it or the value is NULL, the expansion result of word is returned. Otherwise, NULL is returned. The value of variable remains unchanged.
cnt=${a+7}
NULL is assigned to cnt.
cnt=${b+8}
8 is assigned to cnt.
cnt=${c+9}
9 is assigned to cnt.
-
Variable substitution to the length of the character string constituting the value of the variable or to the number of array elements
The table below lists and describes the formats used to perform variable substitution to the length of the character string constituting the value of the variable or to the number of array elements. In these formats, variable represents a variable name and array represents an array name.
Table 5‒10: Formats used to perform variable substitution to the length of the character string constituting the value of the variable or to the number of array elements Format
Description
${#variable}
If variable is * or @, the variable is replaced with the number of positional parameters. Otherwise, the variable is replaced according to the setting specified for the VAR_SHELL_GETLENGTH environment setting parameter:
-
BYTE
Replaces the length of the value stored in variable with the number of bytes.
-
CHARACTER
Replaces the length of the value stored in variable with the number of characters.
If the VAR_SHELL_GETLENGTH environment setting parameter is not specified, the operation is the same as when BYTE is specified in the VAR_SHELL_GETLENGTH environment setting parameter.
${#array[*]}
The value is replaced with the number of elements of the array specified by array.
${#array[@]}
-
-
Variable substitution based on the result of pattern matching
The table below lists and describes the formats used to perform variable substitution based on the result of pattern matching. In these formats, variable represents a variable name and pattern represents a character string used to perform pattern matching with variable. Wildcard characters can be used in pattern.
Table 5‒11: Formats used to perform variable substitution based on the result of pattern matching Classification
Format
Description
Leading match
${variable#pattern }
If pattern matches the leading part of the variable value, the value in variable is replaced with its value less the shortest matching part (the shortest-matching part is deleted). Otherwise, the variable is replaced with the value of variable.
${variable##pattern }
If pattern matches the leading part of the variable value, the variable is replaced with its value less the longest matching part (the longest-matching part is deleted). Otherwise, the variable is replaced with the value of variable.
Trailing match
${variable%pattern }
If pattern matches the trailing part of the variable value, the variable is replaced with its value less the shortest matching part (the shortest-matching part is deleted). Otherwise, the variable is replaced with the value of variable.
${variable%%pattern }
If pattern matches the trailing part of the variable value, the variable is replaced with its value less the longest matching part (the longest-matching part is deleted). Otherwise, the variable is replaced with the value of variable.
The following shows an example of outputting a variable value with deletion of a specified character string (leading match).
Contents of the job definition script:
abc=abcd1234xyz987abcd1234efg echo ${abc#abcd} # 1. echo ${abc#a*2} # 2. echo ${abc##a*2} # 3. echo ${abc#*1234} # 4. echo ${abc##*1234} # 5. echo ${abc#1234} # 6.
STDOUT file contents of the executed job:
******** JOB SCOPE STDOUT ******** 1234xyz987abcd1234efg Result of 1: The leading character string abcd is deleted. 34xyz987abcd1234efg Result of 2: The string of characters from a through 2 is deleted (shortest match). 34efg Result of 3: The string of characters from a through 2 is deleted (longest match). xyz987abcd1234efg Result of 4: The string of characters from the beginning through 1234 is deleted (shortest match). efg Result of 5: The string of characters from the beginning through 1234 is deleted (longest match). abcd1234xyz987abcd1234efg Result of 6:The value of abc is output because there is no leading match.
The following shows an example of outputting a variable value with deletion of a specified character string (trailing match).
Contents of the job definition script:
abc=abcd1234xyz987abcd1234 echo ${abc%1234} # 1. echo ${abc%d*4} # 2. echo ${abc%%d*4} # 3. echo ${abc%34*} # 4. echo ${abc%%34*} # 5. echo ${abc%abcd} # 6.
STDOUT file contents of the executed job:
******** JOB SCOPE STDOUT ******** abcd1234xyz987abcd Result of 1: The trailing character string 1234 is deleted. abcd1234xyz987abc Result of 2: The trailing character string from d through 4 is deleted (shortest match). abc Result of 3: The trailing character string from d through 4 is deleted (longest match). abcd1234xyz987abcd12 Result of 4: The character string beginning with 34 is deleted (shortest match). abcd12 Result of 5: The character string beginning with 34 is deleted (longest match). abcd1234xyz987abcd1234 Result of 6: The value of abc is output because there is no trailing match.
-
Substring expansion
The following table lists and describes the formats for variable substitution that is performed based on the result of substring expansion.
Table 5‒12: Formats for substring expansion Format
Description
${variable:offset}
Retrieves characters from the expansion result of variable. offset specifies the start position of the characters to be retrieved.
${variable:offset:length}
Retrieves as many characters as there are in maximum length from the expansion result of variable. offset specifies the start position of the characters to be retrieved.
${array[*]:offset}
Retrieves elements that begin with ${array[offset]} of the array.
${array[*]:offset:length}
Retrieves as many elements as there are in length that begin with ${array[offset]} of the array.
${array[@]:offset}
Retrieves elements that begin with ${array[offset]} of the array.
${array[@]:offset:length}
Retrieves as many elements as there are in length that begin with ${array[offset]} of the array.
(b) Command substitution
The table below lists and describes the formats used to perform command substitution. In these formats, command represents the name of the command and the arguments to be executed.
In Windows, command substitution is performed in the current process except for external commands.
Format name |
Format |
Description |
---|---|---|
$( ) format |
$(command) |
If the character string of command contains a backslash (\), \ has no special meaning. |
Grave character format |
`command` |
If the character string of command contains \, \ has a special meaning. To specify one command substitution within another, specify \ immediately before the inner grave accent mark character string such as `command \`command\``. |
In the grave character format, a backslash (\) in the character string of command is treated as a metacharacter. Therefore, if the character string of command contains \, the execution results differ between the $( ) and the grave character formats. We recommend that you use the $( ) format.
The following shows example specifications and execution results.
-
$( ) format
Specification example:
echo $(echo '\$x')
Execution result:
\$x
-
Grave character format
Specification example:
echo `echo '\$x'`
Execution result:
$x
(c) File name substitution
The table below lists and describes the formats used to perform file name substitution. In these formats, pattern represents a character string used for pattern matching. Wildcard characters can be used in pattern.
Format |
Description |
Example |
Matching character string |
---|---|---|---|
?(pattern|pattern ...) |
Matches one of the character strings specified as pattern. |
?(h) |
Null character string, h |
*(pattern|pattern ...) |
Matches none or any number of the character strings specified as pattern. |
*(h) |
Null character string, h, h, hh, hhh, ... |
+(pattern|pattern ...) |
Matches at least one of the character strings specified as pattern. |
+(h) |
h, hh, hhh, ... |
@(pattern|pattern ..) |
Matches only one of the character strings specified as pattern. |
@(h) |
h |
!(pattern|pattern ...) |
Matches all but one of the character strings specified as pattern. |
!(h) |
Any character string without h |
You can replace file names by specifying multiple patterns delimited by the vertical bar (|). Do not specify any spaces before or after the vertical bar delimiter. If there is such a space, it will be regarded as part of the pattern.
The following shows examples.
Example of file name substitution
Contents of job definition script:
ls -C # 1. Display the file in the current directory ls -C ?(*.sh|*.exe|*.dot) # 2. Display the file names whose extension is sh,exec, or dot ls -C *(*.sh|*.exe) # 3. Display the file names whose extension is either sh or exe ls -C +(*.jhs|*h) # 4. Display the file names whose extension is jhs or that end with h ls -C @(*.c|*.jhs) # 5. Display the file names whose extension is c or jhs ls -C !(*.c|*.jhs) # 6. Display the file names whose extension is neither c nor jhs
STDOUT file contents of the executed job:
******** JOB SCOPE STDOUT ******** a.jhs a.sh a.txt func.c Execution result of 1 a.sh Execution result of 2 a.sh Execution result of 3 a.jhs a.sh Execution result of 4 a.jhs func.c Execution result of 5 a.sh a.txt Execution result of 6
File names beginning with a dot (.) are excluded as targets of pattern matching. To include file names beginning with a dot as targets of pattern matching, you must specify the dot explicitly.
(7) Arithmetic expansion
Arithmetic expansion involves performing an arithmetic operation and then assigning the result to a variable. The following shows the format and an example of arithmetic expansion.
$((arithmetic-expression))
- Example of arithmetic expansion:
-
- Contents of job definition script:
x=100 # 1. Value 100 is assigned to variable x x=$((x-1)) # 2. Perform the arithmetic operation and then assign the result to x echo $x # 3. Output the value of x to the standard output
- STDOUT file contents of the executed job:
******** JOB SCOPE STDOUT ******** 99 Value 99 is assigned to x.
(8) Input and output redirection
In job definition scripts, you can change the output destination of command execution results and the input source of information needed for command execution before commands are executed. These capabilities are called input and output redirection. This subsection explains input and output redirection as supported by JP1/Advanced Shell.
(a) Redirection
The table below describes the redirection methods supported by JP1/Advanced Shell. A redirection is interpreted from left to right.
Redirection |
Description |
---|---|
> file |
Uses file as the standard output. If file does not exist, the file is created. If file already exists, the existing file is overwritten. |
< file |
Uses file as the standard input. |
command_1 | command_2 |
This is a pipe. It uses the standard output of command_1 as the standard input for command_2. |
>>file |
Uses file as the standard output. If file does not exist, the file is created. If file already exists, new data is added to the existing file. |
>|file |
Uses file as the standard output. If file does not exist, the file is created. If file already exists, the existing file is overwritten. |
<>file |
Opens file as the standard input for read and write operations. |
<<label |
This is a here document. |
n>file |
Redirects the output destination of file descriptor n to file. |
n<file |
Inputs file descriptor n from file. |
>&n |
Copies the standard output to file descriptor n. |
<&n |
Copies the standard input from file descriptor n. |
>&- |
Closes the standard output. |
<&- |
Closes the standard input. |
| |
In UNIX, this direction starts a background process that involves input and output from the parent process. |
>&p#2 |
Redirects the output destination of the background process to the standard output. |
<&p#2 |
Redirects the input of the background process to the standard input. |
(b) File descriptors
The following table lists and describes the file descriptors in JP1/Advanced Shell by type of input and output.
Input or output type |
File descriptor |
Remarks |
---|---|---|
Standard input |
0 |
-- |
Standard output |
1 |
If SPOOL is specified in the -s option of the adshexec command and the OUTPUT_STDOUT parameter in the environment file, the terminal is not opened by this file descriptor because JP1/Advanced Shell uses the standard output for output from the shell to the spool's STDOUT (stdout). |
Standard error |
2 |
The terminal is not opened by this file descriptor because JP1/Advanced Shell uses the standard error output for output from the shell to the spool's STDERR (stderr). |
Other |
3 through 9 |
The user can use these file descriptors in job definition scripts. |
(c) Here document
A here document refers to creation of the standard input within the job definition script. The following table lists and describes the formats related to here documents.
Format |
Description |
---|---|
<< label document label |
The document part enclosed by label and label is passed to the standard input. A variable specified for document is replaced and spaces and tab characters are passed as is to the standard input. |
<<- label document label |
The document part enclosed by label and label is passed to the standard input. A variable specified for document is replaced and spaces are passed as is to the standard input. The tab character at the beginning of each line is deleted. |
<< \label document label |
The document part enclosed by label and label is passed to the standard input. A variable specified for document is not replaced. |
<< 'label' document label |
(d) Pipes
When you want to connect multiple commands and use the standard output from one command as the standard input to another, you can use pipes to connect the commands. A set of commands connected by a pipe is called a pipeline.
-
In Windows
A pipeline is processed from left to right, and the commands in a pipeline are executed sequentially. Among built-in commands, functions, and external commands, only the external commands are executed as separate processes. Temporary files are used to transfer data between commands.
-
In UNIX
A pipeline is processed from left to right, and the commands in a pipeline are executed as separate processes.
You can use the PIPE_CMD_LAST parameter to define whether the last command is to be executed in the current process or in a separate process. The default is the current process.#
For details about the PIPE_CMD_LAST parameter, see PIPE_CMD_LAST parameter (defines execution processing for the last command in a pipe) (UNIX only) in 7. Parameters Specified in the Environment Files.
- #
-
This is not applicable to commands executed in separate processes (such as external commands, child jobs, UNIX-compatible commands, shell operation commands, subshells, commands with a background specified, and commands with a background process specified).
Also in UNIX, if multiple commands are connected by a pipe and then the output command outputs data to the pipe after the input command has terminated and while there is no process, the output command might receive SIGPIPE.
In JP1/Advanced Shell, the command issues the KNAX6522-E message and terminates with an error unless the command handles SIGPIPE.
(9) Command separators
Command separators enable you to specify multiple commands on a single line of a job definition script. The following table lists and describes the formats for command separators.
Format |
Description |
---|---|
command; |
The specification up to the semicolon (;) is interpreted as a command and its arguments. |
command_10&&0command_2 |
This is the AND control operator. If the left-hand command terminates with return code 0, the right-hand command is executed. |
command_10||0command_2 |
This is the OR control operator. If the left-hand command terminates with any non-zero return code, the right-hand command is executed. |
The command separators can be used to handle and execute a group of commands as a single command group.
(10) Grouping commands
You can execute multiple commands in a batch by grouping them. You can also group multiple command groups. The following table lists and describes the formats for grouping commands.
Format |
Description |
---|---|
(command _1;0command _2; ... ) |
Executes the grouped commands in a subshell (child process).# If the environment is changed during command execution, the change is not inherited by the current shell. Delimit the commands in the group with the semicolon or end-of-line character. |
(command_1 (end-of-line) command_2 (end-of-line) ... ) |
|
{1command_1;0command _2; ...;} |
Executes the grouped commands in the current shell. If the environment is changed during command execution, the change is inherited after the commands have terminated. Delimit the commands in the group with the semicolon or end-of-line character. In this format, you must insert at least one space after the {, and you must insert a semicolon (;) or end-of-line character after the last command. |
{1command_1 (end-of-line) command_2 (end-of-line) ...(end-of-line) } |
(11) Other metacharacters
The following table lists and describes the other metacharacters that are supported.
Metacharacter |
Description |
---|---|
~#1 |
Replaced with the HOME shell variable.#2 |
~any-character-string |
(UNIX only) Checks whether the user name matching the character string up to / or an argument delimiter is registered in the /etc/passwd file. If the user name is registered, this metacharacter is replaced with the corresponding user's login directory. If the user name is not registered, the specified character string is interpreted as a character string as is. |
~+#1 |
Replaced with the PWD shell variable. |
~-#1 |
Replaced with the OLDPWD shell variable. |
 |
Executes the job definition script or function in the background. |