Bash Drop Down Menu from contents of Array (CGI-bin/apache2) [duplicate]
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 m开发者_高级运维y 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.
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"
Slightly simpler (but more verbose) solution:
box()
{
local -a a=(1 20 300 4000 50000)
local -i w=0
for i in "${a[@]}"
do
[ $w -lt $i ] && w="${#i}"
done
printf -- '-%.s' $(seq $w)
printf -- '---\n'
for i in "${a[@]}"
do
printf -- '|%-'$w's |\n' $i
done
printf -- '-%.s' $(seq $w)
printf -- '---\n'
}
精彩评论