#How to use shell pipe | in with-exec?

1 messages · Page 1 of 1 (latest)

runic trellis
#

Im trying to run some pipe in a with-exec, like that container | from alpine | with-exec "cat /etc/passwd | grep a" but got an error process "cat /etc/passwd | grep a" did not complete successfully: exit code: 1
Is there a way to do it (or alternative solution?)

I did try it with php sdk to, and played with redirectStdout/redirectStdin but I want to have a solution that does not create tmp file.

// Same error as above
->withExec(['cat', '/path/to/a/file', '|', 'grep', 'bar'])

// This work but create a tmp file
->withExec(
    ['cat', '/path/to/a/file'],
    redirectStdout: '/tmp/foo',
)

->withExec(
    ['grep', 'bar'],
    redirectStdin: '/tmp/foo',
)

Does anyone have a clue? 🙏

solemn bridge
#

One way is to run the command using sh.
For instance, if you want to run cat /etc/alpine-release | grep 3 you can:

container | from alpine | with-exec -- sh -c "cat /etc/alpine-release | grep 3" | stdout

And same thing if you want to use it from PHP:

->withExec(
  ['sh', '-c', 'cat /etc/alpine-release | grep 3']
)