0

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.

jthulhu
  • 7,223
  • 2
  • 16
  • 33
  • You have to make sure it runs with Bash. Use `#!/bin/bash` as the shebang in your script file – that other guy May 04 '22 at 17:54
  • Add the contents of `Relay1.sh` to your question – David C. Rankin May 04 '22 at 18:36
  • Also many thanks to @thatotherguy other guy for setting me on the right path. All working now. – DrinkCoffee May 04 '22 at 19:17
  • @David C. Rankin The .sh file is:- echo -en '\xa0\x01\x01\xa2' | sudo dd of=/dev/usbrelay1-1.3 sleep 1 echo -en '\xa0\x01\x00\xa1' | sudo dd of=/dev/usbrelay1-1.3 sleep 3 echo -en '\xa0\x01\x01\xa2' | sudo dd of=/dev/usbrelay1-1.3 sleep 1 echo -en '\xa0\x01\x00\xa1' | sudo dd of=/dev/usbrelay1-1.3 My coffee maker automatically shuts off after 30 minutes ( another EU legislation). The .sh file mimics the toggle on/off button and is to inserted into crontab to switch the machine off/on every 20 minutes. – DrinkCoffee May 04 '22 at 19:18
  • Sorry I don't get the code edit :-( – DrinkCoffee May 04 '22 at 19:28
  • I must have been unclear, Edit your question and copy and paste your `Relay1.sh` script INTO your question, either indented by 4-spaces so it formats as fixed-text or place `\`\`\`bash` on the line above it and `\`\`\`` on the line below it to accomplish the same thing. – David C. Rankin May 04 '22 at 21:31

1 Answers1

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:

that other guy
  • 116,971
  • 11
  • 170
  • 194
  • I missed a bit out of the .sh when I copied it to the comment. I am using #!/bin/bash at the start of the .sh file. – DrinkCoffee May 04 '22 at 20:22