0

在我的 perl 脚本中,这段代码:

system("ssh -q ullink\@130.45.56.217 \"echo 1 2|awk '{print \$2}'\"");

awk 部分不起作用!预期结果是“1”,但现在是“1 2”我只是不知道如何让它工作?

4

2 回答 2

2

awk,无论您打算在远程主机上还是在本地运行它,都会产生不会去任何地方的输出。 system()不给你运行命令的输出,只给你返回状态。

更新:是的,系统运行的命令仍然可以打印到 STDOUT。

您需要反引号:

 my @output = `command here`;
 print @output;

另外,请记住 Perl 几乎可以做任何事情awk。我更愿意在 Perl 中进行尽可能多的处理,并将外部系统命令保持在最低限度。但这取决于您在做什么,并且在某种程度上是个人喜好。

于 2013-04-19T07:36:38.087 回答
0

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 }\'"');
于 2013-04-19T07:32:05.280 回答