简体   繁体   中英

ssh and environment variables remote and local

I need to use a remote variable and a local variable in the same remote ssh command

export CASSANDRA_DIR=/opt/cassandra

ssh root@sdi-prod-02 <<\EOF
  export READ=$(grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= '{print $2}')
  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

ssh root@sdi-prod-02 echo "directory=$CASSANDRA_DIR"

thanks, Dean

What should be expanded locally, keep the sigil $ as is, like $foobar

What you want to be expanded remotely, you may use backslashes : \\$foobar

By default in here-docs , the variable are expanded.

Ex. :

cat<< EOF
$UID
EOF

To avoid expanding in here-doc , you can use this form :

cat<< 'EOF'
$variable_that_will_not_been_expanded
EOF

or yours :

cat<< \EOF
$variable_that_will_not_been_expanded
EOF

both works.

If you didn't want to use a here-doc you can do it this way:

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\"
"

My final result is thus which works great (notice I cahnge \\EOF to EOF instead!!!!! and then escape the remote variables

export CASSANDRA_DIR=/opt/cassandra

ssh root@sdi-prod-02 <<EOF
  export READ=$(grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= '{print $2}')
  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.

Dean

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM