我的脚本从用户那里获取一个站点名。
./run_script <site>
./run_script cambridge
然后,它允许用户通过脚本签出、编辑和提交对文件的更改。
但是,有些网站有两到六个文件。
所以脚本如下所示
你有不止一个剑桥档案。
请从以下选项中选择:
剑桥1
剑桥2
剑桥3
用户输入单词cambridge[1-3]
但是,我想给每个变量赋值,如下所示。
请选择所需选项:
1)。剑桥1
(第二章)。剑桥2
(第三章)。剑桥3
用户输入1、2或3,然后它将拾取文件。
我现在的代码是:
echo $(tput setaf 5)
echo "Please choose from the following: "
echo -n $(tput sgr0)
find path/to/file/. -name *"$site"* | awk -F "/" '{print $5}' | awk -F "SITE." '{print $2}'
echo $(tput setaf 3)
read -r input_variable
echo "You entered: $input_variable"
echo $(tput sgr0)
最佳答案
有个有趣的方法:
# save the paths and names of the options for later
paths=`find path/to/file/. -name "*$site*"`
names=`echo "$paths" | awk -F "/" '{print $5}' | awk -F "SITE." '{print $2}'`
# number the choices
n=`echo "$names" | wc -l`
[ "$n" -gt 0 ] || echo "no matches" && exit 1
choices=`paste <(seq 1 $n) <(echo "$names") | sed 's/\t/). /'`
echo "Please choose from the following: "
echo "$choices"
read -r iv
echo "You entered: $iv"
# make sure they entered a valid choice
if [ ! "$iv" -gt 0 ] || [ ! "$iv" -le "$n" ]; then
echo "invalid choice"
exit 1
fi
# name and path of the user's choice:
name_chosen=`echo "$names" | tail -n+$iv | head -n1`
path_chosen`echo "$paths" | tail -n+$iv | head -n1`
关于linux - Bash提供编号结果供用户选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36648292/