Suri
Suri

Reputation: 1

Runnig commands on remote unix server

I am working on a shell script. It is used to login to another remote server check for the existance of a file and it it doesnt it creates and if it does nothing happens.

I am trying to do this by, ssh -i private_key server_name 'script to create new file'

After spending a long time thinking why it is not happening, it stuck to mind that the variable from where i am doing ssh will not be available in the server to which it goes. So I have to pass a variable. But I couldnt do so. But the logon part is smooth.

Please help.

Upvotes: 0

Views: 84

Answers (2)

glenn jackman
glenn jackman

Reputation: 246774

If it's OK to update modification time of the file:

ssh -i keyfile server "touch '$newfile'"

otherwise

ssh -i keyfile server "[ -f '$newfile' ] && touch '$newfile'"

Those should work assuming your login shell is a Bourne-type shell.

Upvotes: 1

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798556

Use double quotes, or switch up the quotes if you need single quotes for some parts.

"foo $bar baz"
'foo '"$bar"' baz'

Upvotes: 0

Related Questions