I am in the process of creating a bash script that will list the files (in this case apache sites-available). Listing the files is easy by my ultimate goal would be to take each of those files into an array, display them to the user and allow the user to select which "file" to process, in this case it would be to enable the site.
I haven't gotten very far, I know I need to set the ls as an array and then loop the action:
array=$(ls)
for sites in $array(2)
do
echo "$sites"
done
I know that I need to index each of the files in the directory and then allow the user to type the number to enable. So it would look like this:
(1) newdomain.com
(2) newdomain2.com
Which site would you like to enable (i.e 1)?
Hopefully that makes sense?
You could save yourself a lot of reimplementation by using the built-in select
feature.
The
select
construct allows the easy generation of menus. It has almost the same syntax as thefor
command:select name [in words ...]; do commands; done
The list of words following
in
is expanded, generating a list of items. The set of expanded words is printed on the standard error output stream, each preceded by a number. If thein words
is omitted, the positional parameters are printed, as ifin "$@"
had been specified. ThePS3
prompt is then displayed and a line is read from the standard input. If the line consists of a number corresponding to one of the displayed words, then the value ofname
is set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, theselect
command completes. Any other value read causesname
to be set to null. The line read is saved in the variableREPLY
.
That's not how you use ls
.
array=(*)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With