Cannot create variables via ssh on remote machine

Hello to all

Background info:
Local machine : Linux, /bin/bash
Remote machine (for the user used for ssh) : SunOs, /bin/ksh
(so we have different OS, different Shells)

My problem :
From the local host i execute

$   var=bla
$   result=$(ssh -q user@remote-machine "
> echo \"this is var: $var \"
> var2=ggg
> echo \"this is var2 : $var2 \"
> "
> ) 
$   echo "$result"                   
this is var: bla 
this is var2 :  

So, as you can see I want to open a ssh session on a remote machine and a want to shift a variable there also (in my case variable $var ). From what I know I can only do that by using the syntax : ssh user@machine " command " , so I used double quotes to describe the commands which I wanna execute via ssh. So, from my example you can see that I can shift the $var variable on the remote session, but I cannot create and/or call (?) another variable $var2 .
If I use ' ' (simple quotes) instead of " " . I can create the variable $var2 , but I cannot shift the variable $var :expressionless: .

I tried the same thing when the remote machine was Linux with /bin/sh shell for the user used for ssh. The results are the same, so I don't think it is an issue of different OS or different shells

Any ideas?

P.S. : i dont want to use workarounds such as declare $var in a file, scp the file on the remote host and source it there.

P.P.S. I cannot use rsh, remotexec.

Try this...

$   var=bla
$   result=$(ssh -q user@remote-machine "
> echo \"this is var: $var \"
> var2=ggg
> echo \"this is var2 : \$var2 \"
> "
> ) 

Reason:- Since you are setting var2 inside the ssh command, $var2 will be available only within that ssh scope
--ahamed

1 Like

To explain why that's needed, type this into your shell:

$ echo "LINE=stuff;  echo $LINE"
LINE=stuff;  echo
$

...which of course doesn't execute 'echo $LINE'. But you see the problem: ssh doesn'g even get 'echo $LINE', it gets 'echo', because the shell helpfully substitutes $LINE for you before it's even fed to the command -- in this case echo, in your case ssh...

Either single-quote it, or escape it.

$ echo 'LINE=stuff; echo $LINE'
LINE=stuff; echo $LINE

$ echo "LINE=stuff; echo \$LINE"
LINE=stuff; echo $LINE

$

Thank you very much, indeed that was the answer to my problem

I have a new problem related with the one above (we still have same background info from the 1st post) :

h3mis391_telnet=h3mis391bin
ssh -q user@station " i=h3mis391
> eval value=\$`echo \${i}_telnet`
> echo value is $value
> "
value is

So the variable $value is not set.
I've also tried the remote eval command this way:

eval value=$`echo ${i}_telnet`
eval value=$`echo \${i}_telnet`

With the same result.
So I basically want to create (while I'm in a remote ssh session) the variable value. $value should take the content of the variable h3mis391_telnet (which was set in the initial session, before ssh).But I need to call the variable h3mis391_telnet using variable $i (set after ssh). I wanna use the value of $i variable to create the name of the $h3mis391_telnet, and this name I wanna use in order to call the variable in the eval command.
The idea it;s a bit complicated but the code shouldn;t be hard to understand.

Any help regarding the suntax I use or different approach to accomplish what I want?

---------- Post updated at 05:38 AM ---------- Previous update was at 05:25 AM ----------

To be more clear, this is what I want but done in only one session , without ssh :

h3mis391_telnet=h3mis391bin
i=h3mis391
eval value=$`echo ${i}_telnet`
echo "value is $value "
value is h3mis391bin

So this way it works. The only difference is that I want

h3mis391_telnet=h3mis391bin

------------------------------------> to use ssh in this point

i=h3mis391
eval value=$`echo ${i}_telnet`
echo "value is $value "
value is h3mis391bin

---------- Post updated at 07:54 AM ---------- Previous update was at 05:38 AM ----------

Yes, I've made a mistake. It works like this :

h3mis391_telnet=h3mis391bin
ssh -q user@server " i=h3mis391
> eval value=$`echo \${i}_telnet`
>  echo value is $value
> "
value is h3mis391bin

The difference was here :

eval value=$`echo \${i}_telnet`   # good
eval value=\$`echo \${i}_telnet`  # bad

But I'm not 100 % sure why... o.O

You do know telnet is a reserved word (since its a command...) don't you?
It is maybe time to get to good practice: Use UPPERCASE for Variables! then it will not be misunderstood...

(Depending on what display I am I do not see underscores...)

Do a search, there has been a similar post not long ago... ( with PWD...)

Hello, Please disregard my previous update when I said "it work". It didn;t, so the issue is still present for me. To avoid opening a new post, I will make a short recapitulation of the issue :

Background info :
Local machine : Linux, /bin/bash
Remote machine (for the user used for ssh) : SunOs, /bin/ksh
(so we have different OS, different Shells)

aaa_1=999
ssh -q user@machine "
> i=1
> echo aaa_1 is $aaa_1
> eval value=$`echo aaa_\${i}`
> echo value is \$value
> "
aaa_1 is 999
value is

So the variable $value is not set.
I've also tried the remote eval command this way:

eval value=\$`echo aaa_\${i}`
eval value=$`echo aaa_${i}`

With the same result.
So I basically want to create (while I'm in a remote ssh session) the variable value. $value should take the content of the variable aaa_1 (which was set in the initial session, before ssh).But I need to call the variable aaa_1 using variable $i (set after ssh and which has value "1" which will be used as part of variable aaa_1 name). I wanna use the value of $i variable to create the name of the $aaa_1 , and this name I wanna use in order to call the variable in the eval command.
The idea it;s a bit complicated but the code shouldn;t be hard to understand.

Any help regarding the syntax I use or different approach to accomplish what I want?

---------- Post updated at 05:38 AM ---------- Previous update was at 05:25 AM ----------

To be more clear, this is what I want but done in only one session , without ssh :

aaa_1=999
i=1
eval value=$`echo aaa_${i}`
echo "value is $value "
value is 999

So this way it works. The only difference is that I want

aaa_1=999

------------------------------------> to use ssh in this point

i=1
eval value=$`echo aaa_${i}`
echo "value is $value "
value is 999

@vbe (moderator) please read carefully my post before answering/suggesting something

THX,

You don't need an escape for i...

[root@bt] ssh localhost "
i=1
echo aaa_1 is $aaa_1
eval value=`echo \$aaa_${i}`
echo value is $value
"
aaa_1 is 999
value is 999

echo statement is redundant! eval value=\$aaa_${i}; also will work.

HTH
--ahamed

Nope... it didn;t work :

aaa=999
ssh -q user@remotehost "
> i=1
> echo aaa_1 is $aaa_1
> eval value=`echo \$aaa_${i}`
> echo value is $value
> "
aaa_1 is 999
value is

Nor this way :

aaa=999
ssh -q user@remotehost "
> i=1
> echo aaa_1 is $aaa_1
> eval value=\$aaa_${i}
> echo value is $value
> "
aaa_1 is 999
value is

Are you sure you got the posted results? could be be a shell discrepancy? as I said in background info I have different shells on local host and remote host. What's the situation on your side? what shells do you use ? I know that command lines are parsed differently by different shells.

---------- Post updated at 08:22 AM ---------- Previous update was at 08:09 AM ----------

btw: make sure you didn;t set the variable $value in the current shell. If you slipped by mistake the line
value=999
somwhere before the ssh , the results are wrong, because the value of $values is carried via ssh .
I've made that mistake before when I thought my problem is solved. :expressionless:

To get this right: you want to assign the content of a variable that is set on your localhost to another variable on a remote host and want to determine which local variable to use during the ssh-session? Sounds like bad design to me, but if that's your goal you need to transfer your variables to the remote host first.
Under the assumption that the possible variables to choose all start with aaa_ you could do it like this:

myset=$(set |grep "aaa_")
ssh -q user@remotehost "$myset; i=1; eval value='$'\$(echo aaa_\${i}); echo \$value"

The myset variable does the transfer. set returns the variable in reuseable form so $myset evaluates into all relevant variable-definitions.
Btw, if you like the backquote-notation better than the $()-one you have to escape the backquotes or command subsitution on your local host takes place.

1 Like

Try this...

root@bt:~# ssh localhost "
i=1
echo aaa_1 is $aaa_1
eval value=\$aaa_$i
echo value is $value
"
aaa_1 is 999
value is 999

The above code was running on solaris... weird the same is not working on Linux...

--ahamed

Hello and many thanks. You craked it :smiley: . it works perfectly for me with one small adjustment :
instead of

ssh -q user@remotehost "$myset; i=1; eval value='$'\$(echo aaa_\${i}); echo \$value"

I think it should be

ssh -q user@remotehost "eval $myset; i=1; eval value='$'\$(echo aaa_\${i}); echo \$value"

Indeed this is also a matter of shifting variables through ssh. I use it like this :
I have a config file on the local machine (with lots of defined variables e.g var1=88 ; var2=77 etc. ) I load the content of this file in a variable and then evaluate the content of this variables immediately after ssh (basically is exactly what you suggested but i don't use set | grep , I use a config file instead ).
I think of it as an elegant way to configure lots of things in one file on a single local machine. then to do lots of actions on many remote machines using the parts of config that you need pending by the conditions you find on each remote machine / situation.
do you suggest a general idea of a different approach?
Basically what I'm trying to do is a script to perform lots of network checks on and between many machines from one flow.