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

1 Answer 1

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?

Sign up to request clarification or add additional context in comments.

6 Comments

Unfortunately device['name'] in the view shows empty/blank value.
@ChrisV. Why even should it? Try device[:name]
If you don't explain this, I will resign from my job and become a baker or a garbage collector.
@ChrisV. Symbols are not Strings. Have a look at this, should help: stackoverflow.com/questions/9712706/…
I mean about the map and eval, not the symbols. Do you believe there's an easier way to accomplish this (the whole idea)?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.