Linux 从 ls 创建数组的 Bash 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6561237/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 04:54:17  来源:igfitidea点击:

Bash script that creates an Array from ls?

linuxarraysbashscripting

提问by jason.dot.h

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.

我正在创建一个 bash 脚本,该脚本将列出文件(在本例中为 apache 站点可用)。列出文件很容易,我的最终目标是将这些文件中的每一个放入一个数组中,将它们显示给用户并允许用户选择要处理的“文件”,在这种情况下将启用站点。

I haven't gotten very far, I know I need to set the ls as an array and then loop the action:

我还没有走多远,我知道我需要将 ls 设置为一个数组,然后循环操作:

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?

希望这是有道理的?

回答by geekosaur

You could save yourself a lot of reimplementation by using the built-in selectfeature.

通过使用内置select功能,您可以节省大量的重新实现。

The selectconstruct allows the easy generation of menus. It has almost the same syntax as the forcommand:

select name [in words ...]; do commands; done

The list of words following inis 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 the in wordsis omitted, the positional parameters are printed, as if in "$@"had been specified. The PS3prompt 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 of nameis set to that word. If the line is empty, the words and prompt are displayed again. If EOF is read, the selectcommand completes. Any other value read causes nameto be set to null. The line read is saved in the variable REPLY.

select构造允许轻松生成菜单。它的语法几乎与for命令相同:

select name [in words ...]; do commands; done

下面的单词列表in被扩展,生成一个项目列表。扩展单词集打印在标准错误输出流上,每个单词前面都有一个数字。如果in words省略,则打印位置参数,就像in "$@"已指定一样。所述PS3然后提示显示和线从标准输入读取。如果该行包含与显示的单词之一对应的数字,则将 的值name设置为该单词。如果该行为空,则再次显示单词和提示。如果读取 EOF,则select命令完成。读取的任何其他值都会导致name设置为 null。读取的行保存在变量中REPLY

回答by Ignacio Vazquez-Abrams

That's not how you use ls.

这不是你使用的方式ls

array=(*)

回答by hornetbzz

some hint to get you started :

一些让你开始的提示:

APACHE_CONF=/etc/apache2
SITES_TO_ENABLE="site1.org | site2.com"
LIST_AVAILABLE=$(ls $APACHE_CONF/sites-available)
LIST_ENABLED=$(ls $APACHE_CONF/sites-enabled)

for site in $(echo $SITES_TO_ENABLE | sed -e "s/|//g")
do
    FOUND=$(echo $LIST_AVAILABLE | sed -e "s/ /\n/g" | egrep $site)
    [[ ! -z $FOUND ]] && echo "Checking availability of $site: Ok"
    [[ -z $FOUND ]] && echo "Checking availability of $site: Nok, site \"$site\" required for production has not been found or is not defined" && exit 1
done

You can combine this approach with selectof course.

当然,您可以将此方法与select结合使用。