Get local variable from ssh call in shell

I have a script called global.sh which copies a shell script called my_local_script.sh on a remote machine.

scp my_local_script.sh host@user_a:remote_directory

Then i connect to that ssh host and execute it like this

ssh host@user "
                cd remote_directory
                ./my_local_script.sh
              "

So far so good and everything is ok, the problem is that i want to get a local variable inside my_local_script.sh to global.sh. I tried doing this

ssh host@user "
                cd remote_directory
                my_arg=$(./my_local_script.sh)
              "

But this does not work and the even the call of my local script does not work properly anymore. Any suggestions how can i overcome this issue.

PS : Copying the variable to a temporary file is not an option. I am looking for a shell solution for this if possible.

Answers


You are using a command substitution, which is necessary, but it is in the wrong place. You should place it around the entire SSH command:

my_var=$(ssh host@user "cd remote_directory; ./my_local_script.sh")

This is assuming that the variable you want to capture is being outputted by my_local_script eg by echo or printf.


Need Your Help

How to call a javascript function when iframe finished loading?

javascript html iframe

Could any one tell me how I can detect if an iframe has finished loading so I can call a javascript function and alert the user that iframe finished loading and do some other process inside the