user1856100
user1856100

Reputation: 75

Want to read variable value from remote file

In one of my bash script I want to read and use the variable value from other script which is on remote machine. How should I go ahead to resolve this. Any related info would be helpful. Thanks in advance!

Upvotes: 2

Views: 2214

Answers (3)

Harald
Harald

Reputation: 5113

How about this (which is code I cannot currently test myself):

text=$(ssh yourname@yourmachine 'grep uploadRate= /root/yourscript')

It assumes that the value of the variable is contained in one line. The variable text now contains you variable assignment, presumably something like

uploadRate=1MB/s

There are several ways to convert the text/code into a real variable assignment in your current script, like evaluating the string or using grep. I would recommend

uploadRate=${text#*=}

to just remove the part up and including the =.

Edit: One more caveat to mention is that this only works if the original assignment does not contain variable references itself like in

uploadRate=1000*${kB}/s

Upvotes: 3

MLSC
MLSC

Reputation: 5972

I would tell two ways at least:

1) You can simply redirect output to a file from remote server to your system with scp command...It would work for you.Then your script on your machine should read that file as an argument...

script on your machine:

read -t 50 -p "Waiting for argumet: " $1

It waits for output from remote machine, Then you can

sshpass -p<password> scp user@host:/Path/to/file /path/to/script/

What you need to do:

You should tell the script from your machine, that the output from scp command is the argument($1)

2)Run script from your machine:

#!/bin/bash

script='
#Your commands
'

sshpass -p<password> ssh user@host $script

And you have also another ways to run script to do sth with remote machine.

Upvotes: 0

SzG
SzG

Reputation: 12629

ssh user@machine 'command'

will print the standard output of the remote command.

Upvotes: 0

Related Questions