1752565 Members
5877 Online
108788 Solutions
New Discussion юеВ

Re: UNIX arrays

 
Pat Peter
Occasional Advisor

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
4 REPLIES 4
David Child_1
Honored Contributor

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
Paul Cross_1
Respected Contributor

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.
Pat Peter
Occasional Advisor

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
Bill Hassell
Honored Contributor

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


Bill Hassell, sysadmin