[英]Variable still doesn't work on remote server
我有这部分脚本,无法开始工作。 到处都在搜索,但我必须在这里丢失一些内容。
export RULE=`cat iptables_sorted.txt`
ssh hostname << EOF
for line in $RULE; do
echo \$line >> test.txt (I have tried with and without slashes, etc...)
done
exit
EOF
运行脚本的这一部分后,我得到
stdin: is not a tty
-bash: line 2: syntax error near unexpected token `103.28.148.0/24'
-bash: line 2: `103.28.148.0/24'
...这很奇怪,因为iptables_sorted.txt充满了ip范围(当我在本地运行它时,它可以工作)。
$ RULE中的换行符会导致此问题。 用空格替换它们:
RULE=$(< iptables_sorted.txt)
RULE=${RULE//$'\n'/ }
ssh hostname << EOF
for line in $RULE ; do
echo \$line >> test.txt
done
EOF
请注意,这不适用于包含空格的行。
不要for
对文件进行迭代; 使用while
。 这也演示了将循环的输出而不只是每个单独的echo
传递到远程主机。 cat
用于读取传入的数据并将其重定向到最终输出文件。
while IFS= read -r line; do
echo "$line"
done | ssh hostname 'cat > test.txt'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.