0

I have a shell script in a server which calls another shell script in another server. When I try to get the variable assigned in the 2nd server shell script am not able to get it.

Script in the first server

> ssh -o StrictHostKeyChecking=no -q oracle@$database_server_ip "cd $script_loc && . ./quick"
>echo "$value"

script in second server

> export value=my name

when I try to get the variable value from my second script which is in another server to the script which I call the second server script . I couldn't get the variable value. Need help

  • What do you mean by "global variable"? You are talking about sh-shell here, and for what I know, this [does not](https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05) have the difference between local and global variables. In bash, zsh and ksh, you can have variables which are global, and those which are local to a function, but your example does not involve any function. What you are doing is to set an **environment** variable on the server side, and of course you can't access it afterwards. – user1934428 May 15 '20 at 12:35
  • You could write the content of the variable on the server side to a file, and fetch the file from there to read the variable. Or you can write the content variable to standard output and catch the standard output and extract the value. The latter is more compliated if your script generates more information on stdout, because it means that you have to parse it. – user1934428 May 15 '20 at 12:36
  • Note that `ssh somehost "cd $location"` has *actual security vulnerabilities*. Let's say that the location was configurable, and someone set `location='$(rm -rf ~)'`. That's normally safe -- `cd "$location"` won't run `rm`, and neither will `cd $location` without the quotes (though it'll misbehave in some other ways). But `ssh somehost "cd $location"` will treat the command substitution as code and execute it. To make your commands safe, use `printf -v script_loc_q '%q' "$script_loc"`, and then `ssh host "cd $script_loc_q && ...` – Charles Duffy May 15 '20 at 14:36
  • As for `export` -- it ensures that a variable will be copied to *future child processes* of the shell it was run in. It does not make them available to *parent processes* (or processes that don't have a parent/child relationship at all), whether or not that process is on the other side of a SSH connection. – Charles Duffy May 15 '20 at 14:38

1 Answers1

0

It seems like you want to save a variable, exported from a remote script, which is invoked over an ssh session.

If you are on the same server when you export a variable, this works fine but but you can't simply export a variable on a remote shell and save it because you are working with two separate login environments.

A couple ways to do this, although I can think of a few more involved ones:

1) have ./quick on the remote print the value of the variable you want to save. You can save what's printed on the local side during the run:

mylocal=$(ssh -o StrictHostKeyChecking=no -q oracle@$database_server_ip "cd $script_loc && . ./quick")

A word of caution here - an error message or something unexpected at runtime can end up going into mylocal if a runtime error occurs. This can result in unexpected things in your variable. Often, you can check the result code of the ssh command to see if the remote exited cleanly (check the bash variable $? right after the ssh run). Usually this should be a 0 if everything ran fine which suggests you can trust the value in mylocal. If quick issues an exit 2 in it's code, $? should contain a 2 on the local side after ssh returns.

2) do some extra error checking in the quick script and handle any errors there to make sure you can debug runtime issues and only spit out the value of the variable you want to save locally (mentioned in a comment above). You would still invoke your ssh run the same way, you would just be doing some extra checking to terminate with an error code (exit 2) on purpose so you can check $? locally for errors. If $? is a 0, you know things ran fine and your local variable is valid.

Nstevens
  • 267
  • 2
  • 8
  • Remove the spaces around the `=` if you want the above to be a valid assignment. As it is, your code runs `mylocal` as a command with `=` as its first argument. No such command exits, so it would just fail with an immediate error. – Charles Duffy May 15 '20 at 14:34
  • BTW, if (1) were a responsive answer, the question would be eligible to be flagged a duplicate of [capturing ssh output as variable in bash script](https://stackoverflow.com/questions/12048906/capturing-ssh-output-as-variable-in-bash-script) – Charles Duffy May 15 '20 at 14:40