I am trying to run a menu option though IF loops. I keep getting errors not allowed the menu to be processed correctly. Currently it will accept the first 2 statements but then crash on the 3rd. The 2nd and 3rd have the same syntax, so I do not understand why it breaks.
#!/bin/bash
while true; do
read -p "Enter project number (1/2/3/10/20/30):"
$menu
if [ $menu -eq 1 ]; then
{
echo "1"
}
elif [ $menu -eq 2 ]; then
{
echo "2"
{
elif [ $menu -eq 3 ]; then
{
echo "3"
{
elif [ "$menu" == "10" ]; then
{
echo "10"
{
elif [ "$menu" == "20" ]; then
{
echo "20"
{
elif [ "$menu" == "30" ]; then
{
echo "30"
{
fi
read -p "Would you like to process another project? [y/n]" yn
case $yn in
[Yy]* ) ;;
[Nn]* ) exit;;
* ) echo "Please select proper entry:";;
esac
done
The first read does not populate $menu because it should not have the dollar character (the second read looks ok).
Also there are rather a lot of left braces { and no right braces.
However, this sort of script is best achieved with a case statement similar to your second read and then would be much easier to read:
#!/bin/bash
while true; do
read -p "Enter project number (1/2/3/10/20/30):" menu
case "${menu}" in
1) echo "${menu}"
;;
2) echo "${menu}"
;;
3) echo "${menu}"
;;
10) echo "${menu}"
;;
20) echo "${menu}"
;;
30) echo "${menu}"
;;
*) echo "Invalid option: ${menu}"
;;
esac
read -p "Would you like to process another project? [y/n]" yn
case $yn in
[Yy]* ) ;;
[Nn]* ) exit;;
*) echo "Please select proper entry:";;
esac
done