I have a bash script from another stackoverflow question to get all my serial connected devices:
#!/bin/bash
for sysdevpath in $(find /sys/bus/usb/devices/usb*/ -name dev); do
(
syspath="${sysdevpath%/dev}"
devname="$(udevadm info -q name -p $syspath)"
[[ "$devname" == "bus/"* ]] && continue
eval "$(udevadm info -q property --export -p $syspath)"
[[ -z "$ID_SERIAL" ]] && continue
echo "{name: '/dev/$devname', id_serial: '$ID_SERIAL'}"
)
done
I am trying to parse them as an array of objects in order to iterate and display them on a Rails view. The output is this:
{name: '/dev/input/event16', id_serial: 'Logitech_USB_Receiver'}
{name: '/dev/input/mouse2', id_serial: 'Logitech_USB_Receiver'}
{name: '/dev/hidraw0', id_serial: 'Logitech_USB_Receiver'}
{name: '/dev/usb/hiddev3', id_serial: 'Logitech_USB_Receiver'}
{name: '/dev/input/event17', id_serial: 'Logitech_USB_Receiver'}
{name: '/dev/hidraw1', id_serial: 'Logitech_USB_Receiver'}
{name: '/dev/input/event15', id_serial: 'SunplusIT_INC._Integrated_Camera'}
{name: '/dev/media0', id_serial: 'SunplusIT_INC._Integrated_Camera'}
{name: '/dev/video0', id_serial: 'SunplusIT_INC._Integrated_Camera'}
In Rails console this is the output:
2.2.4 :001 > `./lib/scripts/get_serial.sh`
=> "{name: '/dev/input/event16', id_serial: 'Logitech_USB_Receiver'}\n{name: '/dev/input/mouse2', id_serial: 'Logitech_USB_Receiver'}\n{name: '/dev/hidraw0', id_serial: 'Logitech_USB_Receiver'}\n{name: '/dev/usb/hiddev3', id_serial: 'Logitech_USB_Receiver'}\n{name: '/dev/input/event17', id_serial: 'Logitech_USB_Receiver'}\n{name: '/dev/hidraw1', id_serial: 'Logitech_USB_Receiver'}{name: '/dev/media0', id_serial: 'SunplusIT_INC._Integrated_Camera'}\n{name: '/dev/video0', id_serial: 'SunplusIT_INC._Integrated_Camera'}\n"
so I am trying to parse it like this in my controller:
def index
@devices = `./lib/scripts/get_serial.sh`.strip.gsub(" ","").split("\n")
end
and If I do @devices[0]
the result is: => "{name:'/dev/input/event16',id_serial:'Logitech_USB_Receiver'}"
.
Finally, in the view:
<ul>
<% @devices.each do |device| %>
<li><%= device %> </li>
<% end %>
</ul>
displays all the devices, but If I do <%= device.name %>
I get an error or <%= device['name'] %>
I get the word "name".
My question is simple. Is there any better way to do this and process :name
and :id_serial
as typical variables (iterate the array and print them)?