Home > Mobile >  Powershell - An empty pipe element is not allowed problem
Powershell - An empty pipe element is not allowed problem

Time:01-14

I Tried running this in powershell

cd "c:thetesttun2" ; if ($?) { nvcc thetestrun2.cu -o thetestrun2 } ; if ($?) { .\thetestrun2 } | Out-File "C:\thetesttun2\hellow.txt"

and this error keeps coming up

An empty pipe element is not allowed. CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException FullyQualifiedErrorId : EmptyPipeElement

Please help I am new to powershell

Please give me some code to run on my machine

CodePudding user response:

As hinted in comments, a pipeline statement cannot begin with a control flow structure like if(...){...} - the first element has to start with either a value expression or a command element.

There's a couple of options for solving this. If you simply want the output from the eventual invocation of the program, move | Out-File into the if body:

cd "c:thetesttun2"
if ($?) { nvcc thetestrun2.cu -o thetestrun2 }
if ($?) { .\thetestrun2 | Out-File "C:\thetesttun2\hellow.txt" }

If you also want to stream the output from the compilation to the file, wrap the whole thing in a scriptblock {...} (thus turning the whole thing into a command element) and invoke with the & call operator:

cd "c:thetesttun2"
& {
  if ($?) { nvcc thetestrun2.cu -o thetestrun2 }
  if ($?) { .\thetestrun2 }
} | Out-File "C:\thetesttun2\hellow.txt"

Finally, you can also wrap the if statements in the $(...) subexpression operator or the @(...) array subexpression operator, thus turning the whole thing into a value expression:

cd "c:thetesttun2"
@(
  if ($?) { nvcc thetestrun2.cu -o thetestrun2 }
  if ($?) { .\thetestrun2 }
) | Out-File "C:\thetesttun2\hellow.txt"

... but beware that this will block while executing the statements, and output won't start streaming until the subexpression has been fully evaluated, thus incurring higher memory allocations for the process - which is why I suggest the scriptblock approach detailed above instead :)

  • Related