Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to execute bash script that requires multiply answers?

Here is the case. I have bash script that requires a couple of command to be executed. First of all, it requires sudo, then answer (y/n) and then password one more time. What I want to do is I want to execute it in one command.

Let’s say I have my bash script – myscript.sh. This script requires sudo to be executed. So, to execute it in one line I can write:

echo 'mypassword' | sudo -S bash myscript.sh

And this will work. But after script is executed I need to answer y and type password one more time. How can I do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Here is what I have tried:

printf '%s\n mypassword y mypassword' | sudo -S bash myscript.sh
echo 'y\nmypassword\n' | echo 'mypassword' | sudo -S bash myscript.sh

And there were a couple more of what I have tried, but it didn’t work.

>Solution :

You can:

printf '%s\n' mypassword y mypassword | ...
( echo mypassword; echo y; echo mypassword ) | ...

This script requires sudo to

Note that typing that on the command line or in a script will publish your password. Instead, configure sudoers to allow the user to login without a password. Consider configuring credential caching in sudoers, so you don’t have to type password twice.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading