create variable from tail -f /var/logmessage

47 Views Asked by At

I'm trying to get a variable from /var/log/messages when the string "Attached" appears in /var/log/messages.

I've got so far, and stuck:

 sudo stdbuf -o0 tail -f -n0 /var/log/messages | awk  '{if ($9 == "Attached") print$8}' 

This gives the usb device dev id eg [sdc] when a USB device is plugged in. FRom here I plan to mount the device as exfat. I'm using Centos 7, which does not automount exfat. fuse-exfat and exfat-utils are installed.

1

There are 1 best solutions below

0
On

You are using 'tail -f' which will never terminate (it will wait for additional log messages). You probably want to 'grep' from the file, and pick the first (or last)

device=$(sudo cat /var/log/messages | awk  '{if ($9 == "Attached") print $8 ; exit}')

The 'exit' can be used to pick the first match.