-1

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?

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.

dokichan
  • 857
  • 2
  • 11
  • 44
  • so how many lines are printed with `printf '%s\n mypassword y mypassword'` – KamilCuk Dec 28 '22 at 11:49
  • Why don't you pass the password as a command line argument to your script, and perform the relevant actions there with sudo and the password? – quellenform Dec 28 '22 at 11:49

1 Answers1

0

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.

KamilCuk
  • 120,984
  • 8
  • 59
  • 111