Reputation: 3
In my perl scripts, this code:
system("ssh -q ullink\@130.45.56.217 \"echo 1 2|awk '{print \$2}'\"");
The awk part just doesn't work! the expect result is "1", but now it's "1 2" I just can't figure out how to make it work?
Upvotes: 0
Views: 551
Reputation: 189487
Why would you run awk
on the remote host, or at all?
system(qq(ssh -q $UL_SERVER_LOGIN\@$UL_SERVER echo 1 2 | awk '{print $2}'));
Or even better
print ((split (/\s+/,qx(ssh -q $UL_SERVER_LOGIN\@$UL_SERVER echo 1 2)))[1]);
The concrete problem with your script is that the dollar sign requires a lot more escapes: it gets eaten by the remote shell; but if you escape it from the remote shell with a backslash, the backslash needs to be escaped from Perl, etc etc. It's a lot simpler if you use single quotes where you can.
system("ssh -q $UL_SERVER_LOGIN\@$UL_SERVER " .
'"echo 1 2 | awk \'{ print \$2 }\'"');
Upvotes: 0
Reputation:
awk
, whether you intend to run it on the remote host or locally, is producing output that isn't going anywhere. system()
does not give you the output of the command you run, only the return status.
Update: yes, a command run by system can still print to STDOUT, though.
You need backquotes instead:
my @output = `command here`;
print @output;
Also, keep in mind that Perl can do pretty much anything awk
can. I would prefer to do as much processing as possible in Perl, and keep the external system commands to a minimum. But this depends on what you are doing and is a personal preference to some extent.
Upvotes: 2