Results 1 to 2 of 2

Thread: Interactive Menu

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Lightbulb Interactive Menu

    Hi,

    Am new to bash scripting, so i though i try out some stuff by actually writing the code and gather some feedback. I'm trying to create a interactive menu of the following usage. By triggering the option 1 - 4, the menu would actually launch another script to the specify usage. Would like to gather some feedback if the following codes is correct or rather efficient. Another thing which i'm unsure if a "Return" should be added to the end of the function. Kindly give some advise.


    Code:
    #!/bin/bash
    
    function mainmenu() {
    clear
    echo
    echo "You are logged in as $USER, Welcome to System Control Menu"
    echo "Date of Login is: $(date)"
    echo
    echo "----------------------------------------------------------"
    echo "                   M A I N - M E N U "
    echo "----------------------------------------------------------"
    echo "1. System Backup & Recovery"
    echo "2. User Account Mgt"
    echo "3. System Audit Log"
    echo "4. Exit program"
    echo
    read -p "Enter your selection " option junk
    }
    
    while [[ $option -ne 4 ]]
    do
        mainmenu
    
        case $option in
    
        1 )    clear; . ./BackupMenu.sh
            ;;
        2 )    clear; . ./UserMenu.sh
            ;;
        3 )    clear; . ./AuditMenu.sh
            ;;
        4 )    echo "Shutting down program..."; sleep 3s; clear; exit 0
            ;;
        * )    echo "You have selected an invalid option! Please try again."
            ;;
        esac
    
    echo "You are being redirected to the main menu. Please hold."
    sleep 2s
    done


    This script is the UserMenu.sh. I'm thinking if useradd would be a better option as compare to newusers as i would like to perform a check before the user is added. By using my current method, there could an instant where i would add another user with (different UserID but similiar UID). I believe this would create problem in the system.
    Code:
    #!/bin/bash
    
    function pause() {
    echo "Press any key to continue..."
    read junk
    }
    
    function usermenu() {
    clear
    echo "User Account Mgt"
    echo "----------------"
    echo "a. Create Multi-User"
    echo "b. View the current list of User"
    echo "c. Delete Multi-User"
    echo "d. Return to main menu"
    
    read -p "Enter your selection " option junk
    }
    
    function deluser() {
    num=0
    for user in $(< UserList/dellist.txt)
    do
        userdel -r $user
        num=$((num+1))
    done
    echo "$num users were deleted."
    
    }
    
    
    while [[ $option != [dD] ]]
    do
        usermenu
    
        case $option in
        [aA] )    clear
            read -p "Please enter filename for user addition " filename
            newusers UserList/$filename
            pause
            ;;
        [bB] )    clear
            cat /etc/passwd | cut -d: -f1,3,5 | less
            pause
            ;;
        [cC] )    clear
            deluser
            pause
            ;;
        [dD] )    clear; break;
            ;;
        * )    echo "You have selected an invalid option! Please try again."
            ;;
        esac
    done

  2. #2
    Member billconner's Avatar
    Join Date
    Jul 2010
    Posts
    90
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Rep Power
    5

    Default

    Quote Originally Posted by Zancelot View Post
    Hi,

    Am new to bash scripting, so i though i try out some stuff by actually writing the code and gather some feedback. I'm trying to create a interactive menu of the following usage. By triggering the option 1 - 4, the menu would actually launch another script to the specify usage. Would like to gather some feedback if the following codes is correct or rather efficient. Another thing which i'm unsure if a "Return" should be added to the end of the function. Kindly give some advise.


    Code:
    #!/bin/bash
    
    function mainmenu() {
    clear
    echo
    echo "You are logged in as $USER, Welcome to System Control Menu"
    echo "Date of Login is: $(date)"
    echo
    echo "----------------------------------------------------------"
    echo "                   M A I N - M E N U "
    echo "----------------------------------------------------------"
    echo "1. System Backup & Recovery"
    echo "2. User Account Mgt"
    echo "3. System Audit Log"
    echo "4. Exit program"
    echo
    read -p "Enter your selection " option junk
    }
    
    while [[ $option -ne 4 ]]
    do
        mainmenu
    
        case $option in
    
        1 )    clear; . ./BackupMenu.sh
            ;;
        2 )    clear; . ./UserMenu.sh
            ;;
        3 )    clear; . ./AuditMenu.sh
            ;;
        4 )    echo "Shutting down program..."; sleep 3s; clear; exit 0
            ;;
        * )    echo "You have selected an invalid option! Please try again."
            ;;
        esac
    
    echo "You are being redirected to the main menu. Please hold."
    sleep 2s
    done
    Use the echo command once since it will enclose all newlines and list input options correctly. Follow that with the read command.
    Convert all the bash scripts into functions in the main script and then call them according to option input with the case command.
    It's easier to troubleshoot one script.


    This script is the UserMenu.sh. I'm thinking if useradd would be a better option as compare to newusers as i would like to perform a check before the user is added. By using my current method, there could an instant where i would add another user with (different UserID but similiar UID). I believe this would create problem in the system.
    Code:
    #!/bin/bash
    
    function pause() {
    echo "Press any key to continue..."
    read junk
    }
    
    function usermenu() {
    clear
    echo "User Account Mgt"
    echo "----------------"
    echo "a. Create Multi-User"
    echo "b. View the current list of User"
    echo "c. Delete Multi-User"
    echo "d. Return to main menu"
    
    read -p "Enter your selection " option junk
    }
    
    function deluser() {
    num=0
    for user in $(< UserList/dellist.txt)
    do
        userdel -r $user
        num=$((num+1))
    done
    echo "$num users were deleted."
    
    }
    
    
    while [[ $option != [dD] ]]
    do
        usermenu
    
        case $option in
        [aA] )    clear
            read -p "Please enter filename for user addition " filename
            newusers UserList/$filename
            pause
            ;;
        [bB] )    clear
            cat /etc/passwd | cut -d: -f1,3,5 | less
            pause
            ;;
        [cC] )    clear
            deluser
            pause
            ;;
        [dD] )    clear; break;
            ;;
        * )    echo "You have selected an invalid option! Please try again."
            ;;
        esac
    done
    use grep -c to check for the presence of a user in the /etc/password file. You can use the last line of the /etc/password file to collect the last valid UID and GID:

    uid=`awk -F: 'print $3'`
    gid=`awk -F: 'print $4'`
    if [ `grep -c $newuser /etc/password` -ge 1 ]
    then
    echo "Account $newuser exists ..."
    fi

    Bill

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Add Option To Right Menu
    By eawedat in forum Ubuntu / Debian
    Replies: 5
    Last Post: 28th August 2011, 02:11 PM
  2. Interactive shell script
    By jangozo in forum Shell scripting
    Replies: 2
    Last Post: 12th October 2010, 12:42 AM
  3. Replies: 2
    Last Post: 1st February 2010, 04:28 PM
  4. menu driven
    By gsb1bee in forum Shell scripting
    Replies: 0
    Last Post: 22nd April 2009, 11:56 PM
  5. Not a question: KDE Menu
    By nix CRAFT ROX in forum KDE
    Replies: 0
    Last Post: 15th December 2008, 03:46 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •