0

Using a ksh script, I’m trying to get the value of an environment variable from a remote host and store it into a variable of my ksh script. For example, on the remote host, the value $mypath is

/dir1/dir2/dir3

So on my local host from my ksh, I was figuring I could do something like

ssh user@remotehost "echo $mypath”

But not sure what else to do after that if I want to assign it to a variable like myremotepath.

So far if I use this from my ksh script:

 ssh user@host 'ls -l'
 ssh user@host 'echo "HI"'
 ssh user@host 'echo $my_env_var'

The output is below. The ls and first echo command work, why wouldn't the 2nd echo work? I've verified my_env_var is defined on the remote host.

15:12:25 # ~/my.ksh 
total 16
-rw-r--r-- 1 user group1 40 Jul 15 15:06 myremote2.txt
-rw-r--r-- 1 user group1  1 Jul 15 13:28 myremote.txt
-rw-r--r-- 1 user group1 22 Jun 29 15:29 tempR.txt
-rw-r--r-- 1 user group1  3 Jun 29 16:11 temp.txt
HI
bloodpresage
  • 109
  • 1
  • 7
  • 1
    How are you verifying that the variable is set? – chepner Jul 15 '16 at 19:28
  • chepner, I logged into the remote host through another session and did an echo $my_env_var and it returned the expected value. – bloodpresage Jul 15 '16 at 19:53
  • It would be informative if you could clarify whether or not there is a **blank line** output *after* `HI` and *before* your next prompt. This would indicate whether or not the `echo` command ever ran. – J. Cordasco Jul 23 '16 at 23:33
  • Are you sure you are using single quotes? `ssh user@host "echo $my_env_var"` would replace the `$my_env_var` with the local variable before calling ssh. Try `my_env_var=my_local_value` before calling ssh and see what you get. – Walter A Jul 29 '16 at 10:26
  • Where is my_env_var set? Is it set by `.profile` ? Try `ssh user@host '. .profile; echo $my_env_var'` – Walter A Jul 29 '16 at 10:28
  • We found out the issue. It had to do with the host serving two different environments. – bloodpresage Aug 10 '16 at 04:37

0 Answers0