0

我有一个 Perl 脚本,我想在其中使用 system() 来调用一些 shell 程序,并可能即时对程序的输出进行后处理。

在 powershell 中,我可以运行更多、tee、grep 和其他东西,而无需指定可执行文件的完整路径。我不明白为什么以下失败

C:\Users\xxxwork> perl -W -e "system 'more foo.log | tee  bar.txt'"
'tee' is not recognized as an internal or external command,
operable program or batch file.

虽然以下工作, C:\Users\xxxwork> perl -W -e "system 'more foo.log | grep -e failed'"

直接调用 tee 工作

C:\Users\xxxwork> tee
cmdlet Tee-Object at command pipeline position 1
Supply values for the following parameters:
FilePath:

我尝试用“Tee-Object”中的完整替换 tee,但我得到了相同的结果。在这种特殊情况下,是否需要设置任何额外的环境变量才能让 Perl 看到 shell 看到的完整 $Path?这仅仅是因为在一种情况下 grep 是一个纯应用程序,而在另一种情况下 tee 是 shell 实用程序?

PS C:\Users\xxxwork>  get-command Tee-Object

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Cmdlet          Tee-Object                                         Microsoft.PowerShell.Utility 
PS C:\Users\xxxwork> get-command grep

CommandType     Name                                               ModuleName
-----------     ----                                               ----------
Application     grep.exe

谢谢G

4

1 回答 1

1

system有两个目的。它可以用来启动一个程序:

system($program, @args_to_program)

或者它可以用来执行一个shell命令:

system($shell_command)

后者基本上相当于

system('cmd', '/x', '/c', $shell_command)

powershell /?中,我们看到可以使用-Command参数指示它执行命令,因此等价的 forpowershell将是

system('powershell', '-Command', $powershell_command)
于 2013-07-12T17:56:35.887 回答