Adding yes no cancel prompts in shell script

0 votes

Hi. I want to know if I can add a prompt asking the user for input in a shell script? How can I do this in bash prompt?

Mar 12, 2019 in Linux Administration by Damon Salvatore
• 5,980 points
4,230 views

1 answer to this question.

0 votes

You can use the read command. Here's an example:

while true; do
    read -p "Do you wish to install this program?" yn
    case $yn in
        [Yy]* ) make install; break;;
        [Nn]* ) exit;;
        * ) echo "Please answer yes or no.";;
    esac
done

Another way you can use it is by using Bash select command. example:

echo "Do you wish to install this program?"
select yn in "Yes" "No"; do
    case $yn in
        Yes ) make install; break;;
        No ) exit;;
    esac
done

If you choose select you won't have to sanitize the input i.e. it will display the choices and you'll have to type a number matching your choice.

answered Mar 13, 2019 by ajs3033
• 7,300 points

Related Questions In Linux Administration

0 votes
1 answer

“cd” doesn't work in my shell script

One way to get around this is ...READ MORE

answered May 24, 2019 in Linux Administration by Shubham
• 13,490 points
1,858 views
0 votes
1 answer

Using shell variables in an awk script

Using -v is, imo, The best way because ...READ MORE

answered Jul 1, 2019 in Linux Administration by Shubham
• 13,490 points
2,689 views
0 votes
1 answer

How do I check if a directory exists in a Bash shell script?

To check if a directory exists in ...READ MORE

answered Jun 7, 2022 in Linux Administration by Korak
• 5,820 points
1,813 views
0 votes
1 answer

How to take input from user in bash script?

You can use if-else branch to check ...READ MORE

answered Jan 31, 2019 in Linux Administration by Omkar
• 69,220 points
1,138 views
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer

How to change the default shell in Linux?

1. Change the password file directly for ...READ MORE

answered May 24, 2019 in Linux Administration by Upasana
• 8,620 points
1,011 views
0 votes
1 answer

Linux: Finding all storage devices attached to a machine

Use /proc/partitions first. This will list all ...READ MORE

answered Jun 10, 2019 in Linux Administration by Upasana
• 8,620 points
809 views
0 votes
2 answers

How do I use a shell script to SSH in to a remote machine to execute commands?

Sorry in advance for any formatting. Check out ...READ MORE

answered Feb 7, 2021 in Linux Administration by anonymous
17,312 views
0 votes
1 answer

How to send mail using linux shell script?

If you've already configured the server, with ...READ MORE

answered Jun 11, 2019 in Linux Administration by ajs3033
• 7,300 points
1,231 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP