在PowerShell上重定向标准输入/输出所需的语法是什么?
在Unix上,我们使用:
$./program <input.txt >output.txt
如何在PowerShell中执行相同的任务?
发布于 2012-08-03 10:33:58
您不能将文件直接挂接到stdin,但是您仍然可以访问stdin。
Get-Content input.txt | ./program > output.txt
发布于 2012-07-12 16:30:11
对于输出重定向,您可以使用:
command > filename Redirect command output to a file (overwrite)
command >> filename APPEND into a file
command 2> filename Redirect Errors
输入重定向以不同的方式工作。例如,请参阅此Cmdlet http://technet.microsoft.com/en-us/library/ee176843.aspx
发布于 2019-09-28 02:52:27
或者你可以这样做:
类似于:
$proc = Start-Process "my.exe" "exe commandline arguments" -PassThru -wait -NoNewWindow -RedirectStandardError "path to error file" -redirectstandardinput "path to a file from where input comes"
如果您想知道进程是否出错,请添加以下代码:
$exitCode = $proc.get_ExitCode()
if ($exitCode){
$errItem = Get-Item "path to error file"
if ($errItem.length -gt 0){
$errors = Get-Content "path to error file" | Out-String
}
}
我发现这样,当你需要处理外部程序/进程时,我可以更好地处理你的脚本的执行。否则,我会遇到脚本在某些外部进程错误时挂起的情况。
https://stackoverflow.com/questions/11447598
复制相似问题