Vince
Vince

Reputation: 1527

Perls system($command) returns other result than command on shell

I'm trying to fetch a version number from an xml-file on a remote machine. I do this via the Net::SSH::Perl cmd function. It looks something like this:

my ($version, $err, $exit) = $ssh->cmd("head -11 /some/path/to/the/file.xml | tail -1 | sed 's/<[^>]\+>//g' | xargs");
print Dumper $version;

What I'm trying to achieve with that is, to extract the number out of an XML-tag <version>2.6</version>

It works perfectly fine, when I use the cmd on a ssh-shell via PuTTy

user@remotemachine:~>head -11 /some/path/to/the/file.xml | tail -1 | sed 's/<[^>]\+>//g' | xargs
2.6
user@remotemachine:~>

However, Perl prints

$VAR1 = '<version>2.6</version>
';

Any ideas, why it's not working?

Edit: Obviously it has nothing to do with the Net::SSH::Perl-module, since

perl -e "system(\"head -11 /some/path/to/the/file.xml | tail -1 | sed 's/<[^>]\+>//g' | xargs\");"

Also prints

<version>2.6</version>

Upvotes: 0

Views: 66

Answers (1)

choroba
choroba

Reputation: 241758

You are using double quotes. In double quotes, \ is special, so only + instead of \+ is passed to sed.

You can use the q() operator to avoid backslashing the backslash:

$ssh->cmd(q(head -11 /some/path/to/the/file.xml | tail -1 | sed 's/<[^>]\+>//g' | xargs));

Upvotes: 3

Related Questions