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

Reply With Quote
