【发布时间】:2017-08-23 10:08:14
【问题描述】:
我有一个heredoc,需要从主脚本中调用现有变量,并设置自己的变量以供以后使用。像这样的:
count=0
ssh $other_host <<ENDSSH
if [[ "${count}" == "0" ]]; then
output="string1"
else
output="string2"
fi
echo output
ENDSSH
这不起作用,因为“输出”没有设置为任何值。
我尝试使用这个问题的解决方案:
count=0
ssh $other_host << \ENDSSH
if [[ "${count}" == "0" ]]; then
output="string1"
else
output="string2"
fi
echo output
ENDSSH
它也没有工作。 $output 设置为“string2”,因为 $count 没有展开。
如何使用从父脚本扩展变量的heredoc,并且设置自己的变量?
【问题讨论】:
-
它的行为符合预期。 heredoc 中的代码在远程主机上运行,它没有看到
count=0初始化。 -
有没有办法将变量(和其他几个)传递到heredoc执行中?
-
没有“heredoc 执行”。 heredoc 定义了一个字符串。字符串被传递给 ssh,由 shell 评估。