2
votes

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

1
What do you mean "nothing happens"? Does running bash -x script.sh show the echo happening? Are you running the script as a user that can write to that device?Etan Reisner
Is that the whole of your bash script file?Uncle Iroh
Okay, I look like an idiot now. I ran the script with sh script.sh. and with sh -x script.sh (The latter showed the echo happening, but still the serial relay wasn't turned on.) If I run bash -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 claritychinn
sh's builtin echo doesn't support the -e option. Maybe that explains why you don't get what you want when the script is executed with sh. With sh, you may use the external echo command, with /bin/echo.gniourf_gniourf
@chinn sh is most commonly dash, not bash. And, as the above comment points out, echo in bash 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

1 Answers

-1
votes

I think that is not possible, because with a simple echo, it is not possible to set a speed of serial port.