I'm a rookie who is trying to learn this stuff. What I need help with is putting together a non complicated "while" loop within the below "if" statement. I also need the while loop to keep looping until the user types a key to end the loop. Please reveal the proper insertion points. Thank you, joe.
Script is below:
echo "Please choose a month from January to December and that month will display which holiday is related to that month."
read month
if [ "$month" = "January" ]
then
echo "Martin Luther King Day is in the month of January."
elif
[ "$month" = "February" ]
then
echo "President's Day is in the month of February."
elif
[ "$month" = "March" ]
then
echo "There is no holiday in the month of March."
elif
[ �$month� = �April� ]
then
echo �There is no holiday in the month of April.�
elif
[ �$month� = �May� ]
then
echo �Memorial Day is in the month of May.�
elif
[ �$month� = �June� ]
then
echo �There is no holiday in the month of June.�
elif
[ �$month� = �July� ]
then
echo �The Fourth of July is in the month of July.�
elif
[ �$month� = �August� ]
then
echo �There is no holiday in the month of August.�
elif
[ �$month� = �September� ]
then
echo �Labor Day is in the month of September.�
elif
[ �$month� = �October� ]
then
echo �Columbus Day is in the month of October.�
elif
[ �$month� = �November� ]
then
echo �Thanksgiving one of the most wonderful holidays, is in the month of November.�
elif
[ �$month� = �December� ]
then
echo �Christmas is in the month of December.�
else
echo �This month does not exist.�
fi
Can I suggest a case statement to simplify to logic.
Use break statement to leave while loop. Also notice some glob matching to allow upper or lower case first letter and quit/exit/done to leave.
while true
do
echo "Please choose a month from January to December and that month will display which holiday is related to that month."
read month
case "$month" in
[Jj]anuary) echo "Martin Luther King Day is in the month of January." ;;
[Ff]ebruary) echo "President's Day is in the month of February." ;;
[Mm]arch) echo "There is no holiday in the month of March." ;;
[Aa]pril) echo "There is no holiday in the month of April." ;;
[Mm]ay) echo "Memorial Day is in the month of May." ;;
[Jj]une) echo "There is no holiday in the month of June." ;;
[Jj]uly) echo "The Fourth of July is in the month of July." ;;
[Aa]ugust) echo "There is no holiday in the month of August." ;;
[Ss]eptember) echo "Labor Day is in the month of September." ;;
[Oo]ctober) echo "Columbus Day is in the month of October." ;;
[Nn]ovember) echo "Thanksgiving one of the most wonderful holidays, is in the month of November." ;;
[Dd]ecember) echo "Christmas is in the month of December." ;;
[Dd]one|[Ee]xit|[Qq]uit) break ;;
*) echo "This month does not exist."
esac
done
Also quick note on posting code to this forum, if you put
```text
and
```
tags around the code it stops any formatting (like indenting) from being lost as you can see above.
Thanks for your help but I was looking for specific things. I want to maintain my "if" statements and not use "case." I also don't want a "break" in my script. I want the script to continue until the user types "q" to quit, for example. If you could help me through this or know someone who can, I would appreciate it immensely.
while [ "$month" != "q" ]
do
echo "Please enter a month (q to quit)"
read month
if [ "$month" = "q" ]
then
echo "Now leaving loop"
elif [ "$month" = "January" ]
then
...
else
echo �This month does not exist.�
fi
done
There is also the until loop. Exactly the same that while loop but the test is negated. Some times improves readability.
until [[ "${month}" = "q" ]]
do
printf "Please, enter a month (q to quit): "
read month
if [[ "${month}" = "q" ]]
then
printf "Bye-bye!\n"
elif [[ "${month}" = "January" ]]
then
...
else
printf "%s does not exist in the Western Calendar\n" "${month}"
fi
done