【问题标题】:Powershell: how to invoke an expression with redirected input and outputPowershell:如何使用重定向的输入和输出调用表达式
【发布时间】:2020-07-29 16:31:00
【问题描述】:

我正在编写一个循环脚本来生成一个测试用例,在该测试用例中我对 ACM 问题的解决方案将失败。基本上它会无限生成随机测试用例文件,并使用我在网上找到的正确程序以及有问题的程序来解决它。然后对它们进行文件比较。

我之前在 Linux 上为此任务编写了一个脚本,它运行良好:

problem=383077-E
g++ rand.cpp -o rand.out
g++ std.cpp -o std.out
g++ $problem.cpp -o $problem.out
while true; do
  ./rand.out
  ./std.out < input.txt > answer.txt
  ./$problem.out < input.txt > output.txt
  diff -s answer.txt output.txt
  if [ $? -ne 0 ]; then
    break
  fi
done

然后我尝试为 Windows 编写一个 powershell 版本:

$problem = "383077-E"
g++ rand.cpp -o rand.exe
g++ std.cpp -o std.exe
g++ "$problem.cpp" -o "$problem.exe"
while ($true) {
  .\rand.exe
  cat input.txt | .\std.exe > answer.txt
  cat input.txt | ".\$problem.exe" > output.txt  # Error here
  if (diff (cat file1) (cat file2)) {
    break
  }
}

我无法让这条线 cat input.txt | ".\$problem.exe" &gt; output.txt 工作。

我是否使用了错误的工具(管道)来实现我的目标?

编辑:

目前我正在使用.cmd 脚本来兼容 Windows 7 环境:

@echo off
set problem=383077-E

g++ rand.cpp -o rand.exe
g++ std.cpp -o std.exe
g++ %problem%.cpp -o %problem%.exe

:loop
rand.exe > input.txt
std.exe < input.txt > answer.txt
%problem%.exe < input.txt > output.txt
fc answer.txt output.txt
if not errorlevel 1 goto loop

但我仍在寻找可以继续使用的 PowerShell 版本。

【问题讨论】:

    标签: powershell


    【解决方案1】:

    你需要连接字符串和变量:

    (Get-Content input.txt | .\383077-E.exe) > output.txt
    

    【讨论】:

    • 对不起,我仍然收到错误:+ cat input.txt | ".\$($problem).exe" &gt; output.txt + ~~~~~~~~~~~~~~~~~~~ Expressions are only allowed as the first element of a pipeline. + CategoryInfo : ParserError: (:) [], ParseException + FullyQualifiedErrorId : ExpressionsMustBeFirstInPipeline
    • 我知道可以。但是我创建了 $problem 变量和这个 SO question 来询问我是否可以在一个地方而不是每次都更改三个地方的问题名称。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-30
    • 2012-07-11
    相关资源
    最近更新 更多