Problems with remotely executing scripts

Hi,

in the below command, i export a value to a variable which later is used by the script, however i dont see the exported value is actually been exported.

ssh user@host "export var=/path/ ; cd /path/ ; ./script"

how can i use the above command with proper value of var remotley

It works for me...

cat run
 
#!/bin/bash
echo "My path is : $mypath"
ssh localhost "export mypath=/home2/aen/; cd /home2/aen/; ./run"
My path is : /home2/aen/

Check the usage of the variable in the script.

--ahamed

You have to make your shell send a literal $ to the other end to use variables. That means putting things in single quotes, where the shell isn't allowed to substitute by itself.

One layer of quotes gets stripped out when you run the shell command, letting it substitute properly on the other side.

ssh user@host 'export var=/path/ ; cd "$var" ; ./script'
1 Like

To illustrate the relative correct execution of ahamed:

ant:/home/vbe/z01 $ ssh an12 "export mypath=/etc/; cd $mypath; /home/vbe/run"
vbe@an12's password: 


I am now here :  /home/vbe
My path is : /etc/
ant:/home/vbe/z01 $ ssh an12 "export mypath=/etc/; cd $"mypath"; /home/vbe/run"
vbe@an12's password: 


I am now here :  /etc
My path is : /etc/
ant:/home/vbe/z01 $ 

Forgot run code...

an12:/home/vbe $ more run
#!/bin/ksh
echo "I am now here : " $(pwd)
echo "My path is : $mypath"
run: END

Its still not working for me what i am doing is

user1@host1: ->ssh user@host "export DIR=/trumpet/uatuse1/uat/ogs/QUANTUM/NXT/data ; echo $DIR"

user1@host1: ->

Try this and see if you are getting any output...

ssh user@host "echo 123"

--ahamed

i get this

user1@host1 ->ssh user@host "echo 123"
123
user1@host1 ->

You are not sending a literal $ to the other end. Having put everything in double quotes instead of single quotes, $DIR gets substituted before it gets sent to the server, causing it to receive 'echo' with no arguments.

1 Like