1

I am trying to execute a function via SSH. I am able to do it after following this post.

But if I have some global/environmental variable in the shell script those are not expanded. Here is the source code of the file to SSH node is not an option since its like a local copy.

Ex i tried:

#!/bin/bash

SKS="TYPO"

fun2(){

    arg=$1
    echo $arg >> /tmp/a.o
    echo $SKS >> /tmp/a.o
}

fun1() {

    echo "SKSKSKSKS" > /tmp/a.o
    fun2 "SPSP"

}

fun1

SSH="ssh -4q -o StrictHostKeyChecking=no -o ConnectTimeout=10"
$SSH <ssh-host> "$(typeset -f); fun1"

When I executed this script on the local node it gave me output as:

cat /tmp/a.o 
SKSKSKSKS
SPSP
TYPO

but on the SSHed node its:

SKSKSKSKS
SPSP

So here $SKS is not expanded on the SSHed node.

Please help me to resolve this.

Regards, Shridhar

Community
  • 1
  • 1

2 Answers2

1

You aren't setting or exporting the local $SKS variable to the remote side. typeset -f expands functions so you get the functions on the remote side but not variables.

If you want the variable available on the remote side you either need to assign that in the remote-executed shell script also or pre-expand that in the function on the local side somehow.

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • How shall i extract all environmental variables over ssh? Is there any method or a tweak? – Shridhar Hegde Mar 16 '15 at 13:20
  • The `typeset` command can expand any variables you want (the way Ondrej indicated) just list the ones you want (or drop `-f`) and you get variables and functions. Note well, however, that there are many variables you likely *do not* want expanded on the remote side that are set on the local side so you might want to limit to exported to exported variables (as those should generally be safer) and use `typeset -x`. – Etan Reisner Mar 16 '15 at 13:22
0

Try adding typeset -p SKS to SSH command:

$SSH <ssh-host> "$(typeset -f; typeset -p SKS); fun1"
Ondrej
  • 101
  • 5