I have a function, which gets information of all usb connected devices.
connected_devices = :os.cmd('usb-devices | grep -A 1 Product=')
When I use :os.cmd, it works fine. However, since :os.cmd does not return error code, I would like to use System.cmd for better error handling.
connected_deivces = System.cmd("usb-devices", ["|", "grep", "-A", "1", "Product="])
So I tried to change function like above, but it does not execute command after pipeline.
How can I execute command with pipeline with System.cmd?
System.cmd can only invoke one program directly. os:cmd passes the command to the default shell of the operating system which allows you to use pipes. To do the same thing with System.cmd, you can call /bin/sh if you're on a Unix:
System.cmd("/bin/sh", ["-c", "usb-devices | grep -A 1 Product="])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With