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 in Linux Administration by Damon Salvatore
• 5,510 points
40 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 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 in Linux Administration by Shubham
• 13,300 points
47 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 in Linux Administration by Shubham
• 13,300 points
65 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 in Linux Administration by Omkar
• 67,660 points
43 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 in Linux Administration by Upasana
• 8,530 points
56 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 in Linux Administration by Upasana
• 8,530 points
35 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 in Linux Administration by Upasana
• 8,530 points
27 views
0 votes
1 answer
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 in Linux Administration by ajs3033
• 7,280 points
45 views