Implementing arrays in sh

Implementing arrays in sh

Post by Jim Harki » Sat, 09 Apr 1994 07:33:49



I could really use arrays in the bourne shell.  In order to fake it I'm
trying to tack the subscript onto a common name, that is:

foo0="first"
foo1="second"
foo2="third"

for i in 0 1 2
do
        name = foo${i}
        echo $name <and the value of the name>
done

This correctly sets name to foo0, foo1, and foo2 but damned if I can get
"first", "second", "third" out of the thing with any combination of ', ", \,
{}, or $.

My real program has many more than 3 foos, and there are going to be about
20 lines of processing inside the for loop.  There are also 3 'arrays',
that is, I've got foo[100], bar[100], and blatz[100].  

It's not possible to change to another shell, write the routine in C, use
perl, or take any other easy outs.  It is possible for me to unroll the
loops, but I'd really like to avoid having 100 blocks of code that differ
only in the variable names.

thanks

jim

--
"I want to die peacefully in my sleep like my grandfather.  Not screaming
in terror like his passengers."


San Diego, CA.

 
 
 

Implementing arrays in sh

Post by Tod McQuill » Sat, 09 Apr 1994 08:28:32




> I could really use arrays in the bourne shell.  In order to fake it I'm
> trying to tack the subscript onto a common name, that is:

> foo0="first"
> foo1="second"
> foo2="third"

> for i in 0 1 2
> do
>    name = foo${i}
>    echo $name <and the value of the name>
> done

Try this:

foo0="first"
foo1="second"
foo2="third"

for i in 0 1 2
do
        name=foo${i}
        eval "value=\$$name"
        echo $name $value
done

 
 
 

Implementing arrays in sh

Post by Part » Sat, 16 Apr 1994 22:43:49



> I could really use arrays in the bourne shell.

There is one (only one) "array" in the Bourne shell, it can be set with
the "set" command, and its members are $1, $2, $3 etc.

Quote:> foo0="first"
> foo1="second"
> foo2="third"
> for i in 0 1 2
> do
>    name = foo${i}
>    echo $name <and the value of the name>
> done

set first second third
for i in $*
do echo $i
done

--

EDV-Zentrum, Universitaet fuer Bodenkultur
Nussdorfer Laende 11                      Please correct me if I'm wrong!
A-1190 Wien, Austria (Europe)             Make love, not flame wars.  :-)

 
 
 

1. implementing semaphores with files (csh,sh)

system: Sun SS2, 4.1.2
shells: sh, csh

what, if any, is the "safest" way to create a lock file to avoid
multiple instances of a script from running ? (either csh or sh)

i realize there are race conditions in the common methods. i'm
interested in which method is considered best.

example:

        if lock_file does not exist
                create file
        else
           exit, script in use
        endif
                .
                .
                .
        done:
           rm file

thanks for any help.

2. Solaris 8 X patches (was: New Solaris/x86 driver download page)

3. useful programs implemented in /bin/sh

4. POP server?

5. Implementing sh in perl

6. "System is too big" error on compiling

7. Execute a sh script under perl and sh: sh script; perl script?

8. puzzling xkbcomp error

9. 2-array iteration on sh??

10. How to use arrays in sh/ksh????

11. arrays in /bin/sh?

12. Array in POSIX-sh

13. arrays in sh