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

Bash script while loop case statement example

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


The output color of echo
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 output as bellow
 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
#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"

post new :

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

5 comments

"I very much enjoyed this article.Nice article thanks for given this information. i hope it useful to many pepole.php jobs in hyderabad.
"

Relay

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

I have read your post. I see that you talk about Bash script while loop case statement example.Well written article. Someone please help with annotated bibliography. thank you very much! I take from you an example in the writing essays, but when I dont have enough time for it.

Relay