数组 – 如何将目录列表存储到Bash中的数组中(然后将其打印出来)?

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

我认为这是一种数组操作,但我不知道如何在shell脚本中这样做

例:

> whichdir
There are 3 dirs in the current path
1 dir1
2 dir2
3 dir3
which dir do you want? 
> 3
you selected dir3!
$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/!

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...