为什么这个 here-doc bash 语句不会在 switch case 中执行
Why won't this here-doc bash statement execute in a switch case
我正在为树莓派开发引导脚本。此脚本确定 Pi 本身是 Model 2 还是 Model 3 并相应地设置其 WiFi 特性。
WiFi 特性的变化放在 /etc/rc.local
文件中 (Raspbian),这是通过 bootstrap.sh
脚本完成的.
片段
# model revision number to determine pi
PI_MODEL=$(cat /proc/cpuinfo | grep "Revision" | awk '{print }')
case "$PI_MODEL" in
"rev_1A" | "rev_1B")
# Write the wlan config to the rc.local file
cp /etc/rc.local /etc/rc.local.backup
(
cat << 'EOF'
#!/bin/sh -e
iwconfig wlan0 mode ad-hoc essid pi-adhoc channel 6 txpower 0
exit 0
EOF
) > /etc/rc.local
# Case for Pi-2 ends
;;
"pi3_rev1a" | "pi3_rev1b")
# write the wlan config to rc.local file
(
cat << 'EOF'
#!/bin/sh -e
ifconfig wlan0 down
iwconfig wlan0 mode ad-hoc channel 6 essid pi-adhoc txpower 0
ifconfig wlan0 up
exit 0
EOF
) > /etc/rc.local
;;
# case for Pi ends here
esac
但是无论如何都会发出警告:
warning: here-document at line .. delimited by end-of-file (wanted `EOF')
syntax error: unexpected end of file
这里可能出了什么问题?
主要想法是检查 Pi 的类型,然后将相应的 iwconfig
语句添加到 /etc/rc.local
文件,以便它在重新启动时加入网络。
备注:
在实际情况中,这只是一个更大的引导脚本的片段,为简洁起见,此处未显示
rev_1A
等为了简洁就不写了
参考文献:
由于您的此处文档是缩进的,因此您应该使用 -
形式,以便删除前导标签。您还必须在第 0 列具有结束标记,除非您使用制表符进行缩进:
cat <<-'EOF'
#!/bin/sh -e
iwconfig wlan0 mode ad-hoc essid pi-adhoc channel 6 txpower 0
exit 0
EOF
If the redirection operator is ‘<<-’, then all leading tab characters
are stripped from input lines and the line containing delimiter. This
allows here-documents within shell scripts to be indented in a natural
fashion.
解决"syntax error: unexpected end of file":
而不是:
(
cat << 'EOF'
#code
EOF
) > /etc/rc.local
你想要:
cat << EOF > /etc/rc.local
#code
EOF
如果你想缩进你的代码(让它看起来漂亮,或其他)包括 EOF
语句,你必须添加一个 -
忽略前导标签,像这样:
cat <<- EOF > /etc/rc.local
#code
EOF
我正在为树莓派开发引导脚本。此脚本确定 Pi 本身是 Model 2 还是 Model 3 并相应地设置其 WiFi 特性。
WiFi 特性的变化放在 /etc/rc.local
文件中 (Raspbian),这是通过 bootstrap.sh
脚本完成的.
片段
# model revision number to determine pi
PI_MODEL=$(cat /proc/cpuinfo | grep "Revision" | awk '{print }')
case "$PI_MODEL" in
"rev_1A" | "rev_1B")
# Write the wlan config to the rc.local file
cp /etc/rc.local /etc/rc.local.backup
(
cat << 'EOF'
#!/bin/sh -e
iwconfig wlan0 mode ad-hoc essid pi-adhoc channel 6 txpower 0
exit 0
EOF
) > /etc/rc.local
# Case for Pi-2 ends
;;
"pi3_rev1a" | "pi3_rev1b")
# write the wlan config to rc.local file
(
cat << 'EOF'
#!/bin/sh -e
ifconfig wlan0 down
iwconfig wlan0 mode ad-hoc channel 6 essid pi-adhoc txpower 0
ifconfig wlan0 up
exit 0
EOF
) > /etc/rc.local
;;
# case for Pi ends here
esac
但是无论如何都会发出警告:
warning: here-document at line .. delimited by end-of-file (wanted `EOF')
syntax error: unexpected end of file
这里可能出了什么问题?
主要想法是检查 Pi 的类型,然后将相应的 iwconfig
语句添加到 /etc/rc.local
文件,以便它在重新启动时加入网络。
备注:
在实际情况中,这只是一个更大的引导脚本的片段,为简洁起见,此处未显示
rev_1A
等为了简洁就不写了
参考文献:
由于您的此处文档是缩进的,因此您应该使用 -
形式,以便删除前导标签。您还必须在第 0 列具有结束标记,除非您使用制表符进行缩进:
cat <<-'EOF'
#!/bin/sh -e
iwconfig wlan0 mode ad-hoc essid pi-adhoc channel 6 txpower 0
exit 0
EOF
If the redirection operator is ‘<<-’, then all leading tab characters are stripped from input lines and the line containing delimiter. This allows here-documents within shell scripts to be indented in a natural fashion.
解决"syntax error: unexpected end of file":
而不是:
(
cat << 'EOF'
#code
EOF
) > /etc/rc.local
你想要:
cat << EOF > /etc/rc.local
#code
EOF
如果你想缩进你的代码(让它看起来漂亮,或其他)包括 EOF
语句,你必须添加一个 -
忽略前导标签,像这样:
cat <<- EOF > /etc/rc.local
#code
EOF