bash ssh 和环境变量远程和本地
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17578073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ssh and environment variables remote and local
提问by Dean Hiller
I need to use a remote variable and a local variable in the same remote ssh command
我需要在同一个远程 ssh 命令中使用远程变量和本地变量
export CASSANDRA_DIR=/opt/cassandra
ssh root@sdi-prod-02 <<\EOF
export READ=$(grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= '{print }')
echo "listen_address: $READ" to directory "$CASSANDRA_DIR"
EOF
The $READ variable is working just fine while the CASSANDRA_DIR is not working. The following does work for CASSANDRA_DIR
$READ 变量工作正常,而 CASSANDRA_DIR 不工作。以下确实适用于 CASSANDRA_DIR
ssh root@sdi-prod-02 echo "directory=$CASSANDRA_DIR"
thanks, Dean
谢谢,院长
回答by Gilles Quenot
What should be expanded locally, keep the sigil $as is, like $foobar
应该在本地扩展什么,保持印记$原样,比如$foobar
What you want to be expanded remotely, you may use backslashes : \$foobar
你想远程扩展的,你可以使用反斜杠: \$foobar
By default in here-docs, the variable are expanded.
默认情况下,在here-docs 中,变量被扩展。
Ex. :
前任。:
cat<< EOF
$UID
EOF
To avoid expanding in here-doc, you can use this form :
为避免在here-doc 中扩展,您可以使用以下形式:
cat<< 'EOF'
$variable_that_will_not_been_expanded
EOF
or yours :
或你的:
cat<< \EOF
$variable_that_will_not_been_expanded
EOF
both works.
两者都有效。
回答by hax0r_n_code
If you didn't want to use a here-doc you can do it this way:
如果您不想使用 here-doc,您可以这样做:
export CASSANDRA_DIR=/opt/cassandra
ssh root@sdi-prod-02 "
export READ=$(grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= \'{print $2}\')
echo \"listen_address: $READ\" to directory \"$CASSANDRA_DIR\"
"
回答by Dean Hiller
My final result is thus which works great (notice I cahnge \EOF to EOF instead!!!!! and then escape the remote variables
我的最终结果因此效果很好(注意我将 \EOF 改为 EOF !!!!!然后转义远程变量
export CASSANDRA_DIR=/opt/cassandra
ssh root@sdi-prod-02 <<EOF
export READ=$(grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= '{print }')
echo "listen_address: $READ to directory $CASSANDRA_DIR"
EOF
IT all works great in that READ is generated remotely and CASSANDRA_DIR is the var on my original machine.
这一切都很好,因为 READ 是远程生成的,而 CASSANDRA_DIR 是我原始机器上的 var。
Dean
院长

