-
I would like to use CliWrap to pass some strings to a bash script via standard input. #!/bin/bash
echo "Please type the user"
read -r user
echo "The user is: $user"
echo "Please type the password"
read -r password
echo "The password is: $password" I've followed the example described here #191 but it seems the programs keeps waiting on the input for the user variable: using var semaphore = new SemaphoreSlim(0, 1);
var buffer = new StringBuilder();
var stdin = PipeSource.Create(async (destination, cancellationToken) =>
{
while (!cancellationToken.IsCancellationRequested)
{
await semaphore.WaitAsync(cancellationToken);
var data = Encoding.UTF8.GetBytes(buffer.ToString());
await destination.WriteAsync(data, 0, data.Length, cancellationToken);
}
});
var cmd = stdin | Cli.Wrap("/tmp/test.sh");
await foreach (var cmdEvent in cmd.ListenAsync())
{
if (cmdEvent is StandardOutputCommandEvent stdOutEvent)
{
Console.WriteLine(stdOutEvent.Text);
// Detect if it's a prompt
if (stdOutEvent.Text.Contains("user", StringComparison.InvariantCultureIgnoreCase))
{
// Write the response
buffer.Clear();
buffer.Append("user123");
semaphore.Release();
}
else if (stdOutEvent.Text.Contains("password", StringComparison.InvariantCultureIgnoreCase))
{
// Write the response
buffer.Clear();
buffer.Append("pass123");
semaphore.Release();
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
I found what the problem is: the script was not properly consuming all of the input. Adding a newline character at the end of each input string, fixed the issue:
|
Beta Was this translation helpful? Give feedback.
-
I have more or less the same case in here but I have been struggling for 3 days and I could not find any solution on how to handle it. So I have this bash script ( it is something more complicated that we use in the company, but I am using the below as an example ) PS3="Select your language please: ";
options=("Java" "Bash" "C++" "C#" "Quit");
select lng in "${options[@]}"
do
case $lng in
"Java")
echo "$lng - is your VM ready?";;
"Bash")
echo "$lng - that's what we're talking about";;
"C++")
echo "$lng - let's prepare for a lot of compilation";;
"C#")
echo "I want to learn $lng like Oleksii";;
"Quit")
echo "Bye"
break;;
*)
echo "Invalid option $REPLY";;
esac
done
And this is the C# code that I have used: async Task Main()
{
using var semaphore = new SemaphoreSlim(0, 1);
var buffer = new StringBuilder();
var stdin = PipeSource.Create(async (destination, cancellationToken) =>
{
//while (!cancellationToken.IsCancellationRequested)
//{
//await semaphore.WaitAsync(cancellationToken);
var data = Encoding.UTF8.GetBytes(buffer.ToString());
if(data.Length > 0 )
await destination.WriteAsync(data, 0, data.Length, cancellationToken);
//}
});
var cmd = Cli.Wrap(@"C:/Program Files/Git/bin/bash.exe")
.WithArguments(["-c", "sh foo.sh 2>&1"])
.WithWorkingDirectory(@"C:/Users/temp")
.WithStandardInputPipe(stdin)
.WithValidation(CommandResultValidation.None);
await foreach (var cmdEvent in cmd.ListenAsync())
{
if (cmdEvent is StandardOutputCommandEvent stdOutEvent)
{
Console.WriteLine(stdOutEvent.Text);
if (stdOutEvent.Text.Contains("Select your language please: ", StringComparison.InvariantCultureIgnoreCase))
{
var input = Console.ReadLine();
// Write the response
buffer.Clear();
buffer.Append( int.Parse( input));
//semaphore.Release();
}
}
if (cmdEvent is StartedCommandEvent started)
Console.WriteLine($"Process started; ID: {started.ProcessId}");
if (cmdEvent is StandardErrorCommandEvent stdError)
Console.Error.WriteLine($"Err> {stdError.Text}");
if (cmdEvent is ExitedCommandEvent exited)
Console.WriteLine($"Process exited; Code: {exited.ExitCode}");
}
} So the first issue was that it reads all the options as errors that is why I needed to redirect the errors to the standard output. If I do the same using the Process approach I could not start at all the script. Any help is welcomed! |
Beta Was this translation helpful? Give feedback.
I found what the problem is: the script was not properly consuming all of the input. Adding a newline character at the end of each input string, fixed the issue: