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

4
  • What do you mean by "global variable"? You are talking about sh-shell here, and for what I know, this does not 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. Commented May 15, 2020 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. Commented May 15, 2020 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 && ... Commented May 15, 2020 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. Commented May 15, 2020 at 14:38

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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.
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.