I am trying to port a script to handle 'writing to a serial port' from matlab. I have a serial device on the port /dev/ttyUSB0, to which I need to write '\xFE\x6C\x01' to turn on a relay. I used
echo -en '\xFE\x6C\x01' > /dev/ttyUSB0
on the terminal. It works fine and the relay is turned on. Now if i use it in a bash script file,
#!/bin/bash
echo -en '\xFE\x6C\x01' > /dev/ttyUSB0
and run the file, nothing happens. Why is it so?
This is my first serious bash script. Thanks for any help
bash -x script.sh
show the echo happening? Are you running the script as a user that can write to that device? – Etan Reisnersh script.sh
. and withsh -x script.sh
(The latter showed the echo happening, but still the serial relay wasn't turned on.) If I runbash -x script.sh
the relay turns on. What is the difference? (Thanks a lot) I removed everything else from my bash script for the sake of clarity – chinnsh
's builtinecho
doesn't support the-e
option. Maybe that explains why you don't get what you want when the script is executed withsh
. Withsh
, you may use the externalecho
command, with/bin/echo
. – gniourf_gniourfsh
is most commonlydash
, notbash
. And, as the above comment points out,echo
inbash
is a builtin, so the results are not necessarily going to be portable. That's one of the reasons why using printf is preferred if you're trying to do anything more fancy than just printing a basic string. (printf
is also a builtin in bash, but is far more portable except in various extension cases) – Reinstate Monica Please