I'm creating a bash script and I want to create a menu. When I run the code it displays the menu but the problem is when the user enters a choice it keeps re-printing the menu to ask again and again no matter what choice was selected.
#!/bin/bash
declare -i choice=1;
while ((choice!=5))
do
echo "Main Menu:"
echo -e "\t(a) Add"
echo -e "\t(b) Remove"
echo -e "\t(c) Seach"
echo -e "\t(d) Display"
echo -e "\t(e) Exit"
echo -n "Please enter your choice:"
read choice
case $choice in
"a"|"A")
echo "You entered a"
;;
"b"|"B")
echo "You entered b"
;;
"c"|"C")
echo "You entered c"
;;
"d"|"D")
echo "You entered d"
;;
"e"|"E")
echo "You entered e"
((choice=5))
;;
*)
echo "invalid answer"
;;
esac
done
#!/bin/bash -x
. You can enable and disable tracing by the commandset -x
andset +x
, too. Information to the user should be written to standard error stream (stderr). This can be done by redirecting the echo output, e.g.echo "Main Menu:" >&2
. Now it is possible to pipe the data output of your script to another command. If you really need a menu in a shell script than you can use the shell commandselect
.