1

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?

1
  • 1
    How about your redesign your script to accept options so that it doesn't need user action? Commented Dec 29, 2016 at 13:19

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.