Re: UNIX arrays (215 Views)
Reply
Occasional Advisor
Pat Peter
Posts: 23
Registered: ‎02-04-2005
Message 1 of 5 (215 Views)

UNIX arrays

Hi,

I have written a script which has a line called "set -A array". When I execute it with
ksh <script name> it is executing fine. But the same when I tried without "ksh" it is giving an error called "Array parameter not set".

I have used "#!/usr/bin/ksh -eu" in the first line of the script but still it gives the error.

Can anyone please help.

Thanks,
Pat
Honored Contributor
David Child_1
Posts: 394
Registered: ‎01-10-2001
Message 2 of 5 (215 Views)

Re: UNIX arrays

Have you tried changing the first line to:
#!/usr/bin/ksh
or
#!/usr/bin/ksh -e

I'm not sure without testing how the -u set effects this, but its worth a try:

-u Treat unset parameters as an error when substituting.

David
Respected Contributor
Paul Cross_1
Posts: 288
Registered: ‎03-18-2002
Message 3 of 5 (215 Views)

Re: UNIX arrays

Make sure that the ksh you call on the command line is the same as the one you use in your script.

IE:
> which ksh
should be /usr/bin/ksh.

You could also run the script with -x to see what it is doing.
Occasional Advisor
Pat Peter
Posts: 23
Registered: ‎02-04-2005
Message 4 of 5 (215 Views)

Re: UNIX arrays

Hi,

I don't get the error when I remove the option 'u' from #!/usr/bin/ksh -e.

Can you please let me know why this happens.

Thanks,
Pat
Honored Contributor
Bill Hassell
Posts: 14,218
Registered: ‎05-29-2000
Message 5 of 5 (215 Views)

Re: UNIX arrays

As mentioned, set -u or the command line means that an undefined variable has been used someplace in your script. If your exact command list is:

#!/usr/bin/ksh
set -A array
echo ${array[0]}

then you will indeed get an error that element 0 is not set. Since array has not been assigned anything, set -u treats this as an error (a very good idea by the way) and stops the script. Now if the exact error message is: Array parameter not set" than you probably misspelled the array's name. NOTE: "Array" is not the same as "array". The following will run very differently if you type set -u before running it, and then type set +u and run it again:

#!/usr/bin/ksh
set -A array
echo $array
echo ${array[1]}
echo $Array

All 3 echo lines have arrors: the first is trying to echo an undefined array, the second is trying to show an undefined element (none have been defined) and the last line is a spelling error. So the -u option is flagging your script with an error message. If you leave set -u out of your script. your login shell probably has set +u in force so undefined variable are treated as null values, which is never a good idea. Always code set -u at the beginning of your scripts:

#!/usr/bin/ksh
set -u
set -A array
echo $array
echo ${array[1]}
echo $Array
The opinions expressed above are the personal opinions of the authors, not of HP. By using this site, you accept the Terms of Use and Rules of Participation.