我想编写一个
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/!