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

前端之家收集整理的这篇文章主要介绍了数组 – 如何将目录列表存储到Bash中的数组中(然后将其打印出来)?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想编写一个 shell脚本来显示一个用户输入的目录列表,然后让用户根据目录数目选择一个索引号的目录之一

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

例:

  1. > whichdir
  2. There are 3 dirs in the current path
  3. 1 dir1
  4. 2 dir2
  5. 3 dir3
  6. which dir do you want?
  7. > 3
  8. you selected dir3!
  1. $ls -a
  2. ./ ../ .foo/ bar/ baz qux*
  3. $shopt -s dotglob
  4. $shopt -s nullglob
  5. $array=(*/)
  6. $for dir in "${array[@]}"; do echo "$dir"; done
  7. .foo/
  8. bar/
  9. $for dir in */; do echo "$dir"; done
  10. .foo/
  11. bar/
  12. $PS3="which dir do you want? "
  13. $echo "There are ${#array[@]} dirs in the current path"; \
  14. select dir in "${array[@]}"; do echo "you selected ${dir}"'!'; break; done
  15. There are 2 dirs in the current path
  16. 1) .foo/
  17. 2) bar/
  18. which dir do you want? 2
  19. you selected bar/!

猜你在找的Bash相关文章