How do I automatically answer y in bash script? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) 2019 Community Moderator Election Results Why I closed the “Why is Kali so hard” questionCPU is free, yet bash script not utilizing all CPU resourcesRun bash script on startup in Linux Mint and open mate-terminal automaticallyBASH function to read user input OR interrupt on timeoutWhat should I know when I define a bash function to use a program's name?Bash Scripting: automate command in consoleHow to write bash script while using command as condition in if statement?find -exec not working in bash script but working in terminalBash script failing to call other script without errorbash script not running at startupBash interactive - entire script writing to history

How can I prevent/balance waiting and turtling as a response to cooldown mechanics

Central Vacuuming: Is it worth it, and how does it compare to normal vacuuming?

Project Euler #1 in C++

What is best way to wire a ceiling receptacle in this situation?

Do I really need to have a message in a novel to appeal to readers?

Maximum summed subsequences with non-adjacent items

Did any compiler fully use 80-bit floating point?

How were pictures turned from film to a big picture in a picture frame before digital scanning?

Can the Flaming Sphere spell be rammed into multiple Tiny creatures that are in the same 5-foot square?

What initially awakened the Balrog?

What's the difference between the capability remove_users and delete_users?

Is the IBM 5153 color display compatible with the Tandy 1000 16 color modes?

Putting class ranking in CV, but against dept guidelines

Why are my pictures showing a dark band on one edge?

Why is it faster to reheat something than it is to cook it?

Sum letters are not two different

Prove that BD bisects angle ABC

What does this say in Elvish?

How does Belgium enforce obligatory attendance in elections?

How do living politicians protect their readily obtainable signatures from misuse?

Flight departed from the gate 5 min before scheduled departure time. Refund options

In musical terms, what properties are varied by the human voice to produce different words / syllables?

Is there public access to the Meteor Crater in Arizona?

How to write capital alpha?



How do I automatically answer y in bash script?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)
2019 Community Moderator Election Results
Why I closed the “Why is Kali so hard” questionCPU is free, yet bash script not utilizing all CPU resourcesRun bash script on startup in Linux Mint and open mate-terminal automaticallyBASH function to read user input OR interrupt on timeoutWhat should I know when I define a bash function to use a program's name?Bash Scripting: automate command in consoleHow to write bash script while using command as condition in if statement?find -exec not working in bash script but working in terminalBash script failing to call other script without errorbash script not running at startupBash interactive - entire script writing to history



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








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










share|improve this question



















  • 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 yn to stdout (which is still connected to the terminal, not to stdin of any program)

    – Peter Cordes
    Apr 14 at 22:53


















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










share|improve this question



















  • 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 yn to stdout (which is still connected to the terminal, not to stdin of any program)

    – Peter Cordes
    Apr 14 at 22:53














9












9








9


3






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










share|improve this question
















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







bash






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Apr 14 at 9:38









Rui F Ribeiro

42.2k1484142




42.2k1484142










asked Apr 14 at 7:29









Shalev SasonShalev Sason

493




493







  • 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 yn to stdout (which is still connected to the terminal, not to stdin of any program)

    – Peter Cordes
    Apr 14 at 22:53













  • 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 yn to stdout (which is still connected to the terminal, not to stdin of any program)

    – Peter Cordes
    Apr 14 at 22:53








6




6





ITYM, #! /bin/bash - instead of #!/usr/bin

– Stéphane Chazelas
Apr 14 at 7:54





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





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




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






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




2





What your script actually does: run uninstall and wait for it to exit. Then sleep 10 seconds. Then echo yn to stdout (which is still connected to the terminal, not to stdin of any program)

– Peter Cordes
Apr 14 at 22:53






What your script actually does: run uninstall and wait for it to exit. Then sleep 10 seconds. Then echo yn to stdout (which is still connected to the terminal, not to stdin of any program)

– Peter Cordes
Apr 14 at 22:53











1 Answer
1






active

oldest

votes


















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 '%sn' 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.






share|improve this answer

























  • 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 '%sn' 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 '%sn' y n '' 2 3 (where '' is the empty line).

    – Stéphane Chazelas
    Apr 14 at 8:01












Your Answer








StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512367%2fhow-do-i-automatically-answer-y-in-bash-script%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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 '%sn' 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.






share|improve this answer

























  • 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 '%sn' 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 '%sn' y n '' 2 3 (where '' is the empty line).

    – Stéphane Chazelas
    Apr 14 at 8:01
















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 '%sn' 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.






share|improve this answer

























  • 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 '%sn' 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 '%sn' y n '' 2 3 (where '' is the empty line).

    – Stéphane Chazelas
    Apr 14 at 8:01














36












36








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 '%sn' 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.






share|improve this answer















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 '%sn' 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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Apr 14 at 7:59

























answered Apr 14 at 7:36









Stéphane ChazelasStéphane Chazelas

315k57598955




315k57598955












  • 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 '%sn' 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 '%sn' y n '' 2 3 (where '' is the empty line).

    – Stéphane Chazelas
    Apr 14 at 8:01


















  • 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 '%sn' 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 '%sn' y n '' 2 3 (where '' is the empty line).

    – Stéphane Chazelas
    Apr 14 at 8:01

















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





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





@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 '%sn' y n n y y n 2 3 n y | /opt/MNG/MNGVIEWHP/fe/uninstall

– Shalev Sason
Apr 14 at 7:51






Thanks ! So if I want to enter another values like interger (2 for example) I need to replace it instead of "y"? printf '%sn' 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





Yes, that's the idea.

– Stéphane Chazelas
Apr 14 at 7:52




3




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 '%sn' y n '' 2 3 (where '' is the empty line).

– Stéphane Chazelas
Apr 14 at 8:01






@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 '%sn' y n '' 2 3 (where '' is the empty line).

– Stéphane Chazelas
Apr 14 at 8:01


















draft saved

draft discarded
















































Thanks for contributing an answer to Unix & Linux Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f512367%2fhow-do-i-automatically-answer-y-in-bash-script%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown