How can I convert a BASH menu script to Perl?

pete840264

New member
Hello,

I have the following BASH code for a menu script that runs upon logging in to the server. I need to convert this BASH code to Perl. I know that CASE statements aren't allowed in Perl, so how should I go about converting this code to perl?

Here is part of the code, as all of it will not fit here, but the menus all function the same. Also the validation is at the bottom of my code so it doesnt appear here...

## Purpose: Displays a menu upon user login
#! /bin/bash
shopt -s -o nounset

declare -i mainSelection
declare -i systemSelection
declare -i adminSelection
declare -i editSelection
declare -i networkSelection
declare -i keepgoing=1
declare -ir MINSELECTION=1
declare -ir MAXMAINSELECTION=5
declare -ir MAXSYSTEMSELECTION=5
declare -ir MAXADMINSELECTION=7
declare -ir MAXEDITSELECTION=5
declare -ir MAXNETWORKSELECTION=6

main() {
while [ $keepgoing = 1 ]
do
mainMenu
setMainMenu
case $mainSelection in
1) systemMenu
setSystemInput
systemCase;;
2) adminMenu
setAdminInput
adminCase;;
3) editingMenu
setEditInput
editCase;;
4) networkMenu
setNetworkInput
networkCase;;
5) exit;;
esac
read -p "Would you like to continue? Please enter 1 for yes or 0 to exit: " keepgoing
if [ $keepgoing = 0 ]
then
clear ; exit
fi
done
}

mainMenu() {
clear
tput cup 3 18; echo "Main Menu"
tput cup 4 18; echo "========="
tput cup 5 18; echo "1 - System Commands"
tput cup 6 18; echo "2 - User Administration Commands"
tput cup 7 18; echo "3 - Editing Commands"
tput cup 8 18; echo "4 - Networking Commands"
tput cup 9 18; echo "5 - Exit"
}

setMainMenu() {
declare -ir VIUSERINPUT1=0
tput cup 12 18
read -p "Please enter a selection: " mainSelection
validateInput $VIUSERINPUT1
}

systemMenu() {
clear
tput cup 3 18; echo "System Menu"
tput cup 4 18; echo "==========="
tput cup 5 18; echo "1 - View Current Month's Calendar"
tput cup 6 18; echo "2 - Display Environment Variables"
tput cup 7 18; echo "3 - Display System Date and Time"
tput cup 8 18; echo "4 - Display UTC time"
tput cup 9 18; echo "5 - Return to Main Menu"
}

setSystemInput() {
declare -ir VIUSERINPUT2=1
tput cup 12 18
read -p "Please enter a selection: " systemSelection
validateInput $VIUSERINPUT2
}

systemCase() {
case $systemSelection in
1) cal;;
2) printenv;;
3) date;;
4) date -u;;
5) main;;
esac
}

adminMenu() {
clear
tput cup 3 18; echo "User Administration Menu"
tput cup 4 18; echo "========================"
tput cup 5 18; echo "1 - Show User Login History"
tput cup 6 18; echo "2 - Show Logged-In Users"
tput cup 7 18; echo "3 - Show User and Group IDs"
tput cup 8 18; echo "4 - Check Messages"
tput cup 9 18; echo "5 - Show who is logged in, and what they are doing"
tput cup 10 18; echo "6 - Display Group Memberships"
tput cup 11 18; echo "7 - Return to Main Menu"
}

setAdminInput() {
declare -ir VIUSERINPUT3=2
tput cup 12 18
read -p "Please enter a selection: " adminSelection
validateInput $VIUSERINPUT3
}

adminCase() {
case $adminSelection in
1) last | more;;
2) who;;
3) id;;
4) mail;;
5) w;;
6) groups;;
7) main;;
esac
}

editingMenu() {
clear
tput cup 3 18; echo "Editing Menu"
tput cup 4 18; echo "============"
tput cup 5 18; echo "1 - List Open Files"
tput cup 6 18; echo "2 - Open VI Editor"
tput cup 7 18; echo "3 - Display Current Login Name"
tput cup 8 18; echo "4 - Display Current Processes"
tput cup 9 18; echo "5 - Return to Main Menu"
}

setEditInput() {
declare -ir VIUSERINPUT4=3
tput cup 12 18
read -p "Please enter a selection: " editSelection
validateInput $VIUSERINPUT4
}

editCase() {
case $editSelection in
1) lsof | more;;
2) vi;;
3) logname;;
4) ps | more;;
5) main;;
esac
}
 
Back
Top