linux - Linux shell 从用户输入中获取设备 ID
问题描述
我正在为一个程序编写安装脚本,该程序需要 lsusb 的设备 ID 在其配置中,所以我正在考虑执行以下操作:
$usblist=(lsusb)
#put the list into a array for each line.
#use the array to give the user a selection list usinging whiptail.
#from that line strip out the device id and vender id from the selected line.
抱歉,我的代码还没有走得太远,但我坚持这一点,不知道如何做我想做的事。请有人帮忙。我对 shell 脚本很陌生
解决方案
用于whiptail
选择 USB 设备
对于准备whiptail
或dialog
命令,使用USB ID作为TAG和描述作为item,您可以创建一个小子外壳:
read usbdev < <(
declare -a array=()
while read foo{,,,,} id dsc;do
array+=($id "$dsc")
done < <(lsusb)
whiptail --menu 'Select USB device' 20 76 12 "${array[@]}" 2>&1 >/dev/tty
)
注意:
- 该
$array
变量不会存在于 subshell 的范围之外。 - 正如
$array
由 填充($id "$dsc")
和使用的那样"${array[@]}"
,描述中的空格不会破坏项目列表。 - 语法
read foo{,,,} id dsc
将按行读取输出lsub
,空格分隔,删除 5 个第一个单词,将第 6 个单词分配给 ,将id
其余行分配给dsc
.
这可能会呈现如下内容:
然后
echo $usbdev
1d6b:0002
您可以在如何在 Linux shell 脚本中提示输入是/否/取消whiptail
?
和USB 可移动存储选择器:USBKeyChooserdialog
推荐阅读
- python - Sklearn 拟合线性回归
- react-native - 如何在 Android 上使用 React Native 实现组件高度的平滑动画
- alfresco - 如何将自定义按钮添加到 Alfresco 共享页面正文?
- xml - 使用 XSLT 从 Word 文档中提取文本
- python - 如何将此 Pandas DataFrame 转换为特定的 JSON 格式?
- html - iframe 没有控件工作,视频不播放
- dataframe - 我想创建一个新的数据框(可能使用 groupby 或 loc),其中一列中的所有唯一值都成为列名
- c++ - 在 g++ <7.4.0> 上 boost::archive::text_iarchive in_archive {is} boost <1.71> 崩溃
- azure - Azure ARM 角色分配不同的资源组
- c# - ASP.NET Core 3.1 Web 应用程序在 IIS Express 上运行时抛出错误 500.30,但在使用 dotnet watch run 时不会