Linux Shell从用户输入获取设备ID [英] Linux shell get device id from user input

查看:549
本文介绍了Linux Shell从用户输入获取设备ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为需要在配置中从lsusb获得设备ID的程序的安装脚本工作,所以我正在考虑执行以下操作:

I am working on an install script for a program that needs the device id from lsusb in it's configuration so I was thinking of doing the following:

$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脚本很陌生

Sorry I haven't gotten very far with my code but I am stuck on this and have no idea how to do what I would like to do. Please can someone help. I am very new to shell scripting

推荐答案

使用whiptail选择USB设备

准备whiptaildialog命令时,将 USB ID 作为 TAG ,将 description 作为 item ,您可以创建一个小子外壳:

Using whiptail for choosing USB device

For preparing whiptail or dialog command, with USB ID as TAG and description as item, you could create a little sub-shell:

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
)

Nota:

  • $array变量在子shell范围之外将不存在.
  • 由于$array($id "$dsc")填充并由"${array[@]}"使用,描述中的空格不会破坏项目列表.
  • 语法read foo{,,,} id dsc将按行读取lsub的输出,并以空格分隔,删除5个第一个单词,将第6个单词分配给id,其余的行分配给dsc.
  • The $array variable won't exist outside of the scope of subshell.
  • As $array is populated by ($id "$dsc") and used by "${array[@]}", space in description won't break item list.
  • syntax read foo{,,,} id dsc will read output of lsub by line, space separated, dropping 5 first words, assigning 6th word to id and rest of line to dsc.

这可能会呈现出类似以下内容:

This could render something like:

然后

echo $usbdev 
1d6b:0002

您可以在 USB可移动存储选择器: USBKeyChooser

这篇关于Linux Shell从用户输入获取设备ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆