微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

数组 – 如何将目录列表存储到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/!

原文地址:https://www.jb51.cc/bash/386561.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐