Search code examples
arraysbashdrop-down-menuapache2cgi-bin

Bash Drop Down Menu from contents of Array (CGI-bin/apache2)


Possible Duplicate:
Output of command in Bash script to Drop-down box?

I have an array in a bash script. I have apache2 configured so I can run cgi/bash scripts in my cgi-bin directory, and they will display like a webpage.

Is there any way to take each element of the array and make it one result in a drop down box on the page?

For example, if my array looked like:

a[0] = "255" 
a[1] = "254" 
a[2] = "253"
a[3] = "252"
a[4] = "251"

I would want the dropbox to look like (lol, ASCII)

 _____
|255  |
|254  |
|253  |
|252  |
|251  |
 -----

Any suggestions? Thanks for your help.


Solution

  • you can write a bash for loop to do everything. This just prints out the elements:

    for i in `seq 1 "${#x[*]}"`; do
        echo "|${x[i]} |"
    done
    

    To get the alignment correct, you need to figure out the max length (one loop) and then print out the terms:

    # w will be the length
    w=0
    for i in `seq 1 "${#x[*]}"`; do
        if [ $w -lt ${#x[$i]} ]; then w=${#x[$i]}; fi
    done
    for i in `seq 1 $((w+2))`; do printf "%s" "-"; done
    printf "\n"
    for i in `seq 1 "${#x[*]}"`; do
        printf "|%-$ws |\n" ${#x[$i]}
    done
    for i in `seq 1 $((w+2))`; do printf "%s" "-"; done
    printf "\n"