Vijay
Vijay

Reputation: 1

How to pass local variable to remote using ssh and bash script

I have two servers A and B(remote server) ,need to copy files from A to B remote server . before copy i need to remove the files in the remote server depends on my input variable from local server .

For copy command

scp -r file test@host:/home/test/$name1/$name2.pdf

here $name1 and $name2 are the variables getting from local machine , values will vary

above command works fine

For move command

if [ "$name1" = "RAM" ]
then
    ssh test@host 'mv /home/test/$name1/*.pdf /home/test/$name1/backup'

this is not working

can you please suggest me how to archive this.

Upvotes: 0

Views: 392

Answers (1)

cdarke
cdarke

Reputation: 44434

You problem is quoting:

if [ "$name1" = "RAM" ]
then
    ssh test@host "mv /home/test/$name1/*.pdf /home/test/$name1/backup"
fi

That is, replace the single quotes with double quotes.

Double quotes allows variable expansion whereas single quotes do not allow any expansion. Note that neither quotes expands the wildcards, so they will be expanded on the remote machine (presumably that's what you need).

If you use double [[ ]] then you don't need to quote the condition variable (even if it contains embedded whitespace):

if [[ $name1 == RAM ]]
then
    ssh test@host "mv /home/test/$name1/*.pdf /home/test/$name1/backup"
fi

Upvotes: 3

Related Questions