bash - 在 shell 脚本中选择 IP
问题描述
我正在编写一个 shell 脚本来部署我的主节点。为了设置节点,我想选择可用的 IP 地址,master 稍后应该监听:
PS3='Please select a network the master should listen onto: '
ips=($(hostname -I))
select ip in "${ips[@]}"
do
case $ip in
"Option 1")
echo "you chose choice 1"
;;
"Quit")
break
;;
*) echo "invalid option $REPLY";;
esac
done
但是我遇到了“无效选项”。如何从我的列表中正确选择 IP 并将其进一步用作脚本中的变量?
解决方案
您需要匹配数字。就像是
#!/usr/bin/env bash
PS3='Please select a network the master should listen onto: '
ips=($(hostname -I))
ips=("${ips[@]}" 'Quit')
select ip in "${ips[@]}"; do
case $ip in
*[0-9]*)
echo "you chose choice $REPLY with the value of $ip"
break
;;
Quit) echo quit
break;;
*) echo Invalid option >&2;;
esac
done
推荐阅读
- i18next - 将过滤器应用于嵌套组件
- json - Dart/Flutter 中用于 FHIR 资源的 Json oneOf
- javascript - Firestore/Javascript:如何从子文档的地图字段中获取数据?
- templates - ServiceStack:运行“web new web-corefx ProjectName”创建一个 .NET Framework 4.x 项目
- c# - WPF:过滤 ComboBox 的 ItemCollection 还会过滤绑定到同一 ItemsSource 的其他 ComboBox
- php - 如何在没有第二次选择的情况下对平均函数结果应用 where 条件?
- java - JavaFX:Stage show() 方法的问题
- parsing - 为什么 soup.find_all 总是返回一个空字符串,即使在指定的类中存在非常清楚的标签?
- javascript - 使用反应钩子将基于类的组件转换为功能组件
- node.js - 出现错误:对象作为 React 子对象无效