Creating Menu Lists using Bash script while loop case statement example

Introduction

Creating interactive menus in Bash scripts can be a fun and useful way to automate tasks and provide a user-friendly interface. In this guide, we will create a simple menu using a while loop and case statement. The menu will allow users to perform various actions such as displaying a blog URL, entering a name, age, and hobbies, or quitting the script. We'll also use color to enhance the menu display.

Prerequisites

To run the Bash script, ensure you have a Unix-like system with Bash installed. You can run the script from the terminal.

Bash Script with While Loop and Case Statement


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

Conclusion

Using while loops and case statements in Bash scripting allows you to create interactive and user-friendly menus. This script demonstrates a simple yet effective way to engage users and automate tasks, all while using colorful outputs and logging capabilities. Feel free to modify and expand the script to suit your needs and have fun scripting!Thank you for reading the huuphan.com page!

Comments

Post a Comment

Popular posts from this blog

zimbra some services are not running [Solve problem]

How to install php7 on centos 6: A Step-by-Step Guide

Bash script list all IP addresses connected to Server with Country Information