How do you store a list of directories into an array in Bash (and then print them out)?

后端 未结 4 708
别那么骄傲
别那么骄傲 2020-12-05 09:52

I want to write a shell script to show a list of directories entered by a user and then for a user to select one of the directories with an index number based on how many di

4条回答
  •  有刺的猬
    2020-12-05 10:32

    $ 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/!
    

提交回复
热议问题