'Lost creating a .sh file using either echo -en or printf
I'm trying to create a .sh file to write to a serial usb relay... on a Raspberry pi running Rasbian. If run the following lines from the command prompt it sets the relay on/off correctly:
pi@raspberrypi:~/SerialRelay $ echo -en '\xa0\x01\x01\xa2' | sudo dd of=/dev/usbrelay1-1.3
0+1 records in
0+1 records out
4 bytes copied, 0.00693582 s, 0.6 kB/s
pi@raspberrypi:~/SerialRelay $ echo -en '\xa0\x01\x00\xa1' | sudo dd of=/dev/usbrelay1-1.3
0+1 records in
0+1 records out
4 bytes copied, 0.0075318 s, 0.5 kB/s
however if I add this to a .sh file and run, it fails to set on/off the relay with the lines output:
pi@raspberrypi:~/SerialRelay $ sudo ./Relay1.sh
0+1 records in
0+1 records out
21 bytes copied, 0.00726782 s, 2.9 kB/s
0+1 records in
0+1 records out
21 bytes copied, 0.00732381 s, 2.9 kB/s
pi@raspberrypi:~/SerialRelay $
I have run chmod 777
and ldconfig
on the .sh and I have also tried amending the .sh file with printf
rather than echo -en
commands. Both echo -en
or printf
works ok when run from the command line.
Please can anyone offer help.
Solution 1:[1]
echo -ne
and printf '\x..'
are not portable constructs, and therefore don't work the same between shells. You tested your code in Bash, but ran your script with Dash.
You can either:
- run the script with Bash, as detailed in Why does my Bash code fail when I run it with 'sh'?
- Rewrite it portably using
printf
octal escapes, e.g.printf '\240\001\001\242'
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | that other guy |