Do arrays work in the posix shell? (733 Views)
Reply
Regular Advisor
Trever Furnish
Posts: 191
Registered: ‎08-28-2001
Message 1 of 6 (733 Views)
Accepted Solution

Do arrays work in the posix shell?

Trying to use arrays in the hpux posix shell and either I'm too dumb to interpret the manual page, or the shell's just broken in this respect.

Tell me I'm dumb! :-)

Could someone provide a working example of how to assign three values to one array variable and then reference them?
Hockey PUX?
Please use plain text.
Acclaimed Contributor
James R. Ferguson
Posts: 21,184
Registered: ‎07-06-2000
Message 2 of 6 (733 Views)

Re: Do arrays work in the posix shell?

Hi Trever:

Try this:

# cat .sharray
#!/usr/bin/sh
set -A ARY this works for you
echo ${ARY[0]}
echo ${ARY[1]}
exit 0

# ./sharray
this
works

...Regards!

...JRF...
Please use plain text.
Regular Advisor
Trever Furnish
Posts: 191
Registered: ‎08-28-2001
Message 3 of 6 (733 Views)

Re: Do arrays work in the posix shell?

Thanks, James.

You definitely did exactly what I asked, and now having seen that I realize I was very close in my own attempts, but I had forgotten the numbering started at 0, so when I was assigning using "set" and then referencing ${a[1]}, the shell was telling the truth when it claimed the variable wasn't set. D'OH! :-)
Hockey PUX?
Please use plain text.
Honored Contributor
Bill Hassell
Posts: 14,205
Registered: ‎05-29-2000
Message 4 of 6 (733 Views)

Re: Do arrays work in the posix shell?

The set -A directive is quite nice as it can not only declare the array but assign some of all the values you need at one time. And the elements may be strings:

set -A REASON \
"exceeded password lifetime" \
"exceeded last login time" \
"exceeded absolute account lifetime" \
"exceeded unsuccessful login attempts" \
"password required and a null password" \
"administrator lock" \
"password is a * (asterisk)"

You can display all the defined elements with @ like this:

echo "${REASON[@]}"

or show the elements by number:

for N in 0 1 2 3 4 5 6
do
echo "$N = \"${REASON[$N]}\""
done
Please use plain text.
Honored Contributor
Peter Nikitka
Posts: 1,575
Registered: ‎02-10-2003
Message 5 of 6 (733 Views)

Re: Do arrays work in the posix shell?

Hi,

to make Bill's solution a little more general, you can loop through an array without explicitly naming the indices:
Change
>>
for N in 0 1 2 3 4 5 6
do
echo "$N = \"${REASON[$N]}\""
done
<<

To

typeset -i N=-1
while [ N+=1 -lt ${#REASON[*]} ]
do print $N = \"${REASON[N]\"
done

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Please use plain text.
Regular Advisor
Trever Furnish
Posts: 191
Registered: ‎08-28-2001
Message 6 of 6 (733 Views)

Re: Do arrays work in the posix shell?

Thanks, guys. I assigned 5 points each for the additional info, since James had already scored the 10.
Hockey PUX?
Please use plain text.
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