Huu Phan | Blog Linux operating system | Linux operating system -Blog Huu Phan | www.huuphan.com
huuphan.com. Powered by Blogger.

My blog about Open Source: Zimbra Mail Server,linux,bash script,centos,linux command...

Bash script while loop case statement example

How to create "Menu Lists" use Bash script while loop case statement. I'm use while loop with case statement create "Menu Lists". Bash script for fun!

The display color for echo command
Red color
RED='\033[0;31m'  
Blue color
BLUE='\033[0;34m'
No color
NC='\033[0m'
Read from standard input and write to standard output and files
tee -a /tmp/log__$DATE 
To run bash script
[[email protected] ~]$ ./menu_list 
The display as below:
 Menu Lists

 1) My blog
 2) Your name
 3) How old are you?
 4) Your Hobbies
 5) Quit
>>

My full bash script "bash script while loop case statement" example
#!/bin/bash
# author: HuuPV

RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' #No color
DATE=$(date +%Y%m%d)

while true
do
    echo ""
    echo -e "$BLUE Menu Lists $NC"
    echo -e ""
    echo -e "$BLUE 1) My blog $NC"
    echo -e "$BLUE 2) Your name $NC"
    echo -e "$BLUE 3) How old are you? $NC"
    echo -e "$BLUE 4) Your hobit $NC"
    echo -e "$BLUE 5) Quit $NC"
    echo -n ">> "
    read case

    case "$case" in
     1)
            read -p "Are you sure? [y/N] " response
            if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
            then
                    echo "www.huuphan.com! ^_^"
            else
                    echo "Nothing!"
            fi
     ;;
     2)
            read -p "Are you sure? [y/N] " response
                if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
                then
                        read  -p "Your Name :" NAME
                echo $NAME
                
             else
                        echo "Nothing!"
                fi

     ;;
     3)
            read -p "Are you sure? [y/N] " response
                if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
                then
                       read -p "How old are you:" OLDD
               echo $OLDD
                   
                else
                       echo "Nothing!"
                fi

     ;;
     4)
            read -p "Are you sure? [y/N] " response
                if [[ "$response" =~ ^([yY][eE][sS]|[yY])+$ ]]
                then
                read -p "Your Hobbies:" HOB
                        echo $HOB
                else
                        echo "Nothing!"
                fi
     ;;
     5) break;;
             esac
done | tee -a /tmp/log_$DATE
echo -e "$RED Exit ^_^ $NC"

I hope will this helpful for you
Bash script while loop case statement example

post new :

Huu Phan | Blog Linux operating system | Huu Phan ~ Zimbra Mail Server,linux,bash script,centos,linux command | www.huuphan.com

4 Comments

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Java developer learn from Java Training in Chennai. or learn thru Java Online Training India . Nowadays Java has tons of job opportunities on various vertical industry.

Relay


Hey There. I found your blog using msn. This is a very well written article. I will make sure to bookmark it and return to read more of your useful info. Thanks for the post. I'll certainly comeback. aol.com mail login

Relay

This Blog is protected by DMCA.com