Powershell: how to invoke an expression with redirected input and output

Viewed 120

I'm writing a loop script to generate a test case where my solution to an ACM problem will fail. Basically it infinitely generates random test case file and use a correct program I found on the web along with my problematic program to solve it. Then run a file comparison against them.

I wrote a script on Linux for this task previously and it works well:

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

Then I try to write a powershell version for Windows:

$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
  }
}

I can't get this line cat input.txt | ".\$problem.exe" > output.txt to work.

Am I using the wrong tool (pipeline) to achieve my goal?

Edit:

Currently I'm using a .cmd script for compatibility with Windows 7 environment:

@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

But I'm still looking for a PowerShell version to move on to.

1 Answers

You need to concatenate strings and variables:

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