AbhiCR7
AbhiCR7

Reputation: 43

Detect and Enter Input on a prompt in shell script

I'm working on a shell script, which has lot of network calls and installations. When it is executed I need to enter yes/no for each prompt. Which is fine.

But now i have a requirement to run it as cron. In which case i won't be able to give inputs for each of the prompt that comes up. Is there any way I can automate this or have some mechanism of knowing in script that a prompt has come up?

Upvotes: 1

Views: 2233

Answers (1)

Inian
Inian

Reputation: 85895

Use the yes command to answer interactive prompts,

yes Y | ./script.sh

The above syntax constantly puts the string Y to all your prompts. You can pass the string as you need after yes.

You can also use expect tool meant for this, but you need to know the exact prompt message for capturing and responding to it accordingly. If your prompt is simple and just need a simple input to pass yes would be the right tool.

Also you can use bash built-in printf, but you need to add the responses manually depending upon the number of prompts you have to respond to, e.g.

printf 'Y\nY\nY\n' | ./script.sh

to send response as Y for three prompts. As again, to avoid doing this manually, prefer using the yes command.

Upvotes: 5

Related Questions