1

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)?

Maxim Pontyushenko
  • 2,983
  • 2
  • 25
  • 36
ftshtw
  • 629
  • 1
  • 5
  • 19

1 Answers1

1

Instead of :

@devices = `./lib/scripts/get_serial.sh`.strip.gsub(" ","").split("\n")

Try to use:

@devices = `./lib/scripts/get_serial.sh`.split("\n").map {|hash_string| eval(hash_string)}

For more explanation you should check How do I convert a String object into a Hash object?

Community
  • 1
  • 1
Maxim Pontyushenko
  • 2,983
  • 2
  • 25
  • 36
  • Unfortunately `device['name']` in the view shows empty/blank value. – ftshtw Jan 21 '16 at 20:31
  • @ChrisV. Why even should it? Try ``device[:name]`` – Maxim Pontyushenko Jan 21 '16 at 20:31
  • If you don't explain this, I will resign from my job and become a baker or a garbage collector. – ftshtw Jan 21 '16 at 20:33
  • @ChrisV. Symbols are not Strings. Have a look at this, should help: http://stackoverflow.com/questions/9712706/ruby-symbols-vs-strings-in-hashes – Maxim Pontyushenko Jan 21 '16 at 20:35
  • I mean about the map and eval, not the symbols. Do you believe there's an easier way to accomplish this (the whole idea)? – ftshtw Jan 21 '16 at 20:40
  • @ChrisV. You don't need ``strip`` and delete spaces. By splitting you make an array from strings separated by ``\n``. I'm not really into bash-scripting and the ``eval`` thing just came from the source I provided in my answer :) – Maxim Pontyushenko Jan 21 '16 at 20:49