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,550 points
1,360 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 12, 2019 by ajs3033
• 7,280 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 23, 2019 in Linux Administration by Shubham
• 13,450 points
406 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,450 points
732 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,030 points
153 views
0 votes
1 answer

To run a shell script on a Unix console or Mac terminal

To run a non-executable sh script, use: sh myscript To run ...READ MORE

answered May 16, 2019 in Linux Administration by Upasana
• 8,620 points
139 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
258 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
80 views
0 votes
1 answer

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

Your first problem can be solved like ...READ MORE

answered Jun 27, 2019 in Linux Administration by ajs3033
• 7,280 points
4,277 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,280 points
145 views