By Shalev Sason


2019-04-14 07:29:13 8 Comments

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

1 comments

@Stéphane Chazelas 2019-04-14 07:36:10

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.

@Shalev Sason 2019-04-14 07:40:37

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?

@Stéphane Chazelas 2019-04-14 07:45:22

@Shalev, see edit.

@Shalev Sason 2019-04-14 07:51:13

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

@Stéphane Chazelas 2019-04-14 07:52:10

Yes, that's the idea.

@Shalev Sason 2019-04-14 07:58:31

Thanks! last question: if I want to click on "enter" key - what I need to print?

@Stéphane Chazelas 2019-04-14 08:01:24

@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).

@mpez0 2019-04-14 16:35:47

Note that there are some programs that read not from stdin, but from the tty. For those, you can use the expect program.

@Stéphane Chazelas 2019-04-14 16:38:00

@mpez0, that's already mentioned in the answer.

@Git Gud 2019-04-15 07:48:53

@StéphaneChazelas Should it be "replace yes with n" instead? It won't allow me to edit the answer.

@Stéphane Chazelas 2019-04-15 08:00:51

@GitGud. No, there's generally no n command, but the yes command (not a POSIX command but ubiquitous as it was already there in Unix v7 in the 70s) takes an optional argument that is the text to repeat (y if not specified).

@Git Gud 2019-04-15 08:02:43

@StéphaneChazelas Yes, of course. I actually know this, it was just a brain fart. Sorry!

Related Questions

Sponsored Content

1 Answered Questions

1 Answered Questions

bash script not running at startup

  • 2017-07-11 12:43:31
  • Hexodus
  • 945 View
  • 0 Score
  • 1 Answer
  • Tags:   bash ubuntu

0 Answered Questions

Bash interactive - entire script writing to history

  • 2018-01-22 06:21:49
  • hmedia1
  • 114 View
  • 1 Score
  • 0 Answer
  • Tags:   bash

1 Answered Questions

[SOLVED] Bash script failing to call other script without error

1 Answered Questions

[SOLVED] find -exec not working in bash script but working in terminal

2 Answered Questions

[SOLVED] Bash Scripting: automate command in console

  • 2016-03-02 16:08:06
  • Joao Carlos
  • 900 View
  • 2 Score
  • 2 Answer
  • Tags:   bash shell-script

1 Answered Questions

[SOLVED] BASH function to read user input OR interrupt on timeout

  • 2015-03-19 20:18:41
  • Jay Holister
  • 4749 View
  • 4 Score
  • 1 Answer
  • Tags:   bash read

1 Answered Questions

[SOLVED] What should I know when I define a bash function to use a program's name?

  • 2015-11-05 16:45:40
  • Jonas
  • 331 View
  • 4 Score
  • 1 Answer
  • Tags:   bash

1 Answered Questions

[SOLVED] CPU is free, yet bash script not utilizing all CPU resources

1 Answered Questions

Sponsored Content