“ <”运算符保留以备将来使用

我正在使用 PowerShell 并尝试运行以下命令:

.\test_cfdp.exe < test.full | tee test.log

Full 是一个模拟 test _ cfdp.exe 命令行输入的脚本:

The '<' operator is reserved for future use.

还有其他方法(例如 cmdlet)可以让这个命令在 PowerShell 中工作吗?

110411 次浏览

This was not supported in PowerShell v1 [and as of v5, it's still not...]

An example workaround is:

Get-Content test.full | .\test_cfdp.exe | tee test.log

Also try:

cmd /c '.\test_cfdp.exe < test.full | tee test.log'

If you want to run this command more times, you can just make a *.bat file with the original syntax. That's another solution.

In version 7 of PowerShell, you still need to use Get-Content to get the contents of an item in the specified location. For example, if you want to load a file into a Python script and write the result to a file. Use this construct:

PS > Get-Content input.txt | python .\skript.py > output.txt

Or with displayed and saved in a file:

PS > Get-Content input.txt | python .\skript.py | tee output.txt

Or switch to cmd to use the '<' operator:

C:\>python .\skript.py < input.txt > output.txt

In case PowerShell is not mandatory , running the command in Command Prompt works fine.

Because I dev on windows and deploy on linux I have created this powershell function. The solutions above where not appropriate because the binary file I had to restore. The knowledge of the bash-script is borrowed from: How to invoke bash, run commands inside the new shell, and then give control back to user?

$globalOS = "linux" #windows #linux


function ExecuteCommand($command) {
if($command -like '*<*') {
#Workaround for < in Powershell. That is reserved 'for future use'
if ($globalOS -eq "windows") {
& cmd.exe /c $command
} else {
$wrappercommand = "''" + $command + " ; bash''"
& bash -c $wrappercommand
}
} else {
Invoke-Expression $command
}
}


$command = "docker exec -i mydockerdb pg_restore -U postgres -v -d mydatabase < download.dump"
ExecuteCommand($command)