Dean Hiller
Dean Hiller

Reputation: 20200

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

Upvotes: 5

Views: 5871

Answers (3)

Dean Hiller
Dean Hiller

Reputation: 20200

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

Upvotes: 1

nullByteMe
nullByteMe

Reputation: 6391

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

Upvotes: 1

Gilles Qu&#233;not
Gilles Qu&#233;not

Reputation: 185189

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.

Upvotes: 10

Related Questions