Use of arrays in korn shell?

Use of arrays in korn shell?

Post by Glen Stewa » Sat, 02 Apr 1994 14:03:29



I saw the recent post by one gentleman who was searching for a way to display
menus 'on the fly'.  I too have been trying some methods to do this, and have
been struggling with the use of arrays.  I have a small program that reads in
the contents of a directory, and assigns sub-dir names to elements of an array.
 Unfortunately, I'm not understanding how to print the individual array values
once they're assigned.  A helpful reply with one line of info will surely be
all I need.  Thanks in advance!

... Testify 3.5: Buy into the firm foundation - Jesus!

 
 
 

Use of arrays in korn shell?

Post by Otto Li » Sun, 03 Apr 1994 15:36:36



> I saw the recent post by one gentleman who was searching for a way to
> display menus 'on the fly'. I too have been trying some methods to do this,
> and have been struggling with the use of arrays. I have a small program
> that reads in the contents of a directory, and assigns sub-dir names to
> elements of an array. Unfortunately, I'm not understanding how to print the
> individual array values once they're assigned. A helpful reply with one
> line of info will surely be all I need. Thanks in advance!

Well....

This isn't a one line reply, but I thought that I would post a simple script
which demonstrates a variety of ways to reference arrays. The script allows
users to traverse directories and display files using menus. Search for
"Method" to see the different ways.

----- snip -----
#!/bin/ksh

DoLs()
{
        integer diridx=0
        integer fileidx=0
        unset dir_list
        unset file_list

        # use ls -a to show .files, may need to be changed for a
        # particular system
        for dir in `/bin/ls -a`
        do
                if test -d $dir -a $dir != "."
                then
                        dir_list[diridx]=$dir
                        let diridx=$diridx+1
                elif test -f $dir
                then
                        file_list[fileidx]=$dir
                        let fileidx=$fileidx+1
                fi
        done

Quote:}

Cd()
{
        case $1 in
                [1-9]|[1-9][0-9])       # cd n
                        # Method 1, access array via index (need to
                        # subtract 1 from the select reply)
                        dir=${dir_list[$1-1]}
                        ;;
                *)
                        dir=$1
                        ;;
        esac

        cd $dir

Quote:}

DoCd()
{
        PS3='
Select directory by number or enter a name: '

        echo
        # Method 2, use select to display array and get user input

        do
                if Cd $REPLY
                then
                        return
                fi
        done

Quote:}

DisplayFiles()
{
        echo
        echo Files:
        # Method 3, traverse array using for loop

        do
                echo "    $file"
        done
        # Alternately, we could use method 2

        # do :
        # done < /dev/null

Quote:}

menu[0]="Display Files"
menu[1]="Change Directory"
menu[2]="Exit"

DoMenu()
{
        echo
        PS3='
Enter number of action to take: '

        echo
        # Method 2, use select to display array and get user input
        # IMHO, using select really helps write menu'd scripts.

        do
                case $REPLY in
                    1)
                        DoLs
                        DisplayFiles
                        return
                        ;;
                    2)
                        DoLs
                        DoCd
                        return
                        ;;
                    3)
                        exit 0
                        ;;
                    *)
                        echo Illegal entry
                        ;;
                esac
        done

Quote:}

while test 1
do
        DoMenu
done
----- snip -----

Hope this helps,

Otto

--
Otto Lind                  Coactive Aesthetics

netcom!coactive!otto       voice:(415)626-5152    fax:(415)626-6320