MRKAVANA (mrkavana@gmail.com) - www.facebook.com/kavanathai

Jul 18, 2011

How to write a script to select a choice from a list of choices using bash scripting

This script will work in all linux/unix operating systems like Redhat, Fedora, Centos, Solaris, Ubuntu, Debian etc, if they are having bash shell or sh.
See the example given below :

#!/bin/bash
PS3='Enter the choice:'
LIST="Choice1 Choice2 Choice3 Quit"
select i in $LIST
        do
                if [ "$i" = Choice1 ]
                then
                        echo "Your choice is: $i"
#                       break
                elif [ "$i" = Choice2 ]
                then
                        echo "Your choice is: $i"
#                       break
                elif [ "$i" = Choice3 ]
then
                        echo "Your choice is $i"
#                       break
                elif [ "$i" = Quit ]
                then
                        exit
                fi
        done

No comments:

Post a Comment