Как сохранить совпадающие слова из вывода dmesg в списке

459
user423369

Я подключаю устройства USB-Serial к своему устройству, используя разные порты, и устройства с последовательным интерфейсом USB иногда отображаются как «ttyUSB0», иногда «ttyUSB1» или «ttyUSB2».

Как я могу написать скрипт для извлечения всех имен ttyUSB из этой команды:

deviceNodes = "$(sudo dmesg tail | egrep -i 'ttyUSB')" echo "$"  for i in "$" do udevadm info -a -n /dev/ttyUSB1 | grep '' | head -n1 done 

и сохранить его в переменной и зациклить элементы в списке?

Вот результат команды dmesg:

[37606.832517] usb 2-1.1: FTDI USB Serial Device converter now attached to ttyUSB0 [37664.565271] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0 [37695.844687] usb 2-1.1: FTDI USB Serial Device converter now attached to ttyUSB0 [38017.111961] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0 [38490.802048] usb 2-1.1: FTDI USB Serial Device converter now attached to ttyUSB0 [38776.225985] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0 [38778.317840] usb 2-1.1: FTDI USB Serial Device converter now attached to ttyUSB0 [38874.027395] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0 [38876.631579] usb 2-1.1: FTDI USB Serial Device converter now attached to ttyUSB0 [39040.443963] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0 [39796.942837] usb 2-1.2: FTDI USB Serial Device converter now attached to ttyUSB0 [39802.674018] usb 2-1.1: FTDI USB Serial Device converter now attached to ttyUSB1 [40372.029798] ftdi_sio ttyUSB1: FTDI USB Serial Device converter now disconnected from ttyUSB1 [40372.156024] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0 [41642.886671] usb 2-1.1: FTDI USB Serial Device converter now attached to ttyUSB0 
0
Пожалуйста, добавьте вывод `sudo dmesg tail | egrep -i 'ttyUSB'` на ваш вопрос. Cyrus 9 лет назад 0

2 ответа на вопрос

0
dtbnguyen

Isn't it simply a matter of changing the following,

deviceNodes = "$(sudo dmesg tail | egrep -i 'ttyUSB')" 

to this?

deviceNodes="$(cat temp | egrep -i 'ttyUSB' | sed 's/^.*tty/tty/' )" 

'i' will obviously be the variable you need to iterate over.

Test was the following.

echo "[37664.565271] ftdi_sio ttyUSB0: FTDI USB Serial Device converter now disconnected from ttyUSB0" | sed 's/^.*tty/tty/' ttyUSB0 
0
tripleee
> deviceNodes = "$(sudo dmesg tail | egrep -i 'ttyUSB')" 

That's a syntax error. You can't have whitespace around the equals sign in a variable assignment. See also http://mywiki.wooledge.org/BashPitfalls#foo_.3D_bar

Furthermore, if you only want to iterate over the actual matches, you need to change the expression to only extract those. Try this.

dmesg | grep -Eo 'ttyUSB[0-9]+' | sort -u | while read devnode; do udevadm info -a -n /dev/"$devnode" | grep '' | head -n1 done 

(On my system, dmesg runs without sudo just fine, and doesn't have a tail argument.)

Похожие вопросы