I need to create a script to unbind/bind all usb in one linux machine.
For this I need to run:
lsusb -t wich returns:
root@lsdg7sd-fc:~# lsusb -t
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ohci_hcd/10p, 12M
|__ Port 4: Dev 2, If 0, Class=vend., Driver=ftdi_sio, 12M
|__ Port 6: Dev 4, If 0, Class=HID, Driver=usbhid, 1.5M
|__ Port 6: Dev 4, If 1, Class=HID, Driver=usbhid, 1.5M
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/10p, 480M
Now, for this example I need to run:
// disable the bus 1, port 1
echo '2-4' | tee /sys/bus/usb/drivers/usb/unbind
// disable the bus 1, port 2
echo '2-6' | tee /sys/bus/usb/drivers/usb/unbind
// enable the bus 1, port 1
echo '2-4' | tee /sys/bus/usb/drivers/usb/bind
// enable the bus 1, port 2
echo '2-6' | tee /sys/bus/usb/drivers/usb/bind
In order to achieve this by creating a script (I am very new to linux), I did the following:
#!/bin/bash
lsusb -t | awk '{
bus="";
if($1=="|__")
print "Child USB Port: ",$3;
else if ($1=="/:") {
print $3;
var= $(echo $3 | grep -o "[1-9]");
echo $var;
}
}'
var=`echo "02.Port" | grep -o "[1-9]"`;
echo $var;
var=`echo "02.Port" | grep -o "[0-9]\{2\}"`;
echo $var;
I tried all the possible combinations in order to set var. I always get an error.
awk: line 8: syntax error at or near grep
Can anyone suggest a solution or another approach?
Thanks for reading.
---------------------- Update
Thanks Ed Morton, the input is the result of the command lsusb -t: (at my end)
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=ohci_hcd/10p, 12M
|__ Port 4: Dev 2, If 0, Class=vend., Driver=ftdi_sio, 12M
|__ Port 6: Dev 4, If 0, Class=HID, Driver=usbhid, 1.5M
|__ Port 6: Dev 4, If 1, Class=HID, Driver=usbhid, 1.5M
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=ehci_hcd/10p, 480M
And I want to parse this information in order to get the BUS number and the port number. So the output should be:
2-1
2-4
2-6
2-6
1-1
But, I only need the "child" ports, so I need the following output:
2-4
2-6
2-6
Thanks again!