提示用户使用bash脚本选择目录并读取结果 [英] Prompt user to select a directory with a bash script and read result

查看:60
本文介绍了提示用户使用bash脚本选择目录并读取结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用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.

推荐答案

除非您的格式要求非常严格,否则您可以只使用 bash

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆