How do I automatically answer y in bash script? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm 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

Why was the term "discrete" used in discrete logarithm?

Did Xerox really develop the first LAN?

How to do this path/lattice with tikz

Why aren't air breathing engines used as small first stages

Models of set theory where not every set can be linearly ordered

What does the "x" in "x86" represent?

How discoverable are IPv6 addresses and AAAA names by potential attackers?

Right-skewed distribution with mean equals to mode?

Are my PIs rude or am I just being too sensitive?

Do you forfeit tax refunds/credits if you aren't required to and don't file by April 15?

Why don't the Weasley twins use magic outside of school if the Trace can only find the location of spells cast?

What's the purpose of writing one's academic bio in 3rd person?

How to find all the available tools in macOS terminal?

What do you call a plan that's an alternative plan in case your initial plan fails?

Why are there no cargo aircraft with "flying wing" design?

Determinant is linear as a function of each of the rows of the matrix.

How do I mention the quality of my school without bragging

Can inflation occur in a positive-sum game currency system such as the Stack Exchange reputation system?

"Seemed to had" is it correct?

How can I fade player character when he goes inside or outside of the area?

Is it ethical to give a final exam after the professor has quit before teaching the remaining chapters of the course?

Antler Helmet: Can it work?

How to recreate this effect in Photoshop?

What is the correct way to use the pinch test for dehydration?



How do I automatically answer y in bash script?



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm 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;








7















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



















  • 5





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

    – Stéphane Chazelas
    yesterday











  • check if the uninstall program accepts parameters or a config script or not. It'll be easier that way

    – phuclv
    yesterday






  • 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
    yesterday







  • 1





    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
    yesterday


















7















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



















  • 5





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

    – Stéphane Chazelas
    yesterday











  • check if the uninstall program accepts parameters or a config script or not. It'll be easier that way

    – phuclv
    yesterday






  • 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
    yesterday







  • 1





    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
    yesterday














7












7








7


2






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 yesterday









Rui F Ribeiro

42.1k1483142




42.1k1483142










asked yesterday









Shalev SasonShalev Sason

392




392







  • 5





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

    – Stéphane Chazelas
    yesterday











  • check if the uninstall program accepts parameters or a config script or not. It'll be easier that way

    – phuclv
    yesterday






  • 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
    yesterday







  • 1





    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
    yesterday













  • 5





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

    – Stéphane Chazelas
    yesterday











  • check if the uninstall program accepts parameters or a config script or not. It'll be easier that way

    – phuclv
    yesterday






  • 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
    yesterday







  • 1





    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
    yesterday








5




5





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

– Stéphane Chazelas
yesterday





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

– Stéphane Chazelas
yesterday













check if the uninstall program accepts parameters or a config script or not. It'll be easier that way

– phuclv
yesterday





check if the uninstall program accepts parameters or a config script or not. It'll be easier that way

– phuclv
yesterday




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
yesterday






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
yesterday





1




1





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
yesterday






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
yesterday











1 Answer
1






active

oldest

votes


















32














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
    yesterday











  • @Shalev, see edit.

    – Stéphane Chazelas
    yesterday











  • 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
    yesterday












  • Yes, that's the idea.

    – Stéphane Chazelas
    yesterday






  • 2





    @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
    yesterday












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









32














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
    yesterday











  • @Shalev, see edit.

    – Stéphane Chazelas
    yesterday











  • 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
    yesterday












  • Yes, that's the idea.

    – Stéphane Chazelas
    yesterday






  • 2





    @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
    yesterday
















32














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
    yesterday











  • @Shalev, see edit.

    – Stéphane Chazelas
    yesterday











  • 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
    yesterday












  • Yes, that's the idea.

    – Stéphane Chazelas
    yesterday






  • 2





    @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
    yesterday














32












32








32







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 yesterday

























answered yesterday









Stéphane ChazelasStéphane Chazelas

314k57596954




314k57596954












  • 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
    yesterday











  • @Shalev, see edit.

    – Stéphane Chazelas
    yesterday











  • 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
    yesterday












  • Yes, that's the idea.

    – Stéphane Chazelas
    yesterday






  • 2





    @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
    yesterday


















  • 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
    yesterday











  • @Shalev, see edit.

    – Stéphane Chazelas
    yesterday











  • 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
    yesterday












  • Yes, that's the idea.

    – Stéphane Chazelas
    yesterday






  • 2





    @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
    yesterday

















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
yesterday





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
yesterday













@Shalev, see edit.

– Stéphane Chazelas
yesterday





@Shalev, see edit.

– Stéphane Chazelas
yesterday













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
yesterday






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
yesterday














Yes, that's the idea.

– Stéphane Chazelas
yesterday





Yes, that's the idea.

– Stéphane Chazelas
yesterday




2




2





@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
yesterday






@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
yesterday


















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







Popular posts from this blog

Category:9 (number) SubcategoriesMedia in category "9 (number)"Navigation menuUpload mediaGND ID: 4485639-8Library of Congress authority ID: sh85091979ReasonatorScholiaStatistics

Circuit construction for execution of conditional statements using least significant bitHow are two different registers being used as “control”?How exactly is the stated composite state of the two registers being produced using the $R_zz$ controlled rotations?Efficiently performing controlled rotations in HHLWould this quantum algorithm implementation work?How to prepare a superposed states of odd integers from $1$ to $sqrtN$?Why is this implementation of the order finding algorithm not working?Circuit construction for Hamiltonian simulationHow can I invert the least significant bit of a certain term of a superposed state?Implementing an oracleImplementing a controlled sum operation

Magento 2 “No Payment Methods” in Admin New OrderHow to integrate Paypal Express Checkout with the Magento APIMagento 1.5 - Sales > Order > edit order and shipping methods disappearAuto Invoice Check/Money Order Payment methodAdd more simple payment methods?Shipping methods not showingWhat should I do to change payment methods if changing the configuration has no effects?1.9 - No Payment Methods showing upMy Payment Methods not Showing for downloadable/virtual product when checkout?Magento2 API to access internal payment methodHow to call an existing payment methods in the registration form?