如何在 Bash 中将目录列表存储到数组中(然后将它们打印出来)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4494336/
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-09-09 19:59:15  来源:igfitidea点击:

How do you store a list of directories into an array in Bash (and then print them out)?

arraysbashdirectory

提问by qodeninja

I want to write a shell script to show a list of directories entered by a user and then for a user to select one of the directories with an index number based on how many directories there are

我想编写一个 shell 脚本来显示用户输入的目录列表,然后让用户根据目录的数量选择一个带有索引号的目录

I'm thinking this is some kind of array operation, but im not sure how to do this in shell script

我认为这是某种数组操作,但我不确定如何在 shell 脚本中执行此操作

example:

例子:

> whichdir
There are 3 dirs in the current path
1 dir1
2 dir2
3 dir3
which dir do you want? 
> 3
you selected dir3!

回答by Paused until further notice.

$ ls -a
./ ../ .foo/ bar/ baz qux*
$ shopt -s dotglob
$ shopt -s nullglob
$ array=(*/)
$ for dir in "${array[@]}"; do echo "$dir"; done
.foo/
bar/
$ for dir in */; do echo "$dir"; done
.foo/
bar/
$ PS3="which dir do you want? "
$ echo "There are ${#array[@]} dirs in the current path"; \
select dir in "${array[@]}"; do echo "you selected ${dir}"'!'; break; done
There are 2 dirs in the current path
1) .foo/
2) bar/
which dir do you want? 2
you selected bar/!

回答by John Kugelman

Array syntax

数组语法

Assuming you have the directories stored in an array:

假设您将目录存储在数组中:

dirs=(dir1 dir2 dir3)

You can get the length of the array thusly:

您可以这样获得数组的长度:

echo "There are ${#dirs[@]} dirs in the current path"

You can loop through it like so:

您可以像这样循环遍历它:

let i=1

for dir in "${dirs[@]}"; do
    echo "$((i++)) $dir"
done

And assuming you've gotten the user's answer, you can index it as follows. Remember that arrays are 0-based so the 3rd entry is index 2.

假设您已经得到用户的答案,您可以按如下方式对其进行索引。请记住,数组是从 0 开始的,因此第 3 个条目是索引 2。

answer=2

echo "you selected ${dirs[$answer]}!"

Find

How do you get the file names into an array, anyways? It's a bit tricky. If you have findthat might be the best way:

无论如何,您如何将文件名放入数组中?这有点棘手。如果你有find这可能是最好的方法:

readarray -t dirs < <(find . -maxdepth 1 -type d -printf '%P\n')

The -maxdepth 1stops find from looking through subdirectories, -type dtells it to find directories and skip files, and -printf '%P\n'tells it to print the directory names without the leading ./it normally likes to print.

-maxdepth 1停止从通过子目录寻找发现,-type d告诉它找到的目录和文件跳过,并且-printf '%P\n'告诉它没有导致打印目录名./它通常喜欢打印。

回答by Jester

#! /bin/bash

declare -a dirs
i=1
for d in */
do
    dirs[i++]="${d%/}"
done
echo "There are ${#dirs[@]} dirs in the current path"
for((i=1;i<=${#dirs[@]};i++))
do
    echo $i "${dirs[i]}"
done
echo "which dir do you want?"
echo -n "> "
read i
echo "you selected ${dirs[$i]}"

回答by Brian Agnew

Bash now supports single-dimensional arrays. Because the arrays don't need to have contiguous elements, they look more like maps.

Bash 现在支持一维数组。因为数组不需要有连续的元素,所以它们看起来更像地图。

回答by ocodo

Update: my answer is wrong

更新:我的答案是错误的

Leaving it here to address a common misunderstanding, below the line is erroneous.

把它留在这里是为了解决一个常见的误解,线下是错误的。



To put the directories in an array you can do...

要将目录放入数组中,您可以执行以下操作...

array=( $( ls -1p | grep / | sed 's/^\(.*\)/""/') )

This will capture the dir names, including those with spaces.

这将捕获目录名称,包括带有空格的名称。



Extracting from comments:

从评论中提取:

literal quotes don't have any effect on string-splitting, so array=( echo '"hello world" "goodbye world"' ) is an array with four elements, not two

文字引号对字符串拆分没有任何影响,因此 array=( echo '"hello world" "goodbye world"' ) 是一个包含四个元素的数组,而不是两个

@Charles Duffy

@查尔斯·达菲

Charles also supplied the following link Bash FAQ #50which is an extended discussion on this issue.

Charles 还提供了以下链接Bash FAQ #50,这是对此问题的扩展讨论。

I should also draw attention to the link posted by @Dennis Williamson - why I shouldn't have used ls

我还应该提请注意@Dennis Williamson 发布的链接 -为什么我不应该使用ls