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

2020-05-02 19:01发布

问题:

First off, I appreciate any and all help in answering this question. I have a command in a bash script that will output the following:

255 254 253 252 ... 7 6 5 4 3 2 1 

It is a specific list of numbers, beginning with the largest (which is what I would like), then going to the smallest. The dataset is space-delimited. The output above (except including all numbers), is what you would see if you ran this command in the terminal on a linux machine, or through a bash script.

I have configured my apache2 server to allow for cgi/bash through the cgi-bin directory. When I run this command in a bash file from the web, I get the expected output.

What I'm looking for is for a way to be able to put these numbers each as a separate entry in a drop-down box for selection, meaning the user can select one point of data (254, for example) from the drop down menu.

I'm not sure what I'm doing with this, so any help would be appreciated. I'm not sure if I need to convert the data into an array, or what. The drop down menu can be on the same page of the bash script, but wherever it is, it has to update it's list of numbers from the command every time it is run.

Thank you for your help.

回答1:

I've always found this site useful when fiddling with shell scripts: http://tldp.org/LDP/abs/html/

you'll have to get your output into an array using some sort of string manipulation using the spaces as delimiters, then loop over that to build some html output - so the return value will basically just output your select box on the page where you execute your cgi/bash script.

-sean



回答2:

Repeating the answer (since the original question was marked as duplicate):

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"