新手来了!
在 Shell 脚本 (bash) 上,在提示用户输入名称/密码后,脚本应从 server.list 中读取并生成选项以供选择。
我如何为用户提供 server.list 中的选项,如下所示:
#Please select from the server list:
1) 10.1.1.xx
2) 10.1.1.xx
3) 10.1.1.xx
Select option [1]:
任何帮助,将不胜感激!
最佳答案
假设这个源文件:
$ cat server.list
10.1.1.xx
10.1.2.xx
10.1.3.xx
简答:
select ip in $(cat server.list); do
echo $REPLY $ip
done
演示
$ select ip in $(cat server.list); do echo $REPLY $ip; done
1) 10.1.1.xx
2) 10.1.2.xx
3) 10.1.3.xx
#? 1
1 10.1.1.xx
#? 2
2 10.1.2.xx
检查:http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_09_06.html
您将不得不实现
case
循环使用 ip
做一些有用的事情多变的。示例
select ip in $(cat server.list) exit; do
case $ip in
exit) echo "exiting"
break ;;
*) echo ip $ip;
esac
done
关于bash - Shell Bash : How to prompt a user to select from a dynamically populated list?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34256437/