5.1.3 Arrays
In JP1/Advanced Shell, you can create and reference an array as a type of variable.
You can create a one-dimensional array that can hold a maximum of 65,536 elements with element numbers from 0 to 65,535. If no element is specified, no array is set.
- Organization of this subsection
(1) Creating arrays
The following explains how to create an array.
-
Creating multiple elements at one time (using the set command)
set -A array-name value value ... set +A array-name value value ...
Example:
set -A abc 1 2 3 echo ${abc[1]} -->2 is output to the standard output.
This method can create multiple elements at the same time. For details about the set -A command, see set command (sets shell options, creates an array, or displays variable values) in 9.3 Standard shell commands.
-
Creating a single element
array-name[element-number]=value
Example:
abc[0]=1 abc[1]=2 abc[2]=3 echo ${abc[1]}
-->2 is output to the standard output.
This method creates one element at a time. To create multiple elements, perform the step as many times as there are elements to be created. An array with element number 0 is treated in the same manner as variables.
-
Creating multiple elements at the same time (without using the set command)
array-name=(value value ...)
Example:
abc=(1 2 3) echo ${abc[1]}
-->2 is output to the standard output.
This method can create multiple elements at the same time. For details about the creation method, see (2) Creating arrays by using array-name=(value value ...).
(2) Creating arrays by using array-name=(value value ...)
An array defined in the format array-name=(value value ...) is registered in the format set -A array-name value value .... In JOBLOG, array creation is output as if the set command had been executed, not in the format array-name=(value value ...).
Even when arrays are created in the format array-name=(value value ...), array elements are managed in the same manner as for other arrays. For example, an array created with the following definition is the same as the array created by set -A ARRAY x1 x2 x3 x4 x5:
- Array elements for an array defined as ARRAY=(x1 x2 x3 x4 x5)
ARRAY[0]=x1 ARRAY[1]=x2 ARRAY[2]=x3 ARRAY[3]=x4 ARRAY[4]=x5
Therefore, output to JOBLOG, coverage collection, and output of the xtrace shell option all have the same result as when the set command is used to define arrays.
If array-name=() is defined, a shell variable whose name is array-name and value is the null string is created. This is the same as when array-name= is defined.
(a) Examples of array creation
The table below shows examples of creating array elements that contain shell variables by using the following variables:
A=a B=b C=c MA=' a b c' # MB=d
- #
-
The single quotation mark (') is used to indicate a space. It is not part of the actual variable value.
Table 5‒3: Example of array element creation Array definition
Array elements that are created
Number of array elements created
(a b c)
[0]=a [1]=b [2]=c
3
($A $B $C)
[0]=a [1]=b [2]=c
3
(${A}${B}${C})
[0]=a [1]=b [2]=c
3
($A $B `echo 1`)
[0]=a [1]=b [2]=1
3
($A$B $C)
[0]=ab [1]=c
2
(${A}xyz ${B}stu)
[0]=axyx [1]=bstu
2
($MA $MB)
[0]=a [1]=b [2]=c [3]=d
4
($MA$MB)
[0]=a [1]=b [1]=cd
3
(b) Example of JOBLOG output when arrays are used
Examples of array definition and the resulting JOBLOG output are shown in the following:
-
Specifying 3 as the number of arrays SEQ1 and (x1 x2 x3) as the number of elements
SEQ1=(x1 x2 x3) echo ${SEQ1[@]}
-->x1 x2 x3 is output to the standard output.
The following shows an example of the JOBLOG output when array SEQ1 is used:
KNAX7901-I The job controller will wait for all asynchronous processes at the end of the job. KNAX0724-I The job ID was assigned. job ID=000053 --------------------------------------------------------------- Advanced Shell 10-50 [Information] Job ID : 000053 Spool directory : /var/opt/jp1as/spool/000053/ Date : 2014/02/05 EnvFile(system) : EnvFile(job) : /opt/jp1as/conf/adsh.conf Host name : vm002149 [Environment variable from Automatic Job Management System] --------------------------------------------------------------- ******** JOB CONTROLLER MESSAGE ******** 22:17:21 000053 KNAX0091-I ADSH000053 The job started. 22:17:21 000053 KNAX7901-I The job controller will wait for all asynchronous processes at the end of the job. 22:17:21 000053 KNAX7902-I The job controller will run in tty stdin mode. 22:17:21 000053 KNAX6110-I Execution of the command SEQ1[0]=x1 (line=1) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:17:21 000053 KNAX6110-I Execution of the command SEQ1[1]=x2 (line=1) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:17:21 000053 KNAX6110-I Execution of the command SEQ1[2]=x3 (line=1) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:17:21 000053 KNAX6112-I Execution of the command echo (line=2) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:17:21 000053 KNAX0098-I ADSH000053 The job ended. exit status=0 execution time=0.001s CPU time=0.000s ******** Script IMAGE ******** ***** /home/jp1asuser1/shell/A.sh ***** 0001 : SEQ1=(x1 x2 x3) 0002 : echo ${SEQ1[@]} ***** CONVERSION INFORMATION ***** ******** JOB SCOPE STDERR ******** KNAX0098-I ADSH000053 The job ended. exit status=0 execution time=0.001s CPU time=0.000s ******** JOBSTEP OUTPUT ******** KNAX6380-I A job name will be added to the spool job directory of the root job. spool job directory="/var/opt/jp1as/spool/000053-ADSH000053/" KNAX7999-I Advanced Shell ended. exit status=0 ******** JOB SCOPE STDOUT ******** x1 x2 x3
-
Defining variable names to which array elements are assigned as shown below, and specifying 3 as the number of arrays SEQ1
ARR1=x1 ARR2=x2 ARR3=x3 SEQ1=($ARR1 $ARR2 $ARR3) echo ${SEQ1[@]}
-->x1 x2 x3 is output to the standard output.
The following shows an example of the JOBLOG output when array SEQ1 is used:
KNAX7901-I The job controller will wait for all asynchronous processes at the end of the job. KNAX0724-I The job ID was assigned. job ID=000051 --------------------------------------------------------------- Advanced Shell 10-50 [Information] Job ID : 000051 Spool directory : /var/opt/jp1as/spool/000051/ Date : 2014/02/05 EnvFile(system) : EnvFile(job) : /opt/jp1as/conf/adsh.conf Host name : vm002149 [Environment variable from Automatic Job Management System] --------------------------------------------------------------- ******** JOB CONTROLLER MESSAGE ******** 22:10:35 000051 KNAX0091-I ADSH000051 The job started. 22:10:35 000051 KNAX7901-I The job controller will wait for all asynchronous processes at the end of the job. 22:10:35 000051 KNAX7902-I The job controller will run in tty stdin mode. 22:10:35 000051 KNAX6110-I Execution of the command ARR1=x1 (line=1) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:10:35 000051 KNAX6110-I Execution of the command ARR2=x2 (line=2) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:10:35 000051 KNAX6110-I Execution of the command ARR3=x3 (line=3) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:10:35 000051 KNAX6110-I Execution of the command SEQ1[0]=x1 (line=4) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:10:35 000051 KNAX6110-I Execution of the command SEQ1[1]=x2 (line=4) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:10:35 000051 KNAX6110-I Execution of the command SEQ1[2]=x3 (line=4) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:10:35 000051 KNAX6112-I Execution of the command echo (line=5) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s 22:10:35 000051 KNAX0098-I ADSH000051 The job ended. exit status=0 execution time=0.001s CPU time=0.000s ******** Script IMAGE ******** ***** /home/jp1asuser1/shell/B.sh ***** 0001 : ARR1=x1 0002 : ARR2=x2 0003 : ARR3=x3 0004 : SEQ1=(${ARR1} ${ARR2} ${ARR3}) 0005 : echo ${SEQ1[@]} ***** CONVERSION INFORMATION ***** ******** JOB SCOPE STDERR ******** KNAX0098-I ADSH000051 The job ended. exit status=0 execution time=0.001s CPU time=0.000s ******** JOBSTEP OUTPUT ******** KNAX6380-I A job name will be added to the spool job directory of the root job. spool job directory="/var/opt/jp1as/spool/000051/000051-ADSH000051/" KNAX7999-I Advanced Shell ended. exit status=0 ******** JOB SCOPE STDOUT ******** x1 x2 x3
(c) Notes
A maximum of 8,192 bytes can be specified per line. If an array whose array element numbers have been extended is defined and the maximum number of arrays are specified all on one command line, an error will result. If the specification exceeds 8,192 bytes, use the continuation line specification (\) to continue specification onto the next line so that no line exceeds 8,192 bytes.
Definition examples that use the continuation line specification (\) are shown in the following.
- Example of definition using the set command
set -A ARRAY x0\ x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 ... x1000 \ x1001 x1002 x1003 x1004 x1005 x1006 x1007 x1008 x1009 x1010 x1011 ... x2000 \ : x65001 x65002 x65003 x65004 x65005 x65006 x65007 x65008 x65009 ... x65535
- Example of definition using an assignment expression
ARRAY=( x0\ x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 ... x1000 \ x1001 x1002 x1003 x1004 x1005 x1006 x1007 x1008 x1009 x1010 x1011 ... x2000 \ : x65001 x65002 x65003 x65004 x65005 x65006 x65007 x65008 x65009 ... x65535)
(3) Referencing the values of arrays
The following explains how to reference the values in an array.
-
Referencing the value of element 1 of an array
${array-name[element-number]}
- Example:
-
set -A abc 1 2 3
echo ${abc[1]}
--> 2 is output to the standard output.
-
Referencing the values of all elements of an array
There are four ways to reference the values of all elements of an array:
- Referencing method 1:
-
${array-name[*]}
- Example:
-
set -A abc 1 2 3
echo ${abc[*]}
--> 1 2 3 is output to the standard output.
- Referencing method 2:
-
${array-name[@]}
- Example:
-
set -A abc 1 2 3
echo ${abc[@]}
--> 1 2 3 is output to the standard output.
- Referencing method 3:
-
"${array-name[*]}"
Note: When this method is used, each value is separated by the value of the IFS shell variable.
- Example:
-
set -A abc 1 2 3
IFS=:
echo "${abc[*]}"
--> 1:2:3 is output to the standard output.
- Referencing method 4:
-
"${array-name[@]}"
- Example:
-
set -A abc 1 2 3
echo "${abc[@]}"
--> 1 2 3 is output to the standard output.
The following shows an example of referencing the values of an array:
- Contents of job definition script
set -A myArray a01 a02 a03 # Define myArray as an array for myElement in ${myArray[*]} # Expand all elements of myArray to wordlists in the for statement do echo $myElement done
- Results output to the standard output
a01 a02 a03