Is there a way to execute piped shell command in GnuProlog?

75 views Asked by At

Im trying to execute this piped command:

powershell.exe Get-ChildItem | Where-Object { $\_.Name -match "router" } 

in GnuProlog:

:- initialization(main).

main :-
    shell('powershell.exe Get-ChildItem \174\ Where-Object { $_.Name -match "router" }'), nl, halt.

It seems that shell predicate does not support piping, since the output is:

'Where-Object' is not recognized as an internal or external command, operable program or batch file.
warning: C:/prolog.pl:1: user directive failed

is there another way to do it?

1

There are 1 answers

1
TessellatingHeckler On

I think you need to quote the whole argument in double quotes for Windows' system call handler to see it as one thing. And then edit the other quotes in it so they don't clash with either the Prolog atom single quotes or the PowerShell argument double quotes.

Try:

shell('powershell.exe -NoProfile -NonInteractive -Command "Get-ChildItem | Where-Object { $_.Name -match \'router\' }"'), nl, halt.

You might be able to skip the complexity of piping to Where-Object with:

Get-ChildItem -Filter *router*

PowerShell supports having the command code sent to it in Base64 encoded UTF16-LE, using the -EncodedCommand parameter; that exists to solve this kind of problem, and the effort of carefully quoting and escaping for shell handlers.