数组 – 如何将目录列表存储到Bash中的数组中(然后将其打印出来)
发布时间:2020-12-15 18:51:30 所属栏目:安全 来源:网络整理
导读:我想编写一个 shell脚本来显示一个用户输入的目录列表,然后让用户根据目录数目选择一个索引号的目录之一 我认为这是一种数组操作,但我不知道如何在shell脚本中这样做 例: whichdirThere are 3 dirs in the current path1 dir12 dir23 dir3which dir do you
我想编写一个
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/! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |