Linux Shell从用户输入获取设备ID [英] Linux shell get device id from user input
问题描述
我正在为需要在配置中从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设备
准备whiptail
或dialog
命令时,将 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 oflsub
by line, space separated, dropping 5 first words, assigning 6th word toid
and rest of line todsc
.
这可能会呈现出类似以下内容:
This could render something like:
然后
echo $usbdev
1d6b:0002
您可以在 USB可移动存储选择器: USBKeyChooser
这篇关于Linux Shell从用户输入获取设备ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!