Bash脚本 - 猫EOF打破

时间:2013-04-20 20:08:17

标签: bash eof cat

我正在尝试使用cat EOF编写一个bash脚本来自动安装一个cron脚本,这部分是打破脚本而我不知道我哪里出错了。出于某种原因,脚本在将它们回显到/etc/no-ip.sh

之前运行这些命令
echo "#!/bin/sh" >> /etc/no-ip.sh
echo "HOSTNAME=hostname" >> /etc/no-ip.sh
echo "LOGFILE=no-ip-log" >> /etc/no-ip.sh
echo " " >> /etc/no-ip.sh
echo "Current_IP=$(host $HOSTNAME | cut -f4 -d' ')" >> /etc/no-ip.sh
echo " " >> /etc/no-ip.sh
echo "if [ ! -s $LOGFILE ] ; then" >> /etc/no-ip.sh
echo "echo "sshd : $Current_IP : allow" >> /etc/hosts.allow" >> /etc/no-ip.sh
echo "echo $Current_IP > $LOGFILE" >> /etc/no-ip.sh
echo "else" >> /etc/no-ip.sh
echo " " >> /etc/no-ip.sh
echo "Old_IP=`head -n 1 /etc/no-ip-log`" >> /etc/no-ip.sh
echo " " >> /etc/no-ip.sh
echo "if [ "$Current_IP" = "$Old_IP" ] ; then" >> /etc/no-ip.sh
echo "echo IP address has not changed" >> /etc/no-ip.sh
echo "else" >> /etc/no-ip.sh
echo "sed -i 's/'$Old_IP'/'$Current_IP'/' /etc/hosts.allow" >> /etc/no-ip.sh
echo "echo $Current_IP > $LOGFILE" >> /etc/no-ip.sh
echo "echo iptables have been updated" >> /etc/no-ip.sh
echo "fi" >> /etc/no-ip.sh
echo "fi" >> /etc/no-ip.sh
echo "EOF" >> /etc/no-ip.sh

1 个答案:

答案 0 :(得分:2)

双引号内的一些内容仍然会被评估,例如:

echo "Current_IP=$(host $HOSTNAME | cut -f4 -d' ')" >> /etc/no-ip.sh

此处,评估$(...)构造。所以你想在整个事情中使用单引号:

echo 'Current_IP=$(host $HOSTNAME | cut -f4 -d" ")' >> /etc/no-ip.sh

与其他系列类似。


那就是说,heredoc可能是一种更好的写作方式。您需要引用分隔符字符串以使字体按字面解释:

cat <<'EOF' > /etc/no-ip.sh
...
EOF