提示用户使用bash脚本选择目录并读取结果 [英] Prompt user to select a directory with a bash script and read result
问题描述
我想使用bash脚本读取目录(实际上我正在使用zsh).
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 enter a number to select the correct folder.
Please select a Folder eg, 1,2,3.
1. Folder Name 1 (this should the actual name of the folder in the dir
2. Folder 2
3. Folder 3.
我还希望能够转换输入,例如1.回到实际的文件夹名称,这样我就可以
I would like to also be able to convert the input eg 1. Back to the actual folder name so I can
cd ./$foldername/
感谢您的帮助. 干杯,约翰.
Thanks for you help. Cheers, John.
推荐答案
Unless your formatting requirements are very strict, you can probably just use bash’s select
construct.
以下代码将显示当前目录中所有目录的菜单,然后按chdir到所选目录:
The following code will present a menu of all the directories in the current directory and then chdir to the selected one:
printf "Please select folder:\n"
select d in */; do test -n "$d" && break; echo ">>> Invalid Selection"; done
cd "$d" && pwd
这篇关于提示用户使用bash脚本选择目录并读取结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!