1

So I am working on an install script for a program that needs the device id from lsusb in its 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.

The line looks as follows:

Bus 001 Device 004: ID 0665:5161 Cypress Semiconductor USB to Serial

So I want only the 9 characters after "ID{space}"

0

4 Answers 4

0

The first errors that I see are. You wrote $usblist=(lsusb | awk '{print $6}')

You need to remove the $ from the start, and add a $ before the (. Some quotes will also help. i.e.

usblist="$(lsusb | awk '{print $6}')"

0

Just use, this uses awk to print out the 6th field which in your case is the ID of the device

usblist="$(lsusb | awk '{print $6}')"

So you print any field you want, here are the mapping:

  • $1 : Bus
  • $2 : 001
  • $3 : Device
  • $4 : 004:
  • $5 : ID
  • $6 : 0665:5161
  • $7 : Cypress
  • $8 : Semiconductor
  • $9 : USB
  • $10 : to
  • $11 : Serial

If you want to print more than one field , such as name , you can do like this

usblist="$(lsusb | awk '{print $7,$8,$9}')"
0
0

usblist=lsusb | awk '{print $6}'

echo $usblist

it should print 0665:5161, other than awk you can also use cut command.Check man page of cut for more info.

3
  • I get some duplicate values with this command - some of the Linux root hubs are the same. echo $(lsusb | awk '{print $6}' | sort -u)} seems OK though Commented Jul 2, 2021 at 21:02
  • Correction , it should be usblist=`lsusb | awk '{print $6}'`, if you are getting duplicate values then you can pipe above output to uniq
    – rohit
    Commented Jul 3, 2021 at 11:28
  • uniq doesn't do the job; echo $(lsusb | awk '{print $6}' | uniq ) gives [1d6b:0003 1d6b:0002 1d6b:0003 04b3:3003 04b3:3004 05e3:0610 0b05:1939 046d:c077 1d6b:0002] - that's 9 items - but echo $(lsusb | awk '{print $6}' | sort -u) gives [046d:c077 04b3:3003 04b3:3004 05e3:0610 0b05:1939 1d6b:0002 1d6b:0003] which is 7 items. Note man uniq - "Filter adjacent matching lines from INPUT (or standard input)..." Commented Jul 3, 2021 at 13:53
0

Array assignment syntax in zsh, ksh93, bash, mksh or yash is:

array=( value1 value2... )

Getting the output of a command is $(a command).

Splitting the output of a command on newline can be done with the f parameter expansion flag in zsh or in other shells by leaving that $(...) unquoted (the split+glob operator), after having assigned the newline character to the $IFS special variable and disabled globbing.

  • zsh:

    array=( ${(f)"$(a command)"} )
    
  • other shells (but also zsh)

    IFS='
    '
    set -o noglob # not needed in zsh unless in sh/ksh emulation
    array=( $(a command) )
    

To get the 6th blank delimited field of each of the lines of the output of a command, as others have said, you can use awk '{print $6}' or grep -Po '^.*?ID \K.{9}' (assuming GNU grep or compatible with perl-style regex support enabled) to get the nine characters after the first occurrence of ID<space> as you requested. So putting it altogether, that becomes:

array=( ${(f)"$(lsusb | awk '{print $6}')" ) # (zsh)
IFS='
'
set -o noglob
array=( $(lsusb | awk '{print $6}') )

Now, the default value of $IFS is space, tab, newline (and NUL in zsh), and USB ids in theory only contain hexadecimal digits and colons, so the output of awk should be guaranteed not to contain space, tab, nul nor glob characters, so if you can guarantee that $IFS still contains its default value, you can omit the first two commands and simplify it to:

array=( $(lsusb | awk '{print $6}') )

Or you could get the information directly from /sys without need for the lsusb nor awk command in zsh with:

array=(
  /sys/bus/usb/devices/*/idVendor(Ne['REPLY=$(<$REPLY):$(<$REPLY:h/idProduct)'])
)

Using globbing to find the idVendor files and the e glob qualifier to replace each glob expansion with the contents of that file and that of the idProduct file in the same directory (also has the benefit of giving you a sorted list).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .