Tyrrrz / CliWrap

Library for running command-line processes
MIT License
4.32k stars 264 forks source link

Can not redirect standard output from bash `read` command prompt #140

Closed my522cn closed 2 years ago

my522cn commented 2 years ago

Version

3.4.1

Details

The 'read' shell script can not be redirect to StandardOutput.

Here's the C# code:

var cmd = Console.OpenStandardInput() | Cli.Wrap(path).WithStandardOutputPipe(PipeTarget.ToStream(Console.OpenStandardOutput()));
await cmd.ExecuteAsync();

Here's the shell code

#!/bin/bash
echo ""
echo ""
echo ""
echo ""
echo ""
echo "I'm #1"
read -t 3 -p "Ready for test press any key or wait for 3 s" k
echo "I'm #2"
read -t 3 -p "Ready for test press any key or wait for 3 s" k
echo "I'm #3"
read -t 3 -p "Ready for test press any key or wait for 3 s" k
echo "I'm #4"
read -t 3 -p "Ready for test press any key or wait for 3 s" k
RESULTFILE=./test.result
echo "PASS">${RESULTFILE}
ls

Snipaste_2022-02-23_17-24-56

"Ready for test press any key or wait for 3 s" was not printed

Steps to reproduce

As details above.

Tyrrrz commented 2 years ago

Can you check if the same thing works using Process?

using var process = new Process
{
    StartInfo =
    {
        FileName = path,
        RedirectStandardOutput = true
    }
};

process.Start();
var output = process.StandardOutput.ReadToEnd();
my522cn commented 2 years ago

Can you check if the same thing works using Process?

using var process = new Process
{
    StartInfo =
    {
        FileName = path,
        RedirectStandardOutput = true
    }
};

process.Start();
var output = process.StandardOutput.ReadToEnd();

it works using Process: but without \n Snipaste_2022-02-24_08-40-14

Tyrrrz commented 2 years ago

I'd appreciate it if you can figure out where the bug is in CliWrap. I'm currently preoccupied with a war looming on the horizon. 🙁

my522cn commented 2 years ago

It's incredibly challenging for me. I will PR if I figure the bug out.

Tyrrrz commented 2 years ago

I did this in command line:

/bin/bash -c "echo test1; read -r -p test2; echo test3" > foo.txt

The output file contained only:

test1
test3

So it might be that's just how read works when stdout is redirected?

Tyrrrz commented 2 years ago

Tested using System.Diagnostics.Process with the below code, the result is the same.

    using var process = new Process
    {
      StartInfo =
      {
          FileName = "/bin/bash",
          Arguments = "-c \"echo test1; read -r -p test2; echo test3\"",
          RedirectStandardOutput = true
      }
    };

    process.Start();
    var output = process.StandardOutput.ReadToEnd();

Closing as it doesn't seem to be a problem with CliWrap.