bash - Shell Bash : How to prompt a user to select from a dynamically populated list?

标签 bash shell sh

新手来了!

在 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/

相关文章:

linux - 即使报告捕获了数百万个数据包,Tcpdump 保存的文件也是空的

linux - 在 Linux Mint 上构建 rtem 时 shell cmd 失败

scripting - 如何在 Solaris 上的 find 命令中排除完整目录路径列表

bash - 需要一个 shell 脚本来删除除 *.pdf 之外的所有文件

linux - Bash,来自带有 STDIN 的文件的 Cowsay 命令

bash - echo 输出中的三个星号插入列表?

linux - 解析 "control"文件的脚本出现问题

java - echo %JAVA_HOME% 在 cmd 中有效,但在 git bash 中无效

bash - 在 sh 中初始化数组

linux - 仅在键入字符后退格删除 bash 提示