Please use ***
[code][/code]*** tags around your code and data, to preserve the original formatting and to improve readability. Do
not use quote tags, bolding, colors, "start/end" lines, or other creative techniques. Thanks.
I think you should break this up into multiple parts. Start with the main menu, then have it call individual
functions for doing the sub-actions. That way you can run the function menus in loops of their own until you get the input you want, and exit back to the main menu when done.
It also helps to keep clutter down and make your script more readable and debuggable. ( See
Scripting With Style )
And yes, it's usually best to just use a simple
while true loop, test the input, and issue
continue or
break commands to control whether to loop through it again.
Simplified example:
Code:
#It's better to process name and password separately
usertest(){
local username
while true; do
read -p "Enter new username: " -r username
case $username in
'')
echo "username blank! Try again."
continue
;;
*[^a-z0-9]*)
echo "username contains invalid characters! Try again."
continue
;;
*)
echo "username ok"
break
;;
esac
done
passtest(){
local password
while true; do
read -p "Enter new password: " -r password
......etc .....
}
#run the main menu
while true; do
read -p "Select a number option from the choices above:" -r choice
case "$choice" in
1)
usertest
passtest
;;
.....etc......
esac
done
And a couple more scripting hints for you:
Code:
if [ "$username" == "" -o "$password" == "" ]; then
if [ $? -eq 0 ]
When using advanced shells like
bash or
ksh, it's recommended to use
[[..]] for string/file tests, and
((..)) for numerical tests. Avoid using the old
[..] test unless you specifically need POSIX-style portability.
http://mywiki.wooledge.org/BashFAQ/031
http://mywiki.wooledge.org/ArithmeticExpression
Code:
#YN=`echo $YesNo | tr [:lower:] [:upper:]`
This is also unneeded in shells like
bash or
ksh that have advanced
parameter substitution patterns built in:
Also,
$(..) is highly recommended over `..`.