问题描述
我们有通常在unix环境中执行的脚本.以下是脚本中的一行:
We have script which is normally executed within a unix environment.The following is a line from the script:
command => 'use/bin/tail -n 1 %{path} | grep --silent -F "%{message}" && rm -f {path}'
在PowerShell中运行时,显然无法识别use/bin/tail
路径.作为替代方案,我尝试了以下方法:
When run within PowerShell, the use/bin/tail
path is obviously not recognized. As an alternative I tried this:
command => 'Get-Content -n 1 %{path} -Tail | grep --silent -F "%{message}" && rm -f {path}'
但是,虽然从PowerShell命令行本身运行时可以识别Get-Content
,但是从脚本内部运行时却不能识别.这是错误消息:
However, while Get-Content
is recognized when run from the PowerShell command line itself, it is not recognized when run from within the script.This is the error message:
在PowerShell中运行脚本时,复制unix tail
命令的正确方法是什么?
What is the correct way to replicate the unix tail
command when running a script in PowerShell?
推荐答案
您收到的错误:
%1 is not recognized as an internal or external command, operable program or batch file.
是来自cmd.exe
而不是powershell.exe
的标准错误:
is a standard error from cmd.exe
, not powershell.exe
:
您的命令不是由Powershell主机执行的,而是由常规命令提示符执行的-PowerShell将提到在找不到命令"的情况下调用"cmdlet,函数,脚本文件或可运行程序"的可能性(故意拼写错误的Get-Content来演示):
Your command is not getting executed by a powershell host, but by a regular command prompt - PowerShell would have mentioned the possibility of invoking a "cmdlet, function, script file or operable program" in case of "command not found" (misspelled Get-Content on purpose to demonstrate):
您还需要多一点编辑命令,为了使PowerShell能够理解它,管道中的第一条语句将是:
You'll also need to edit the command a little more, for PowerShell to make sense of it, the first statement in the pipeline would be:
Get-Content $Path -Tail 1
假定$Path
包含要尾随的文件的路径.
assuming that $Path
contains the path to the file you're tailing.
这篇关于无法识别Get-Content命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!