nushell icon indicating copy to clipboard operation
nushell copied to clipboard

How to pipe binary output from one process as input to another? (All arguments to an external command need to be string-compatible)

Open DavidTelenko opened this issue 2 years ago • 5 comments

Question

Here is a bash equivalent of a task I want to accomplish.

some_process < my/binary_file.bin

In nushell apparently there is no process substitution but I can run commands in () curly braces to pipe it's result to stdin of other command. However it doesn't seem to accept binary input.

some_process (open -r my/binary_file.bin)

It results with an error

  × External command failed
   ╭─[entry #1:1:24]
 1 │ some_process (open -r my/binary_file.bin)
   ·                        ────────────┬───
   ·                                    ╰── Cannot convert binary to a string
   ╰────
  help: All arguments to an external command need to be string-compatible

DavidTelenko avatar Apr 04 '24 21:04 DavidTelenko

Maybe you can just use a pipeline?

open -r my/binary_file.bi | some_process

WindSoilder avatar Apr 05 '24 00:04 WindSoilder

Given

some_process < my/binary_file.bin
some_process (open -r my/binary_file.bin)

is not semantically equivalent. The former is passing thru stdin, the latter is passing as an argument. What WindSoilder said is the only equivalent form

crides avatar Apr 05 '24 08:04 crides

The thing is I need to be able to pass read binary file not as only parameter to process. Here's the example:

some_process param1 < my/binary_file.bin

This time in nushell there is no way to do this in a simple pipeline and either I need to pass it in () as a parameter or use xargs equivalent in pipeline ($in parameter)

some_process param1 (open -r my/binary_file.bin)

or

(open -r my/binary_file.bin) | some_process param1 $in

Either way will yield same error saying that we cannot convert binary to a string.

I'm sorry for confusion but I'm particularly new to both bash and nushell scripting.

DavidTelenko avatar Apr 05 '24 09:04 DavidTelenko

open -r my/binary_file.bin | some_process param1

is fine. stdin and arguments are separate entities (in nushell or POSIX shells like bash)

For the bash example, the following is equivalent in effect:

cat my/binary_file.bin | some_process param1

(It's just whether you use a POSIX shell built-in redirection or pipes to pass things into stdin) Which is equivalent semantically to the nushell command above

crides avatar Apr 05 '24 09:04 crides

@crides Oh, ok I didn't know that, thank you, my issue seem to be resolved)

DavidTelenko avatar Apr 05 '24 10:04 DavidTelenko