首页 > 解决方案 > 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 脚本很陌生

标签: linuxshelllsusb

解决方案


用于whiptail选择 USB 设备

对于准备whiptaildialog命令,使用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变量不会存在于 subshel​​l 的范围之外。
  • 正如$array由 填充($id "$dsc")和使用的那样"${array[@]}",描述中的空格不会破坏项目列表。
  • 语法read foo{,,,} id dsc将按行读取输出lsub,空格分隔,删除 5 个第一个单词,将第 6 个单词分配给 ,将id其余行分配给dsc.

这可能会呈现如下内容:

在此处输入图像描述

然后

echo $usbdev 
1d6b:0002

您可以在如何在 Linux shell 脚本中提示输入是/否/取消whiptail? 和USB 可移动存储选择器:USBKeyChooserdialog


推荐阅读