Hitachi

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


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. In addition, you can create a two-dimensional array that can hold a maximum of 65,536 x 256 elements with arrays consisting of 2 element numbers. If no element is specified, no array is set.

Organization of this subsection

(1) Creating arrays

The following explains how to create an array.

(2) Creating arrays by using array-name=(value value ...)

The one-dimensional array that is defined in the syntax of "syntax name=(value value ...)" is registered in the syntax of "set -A array-name value value ...".

The two-dimensional array that is defined in the syntax of "array-name[]=({ value value ... } { value value ... } ...)" is registered in the syntax of "set -D syntax-name { value value ... } { value value ... }".

Execution of the "set command" is output to the JOBLOG instead of the syntax of "array-name=(value value ...)" and "array-name []=({ valuevalue ... } { value value ... } ...)".

How to manage the array element is the same as other arrays even if the array is created in the syntax of "array-name=(value value ...)". For example, the one-dimensional array is the same array as the array that is created in the format of "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

n#

Elements

0

x1

1

x2

2

x3

3

x4

4

x5

#:

n indicates two-dimensional array element.

For the two-dimensional array, how to manage the array element is the same as that of the array that is created by using the set-D command if the array is created in the syntax of "array-name[]=({ value value ... } { valuevalue ... } ...)". For example, the two-dimensional array is the same array as the array that is created in the format of "set -D ARRAY { x1 x2 x3 } { x4 x5 x6 }".

Element of array if defined as ARRAY[]=({ x1 x2 x3 } { x4 x5 x6 })
ARRAY[0][0]=x1
ARRAY[0][1]=x2
ARRAY[0][2]=x3
ARRAY[1][0]=x4
ARRAY[1][1]=x5
ARRAY[1][2]=x6

n#1

m#2

0

1

2

0

x1

x2

x3

1

x4

x5

x6

#1:

n indicates two-dimensional array element.

#2:

m indicates two-dimensional array element.

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:

  • Set an array with three elements (x1 x2 x3) for the one-dimensional array SEQ1.

    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=046188
    ---------------------------------------------------------------
     Advanced Shell 11-00
    
     [Information]
       Job ID          : 046188
       Spool directory : /var/opt/jp1as/spool/046188/
       Date            : 2015/10/29
       EnvFile(system) : 
       EnvFile(job)    : 
       Host name       : host01
     [Environment variable from Automatic Job Management System]
    ---------------------------------------------------------------
    ********  JOB CONTROLLER MESSAGE  ********
    14:52:48 046188 KNAX0091-I ADSH046188 The job started.
    14:52:48 046188 KNAX7901-I The job controller will wait for all asynchronous processes at the end of the job.
    14:52:48 046188 KNAX7902-I The job controller will run in tty stdin mode.
    14:52:48 046188 KNAX6112-I Execution of the command set (line=1) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    14:52:48 046188 KNAX6112-I Execution of the command echo (line=2) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    14:52:48 046188 KNAX0098-I ADSH046188 The job ended. exit status=0 execution time=0.001s CPU time=0.000s
    
    ********   Script IMAGE    ********
    
    ***** /home/user001/SAMPLE_JOB6.ash *****
    0001 : SEQ1=(x1 x2 x3)
    0002 : echo ${SEQ1[@]}
    0003 : 
    
    ***** CONVERSION INFORMATION *****
    
    ********   JOB SCOPE STDERR    ********
    KNAX0098-I ADSH046188 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/046188-ADSH046188/"
    KNAX7999-I Advanced Shell ended. exit status=0
    
    ********   JOB SCOPE STDOUT    ********
    x1 x2 x3
  • Set an array with 3 x 2 elements ({ x1 x2 x3 } { x4 x5 x6 }) for the two-dimensional array SEQ2.

    SEQ2[]=({ x1 x2 x3 }{ x4 x5 x6 })
    echo ${SEQ2[@]}

    --> "x1 x2 x3 x4 x5 x6" is output to the standard output.

    The following is an example of the output of JOBLOG using the array SEQ2:

    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=046190
    ---------------------------------------------------------------
     Advanced Shell 11-00
    
     [Information]
       Job ID          : 046190
       Spool directory : /var/opt/jp1as/spool/046190/
       Date            : 2015/10/29
       EnvFile(system) : 
       EnvFile(job)    : 
       Host name       : host01
     [Environment variable from Automatic Job Management System]
    ---------------------------------------------------------------
    ********  JOB CONTROLLER MESSAGE  ********
    15:01:51 046190 KNAX0091-I ADSH046190 The job started.
    15:01:51 046190 KNAX7901-I The job controller will wait for all asynchronous processes at the end of the job.
    15:01:51 046190 KNAX7902-I The job controller will run in tty stdin mode.
    15:01:51 046190 KNAX6112-I Execution of the command set (line=1) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:01:51 046190 KNAX6112-I Execution of the command echo (line=2) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:01:51 046190 KNAX0098-I ADSH046190 The job ended. exit status=0 execution time=0.001s CPU time=0.000s
    
    ********   Script IMAGE    ********
    
    ***** /home/user001/SAMPLE_JOB7.ash *****
    0001 : SEQ2[]=({ x1 x2 x3 } { x4 x5 x6 })
    0002 : echo ${SEQ2[@]}
    
    ***** CONVERSION INFORMATION *****
    
    ********   JOB SCOPE STDERR    ********
    KNAX0098-I ADSH046190 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/046190-ADSH046190/"
    KNAX7999-I Advanced Shell ended. exit status=0
    
    ********   JOB SCOPE STDOUT    ********
    x1 x2 x3 x4 x5 x6
  • Define the variables with which the array elements are to be substituted and sets the number of elements of the one-dimensional array SEQ1 to 3, as follows.

    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=046192
    ---------------------------------------------------------------
     Advanced Shell 11-00
    
     [Information]
       Job ID          : 046192
       Spool directory : /var/opt/jp1as/spool/046192/
       Date            : 2015/10/29
       EnvFile(system) : 
       EnvFile(job)    : 
       Host name       : host01
     [Environment variable from Automatic Job Management System]
    ---------------------------------------------------------------
    ********  JOB CONTROLLER MESSAGE  ********
    15:10:25 046192 KNAX0091-I ADSH046192 The job started.
    15:10:25 046192 KNAX7901-I The job controller will wait for all asynchronous processes at the end of the job.
    15:10:25 046192 KNAX7902-I The job controller will run in tty stdin mode.
    15:10:25 046192 KNAX6110-I Execution of the command ARR1=x1 (line=1) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:10:25 046192 KNAX6110-I Execution of the command ARR2=x2 (line=2) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:10:25 046192 KNAX6110-I Execution of the command ARR3=x3 (line=3) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:10:25 046192 KNAX6112-I Execution of the command set (line=4) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:10:25 046192 KNAX6112-I Execution of the command echo (line=5) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:10:25 046192 KNAX0098-I ADSH046192 The job ended. exit status=0 execution time=0.002s CPU time=0.000s
    
    ********   Script IMAGE    ********
    
    ***** /home/user001/SAMPLE_JOB8.ash *****
    0001 : ARR1=x1
    0002 : ARR2=x2
    0003 : ARR3=x3
    0004 : SEQ1=($ARR1 $ARR2 $ARR3)
    0005 : echo ${SEQ1[@]}
    
    ***** CONVERSION INFORMATION *****
    
    ********   JOB SCOPE STDERR    ********
    KNAX0098-I ADSH046192 The job ended. exit status=0 execution time=0.002s 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/046192-ADSH046192/"
    KNAX7999-I Advanced Shell ended. exit status=0
    
    ********   JOB SCOPE STDOUT    ********
    x1 x2 x3
  • Define the variables with which the array elements are to be substituted and set the number of elements of the two-dimensional array SEQ2 to 3 x 2, as follows.

    ARR1=x1
    ARR2=x2
    ARR3=x3
    ARR4=x4
    ARR5=x5
    ARR6=x6
    SEQ2[]=({ $ARR1 $ARR2 $ARR3 } { $ARR4 $ARR5 $ARR6 })
    echo ${SEQ2[@]}

    --> "x1 x2 x3 x4 x5 x6" is output to the standard output.

    The following is an example of the output of JOBLOG using the array SEQ2:

    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=046194
    ---------------------------------------------------------------
     Advanced Shell 11-00
    
     [Information]
       Job ID          : 046194
       Spool directory : /var/opt/jp1as/spool/046194/
       Date            : 2015/10/29
       EnvFile(system) : 
       EnvFile(job)    : 
       Host name       : host01
     [Environment variable from Automatic Job Management System]
    ---------------------------------------------------------------
    ********  JOB CONTROLLER MESSAGE  ********
    15:18:07 046194 KNAX0091-I ADSH046194 The job started.
    15:18:07 046194 KNAX7901-I The job controller will wait for all asynchronous processes at the end of the job.
    15:18:07 046194 KNAX7902-I The job controller will run in tty stdin mode.
    15:18:07 046194 KNAX6110-I Execution of the command ARR1=x1 (line=1) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:18:07 046194 KNAX6110-I Execution of the command ARR2=x2 (line=2) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:18:07 046194 KNAX6110-I Execution of the command ARR3=x3 (line=3) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:18:07 046194 KNAX6110-I Execution of the command ARR4=x4 (line=4) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:18:07 046194 KNAX6110-I Execution of the command ARR5=x5 (line=5) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:18:07 046194 KNAX6110-I Execution of the command ARR6=x6 (line=6) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:18:07 046194 KNAX6112-I Execution of the command set (line=7) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:18:07 046194 KNAX6112-I Execution of the command echo (line=8) finished successfully. exit status=0 execution time=0.000s CPU time=0.000s
    15:18:07 046194 KNAX0098-I ADSH046194 The job ended. exit status=0 execution time=0.003s CPU time=0.000s
    
    ********   Script IMAGE    ********
    
    ***** /home/user001/SAMPLE_JOB9.ash *****
    0001 : ARR1=x1
    0002 : ARR2=x2
    0003 : ARR3=x3
    0004 : ARR4=x4
    0005 : ARR5=x5
    0006 : ARR6=x6
    0007 : SEQ2[]=({ $ARR1 $ARR2 $ARR3 } { $ARR4 $ARR5 $ARR6 })
    0008 : echo ${SEQ2[@]}
    
    ***** CONVERSION INFORMATION *****
    
    ********   JOB SCOPE STDERR    ********
    KNAX0098-I ADSH046194 The job ended. exit status=0 execution time=0.003s 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/046194-ADSH046194/"
    KNAX7999-I Advanced Shell ended. exit status=0
    
    ********   JOB SCOPE STDOUT    ********
    x1 x2 x3 x4 x5 x6

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

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

(4) Regarding the necessary amount of memory for securing a large amount of an element of the array

You can create a maximum of 65,536x64 elements of array in a two-dimensional array.

The maximum number of elements of array may not be ensured depending on the amount of available memory that is granted to the process that creates the two-dimensional array.

The formula for estimating the memory amount that is required for creating a two-dimensional array is as follows:

(324+x) × y
x: Average array element data (specified by rounding up the value in units of multiples of 8)
y: Maximum number of array elements to be created (1 to 65,536×64).

Confirm that the total of the aforementioned necessary memory amount and required amount of memory that is used by other syntax does not exceed the memory amount that is available in the process.