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
[huupv@huupv ~]$ ./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

Comments


  1. 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

    ReplyDelete

Post a Comment

Popular posts from this blog

zimbra some services are not running [Solve problem]

Bash script list all IP addresses connected to Server

How to install php7 on centos 6