我试图创建一个.sh文件写入串行usb继电器…在运行Rasbian的树莓派上如果在命令提示符中运行以下行,它将正确地设置继电器的开/关:
pi@raspberrypi:~/SerialRelay $ echo -en 'xa0x01x01xa2' | 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 'xa0x01x00xa1' | 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
但是,如果我将它添加到.sh文件中并运行,它无法通过输出行设置继电器的开关:
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 $
我在。sh上运行了chmod 777
和ldconfig
,我还尝试用printf
而不是echo -en
命令修改。sh文件。当从命令行运行时,echo -en
或printf
都可以正常工作。
请任何人提供帮助。
echo -ne
和printf 'x..'
不是可移植的结构,因此在shell之间的工作方式不同。您在Bash中测试代码,但在Dash中运行脚本。
- 使用Bash运行脚本,详见为什么我的Bash代码在使用'sh'运行时失败?
- 使用
printf
八进制转义可移植地重写它,例如printf '240 01 01242'