Prompt user to select a directory with a bash script and read result

前端 未结 3 1509
情深已故
情深已故 2020-12-28 18:41

I want to be read a dir with a bash script (actually I am using zsh).

I want to list the current folders in the same dir and display it to the user asking them to en

3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-28 18:50

    Unless your formatting requirements are very strict, you can probably just use bash’s select construct.

    The following code will present a menu of all the directories in the current directory and then chdir to the selected one:

    #!/bin/bash
    printf "Please select folder:\n"
    select d in */; do test -n "$d" && break; echo ">>> Invalid Selection"; done
    cd "$d" && pwd
    

提交回复
热议问题