run command via ssh - problem

Cameron Simpson cs at zip.com.au
Wed Nov 4 21:02:11 UTC 2009


On 04Nov2009 14:56, Dan Track <dan.track at gmail.com> wrote:
| On Wed, Nov 4, 2009 at 2:29 PM, Bryn M. Reeves <bmr at redhat.com> wrote:
| >> for i in server1 server2;do ssh root@$i "DNSNAME=$(basename
| >> $(hostname)$);echo $DNSNAME";done
| >
| > You need to use single quotes instead of double quotes - see the rules
| > in the bash man page about quote expansion. A single quoted string is
| > not subject to any expansion by the shell on the client machine but a
| > double quoted string will be expanded on the client before the ssh
| > command is executed.
| >
| > $ ssh abox 'DNSNAME=$(basename $(hostname));echo $DNSNAME'
| > abox.example.com
| >
| > I still don't think that basename will do what you want here...
| 
| Great that single quote worked. It's nwo returning the correct result.
| Many thanks. The basename command works well.

I'm echoing from both Todd and Bryn's remarks about basename being useless
here (try replacing $(basename $(hostname)) with just $(hostname) and see the
script do exactly the sam thing).

However, I want to point out that because ssh hands its
arguments-joined-together-with-spaces to the shell at the far end,
you can do a lot of testing like this:

  sh -c 'some shell command...'

and when happy, run it remotely thus:

  ssh remote-host 'some shell command...'

Also, to see how it is looking while debugging, you can do this:

  ( set -x
    sh -c 'some shell command...'
  )

or on one line if you're command line editing:

  ( set -x; sh -c 'some shell command...' )

to see how the 'some shell command...' bit is prepared (since in your
real world example that's got double quotes and stuff), and you can also
go:

  sh -xc 'some shell command...'

to see exactly what the shell is doing with the string you're giving it.

All of this before throwing over the net with ssh.

Cheers,
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

No man should escape our universities without knowing how little he knows.
- J. Robert Oppenheimer




More information about the users mailing list