Issue
I am tasked with making a user menu that will have 2 different variations, one for the novice user and the others for an expert.
I got it calling all my novice scripts, but in order to call my expert scripts, my main script needs to detect if a user has passed any of the arguments like "help, man, status" etc. And if they do I need my main script to call expert scripts in that case.
I am very new to bash scripting so I hope I can get some guidance here.
#!/bin/bash
echo -n "Name please? "
read name
echo "Menu for $name"
echo "===================================================="
echo -e "Enter
( 1 ) File and Directory Managment Commands\nEnter
( 2 ) Text Processing Commands\nEnter
( 3 ) System Status Commands\nEnter
( 4 ) Exit"
echo "Enter your choice :"
read en
case "$en" in
1)sh ./fileNovice.sh;;
2)sh ./textNovice.sh;;
3)sh ./systemStatusNovice.sh;;
4)echo "Good Bye..."
sleep 0;;
*)echo "Wrong Option selected" ;;
esac
Solution
Do not reinvent the wheel and use Bash's select
:
#!/usr/bin/env bash
declare -a level_names=([0]='Novice' [1]='Expert')
declare -i level=0
while :; do
printf 'Current mode : %s\n' "${level_names[level]}"
PS3='Enter your choice : '
select _ in \
'File and Directory Managment Commands' \
'Text Processing Commands' \
'System Status Commands' \
"Switch to ${level_names[level ^ 1]} mode" \
'Exit'; do
case "$REPLY" in
1) bash "./file${level_names[level]}.sh"; exit ;;
2) bash "./text${level_names[level]}.sh"; exit ;;
3) bash "./systemStatus${level_names[level]}.sh"; exit ;;
4) level=$((level ^ 1)); break ;;
5) exit ;;
*) echo 'Wrong Option selected'; break ;;
esac
done
done
Answered By - Léa Gris Answer Checked By - Marie Seifert (WPSolving Admin)