1

I have two scripts that are being run. One runs from a client workstation and calls a script on a server. I need a variable from the server script and I figured I could do it this way, but it isn't working:

#!/bin/sh

local_var=""

ssh user@server " $local_var=\$server_var "

echo "$local_var"

My output of local_var is null. Is there a way to do this?

3 Answers 3

3

Try:

local_var=$(ssh user@server "echo ${server_var}")
Sign up to request clarification or add additional context in comments.

4 Comments

I'm still testing this, but I think it may be the solution
I'm having no luck with this. I tried typing it exactly the way you have it, but my variable still comes up null. I also tried adding a space like this: local_var=$( ssh user@server "echo ${server_var}" )
Are you sure that server_var is set on the remote system?
yes sir, it's used heavily throughout the server script (which works)
2

yes, you need to print the variable names and values, then source the output of that. An example:

$ source <(ssh user@server 'echo foo=\"$bar\"')
$ echo $foo
bar

This works on bash, it may not work on shells which does not support redirecting command output to source. For those shells, you may need to create a temporary file first.

1 Comment

Getting an error on this... "syntax error near unexpected token `(' ..."
1

I'm going to make 3 assumptions:

  1. The remote machine is a Linux box.
  2. You know the process ID of the remote script's instance.
  3. The variable you want is in the remote process's environment.

Assuming the above, the following should work:

local_var=$( ssh user@server bash -c '
   while IFS="=" read -d '' -r name value; do
     [[ $name = "server_var" ]] && echo $server_var && break
   done' < /proc/$REMOTE_SCRIPT_PROC_ID/environ )

Comments

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.