-
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.
Answered by
mihaimyh
Feb 8, 2023
Replies: 2 comments 2 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.
2 replies
Answer selected by
mihaimyh
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: