剧本:
#!/bin/bash
interface=enp4s0
mac_address=$(ip link show $interface | grep ether | awk '{print $2}')
cat > /dev/usb/lp0 <<EOF
SIZE 30 mm,90 mm,CLS,TEXT 200,40,0,90,2,2,"MAC Address of $interface:",TEXT 140,40,0,90,2,2,"$mac_address",PRINT 1,END
>EOF
我正在使用 TSPL 语言打印到字符设备。问题是要打印的内容必须用双引号引起来。例如TEXT 200,40,0,90,2,2,"Some text to be printed"
。我还想在文本中嵌入一个变量。
echo "SIZE 30 mm,90 mm,CLS,TEXT 200,40,0,90,2,2,"MAC Address of $interface:",TEXT 140,40,0,90,2,2,"$mac_address",PRINT 1,END > /dev/usb/lp0
不起作用,因为双引号嵌套破坏了 TSPL 的命令。
我在heredoc中遇到的错误是
warning:here-document at line 8 delimited by end-of-file (wanted `EOF')
答案1
正如 @thrig 所建议的,更改>EOF
为EOF
可以快速解决您的问题:
cat > /dev/usb/lp0 <<EOF
SIZE 30 mm,90 mm,CLS,TEXT 200,40,0,90,2,2,"MAC Address of $interface:",TEXT 140,40,0,90,2,2,"$mac_address",PRINT 1,END
EOF
我建议将来在具有良好语法突出显示的 IDE 中编写 bash 脚本,VS Code 很快就注意到了这一差异。干杯!