9

I want to uninstall some program via a bash script. After I run the command, the terminal asking me if I am sure that I want to uninstall it. So I need to answer y. I want to do it automatically and answer y after delay of 10 seconds.

I did this example but it's not working -

#!/usr/bin
/opt/MNG/MNGVIEWHP/fe/uninstall
sleep 10
echo "y"

Any idea? Centos 7.2

  • 6
    ITYM, #! /bin/bash - instead of #!/usr/bin – Stéphane Chazelas Apr 14 at 7:54
  • check if the uninstall program accepts parameters or a config script or not. It'll be easier that way – phuclv Apr 14 at 9:49
  • 1
    Asked and answered ad nauseam across the web. "it's not working" is not a good decription of the problem. Perhaps you can state how your problem is different than the countless others. – jww Apr 14 at 14:13
  • 2
    What your script actually does: run uninstall and wait for it to exit. Then sleep 10 seconds. Then echo y\n to stdout (which is still connected to the terminal, not to stdin of any program) – Peter Cordes Apr 14 at 22:53
36

That's what the yes command is for. It outputs ys one per line indefinitely so it can be piped to commands that ask yes/no questions.

yes | /opt/MNG/MNGVIEWHP/fe/uninstall

That answers y to all questions. To answer n to all questions, replace yes with yes n. For a predefined mix of y and n, you can replace yes with:

printf '%s\n' y n n y y n...

Or run it as:

/opt/MNG/MNGVIEWHP/fe/uninstall << 'EOF'
y
n
n
y
y
n
EOF

If you do need the answer not to be available for reading before 10 seconds, you'd do:

(sleep 10; echo y; sleep 2; echo n;...) | /opt/MNG/MNGVIEWHP/fe/uninstall

But that would probably not be necessary, when we write y to the pipe, it's going to be there for uninstall to read it whenever it wants to read it, it's unlikely you'd need to wait for it to be ready to read it. The exception would be if uninstall decides to flush the input before asking the question.

All those assume the uninstall command just reads each answer as one line of input from its standard input.

For more complex cases, where the command reads the answers directly from the tty device or where you need to feed answers conditionally (for instance based on what the command outputs), that's where you'd use things like expect or zsh's zpty.

Note that many interactive programs can enter some non-interactive mode when passed some option. You may want to check their manual first, before spending too much effort working around the problem.

  • Thanks! This is fine in case that we need to answer only for one question. What happened if we have to answer on 20 questions for example? – Shalev Sason Apr 14 at 7:40
  • @Shalev, see edit. – Stéphane Chazelas Apr 14 at 7:45
  • Thanks ! So if I want to enter another values like interger (2 for example) I need to replace it instead of "y"? printf '%s\n' y n n y y n 2 3 n y | /opt/MNG/MNGVIEWHP/fe/uninstall – Shalev Sason Apr 14 at 7:51
  • Yes, that's the idea. – Stéphane Chazelas Apr 14 at 7:52
  • 3
    @ShalevSason, in all the solutions I've gave, like with echo "y", each time, we write y<newline>, simulating you pressing y followed by Enter. If you want to simulate pressing Enter alone, that would be feeding an empty line, so printf '%s\n' y n '' 2 3 (where '' is the empty line). – Stéphane Chazelas Apr 14 at 8:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.