While loop within if statement


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While loop within if statement
# 1  
Old 08-29-2013
While loop within if statement

Hi,

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:
Code:
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


Last edited by Scott; 08-29-2013 at 11:57 PM.. Reason: Code tags
# 2  
Old 08-29-2013
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.

Code:
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 [code] and [/code] tags around the code it stops any formatting (like indenting) from being lost as you can see above.

Last edited by Chubler_XL; 08-29-2013 at 11:33 PM..
# 3  
Old 08-29-2013
Re: Re: While loop within if statement

Hi Chubler,

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.

Thank you again,
joe.
# 4  
Old 08-30-2013
OK you can try:

Code:
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

done
# 5  
Old 08-30-2013
There is also the until loop. Exactly the same that while loop but the test is negated. Some times improves readability.

Code:
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


Last edited by Aia; 08-30-2013 at 12:40 AM.. Reason: More
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Windows & DOS: Issues & Discussions

Loop statement within a batch script

I have to create a couple of files within a directory structure. Filename stays the same, but at the end of each file, the number appears. Example: File needs to be created within directory c:\temp File Name will stay the same (testfile), but extension will increase, starting at 01 - example... (1 Reply)
Discussion started by: HenkTrumpie
1 Replies

2. Shell Programming and Scripting

Bash - Nesting if statement in for loop

I have the basic command written in bash for element in 1 2 do if ]; then set el = "t" else set el = "p" fi done but i get the following error syntax error near unexpected token `for' ` for element in 1 2' What should i do differently? (3 Replies)
Discussion started by: ncwxpanther
3 Replies

3. Shell Programming and Scripting

awk if statement in a for loop?

I need to match multiple values in a single column in a file: example: Source file: abd,123,one def,232,two ghi,987,six Target file: 12345,abcde,123 09876,zxvyr,566 56789,lmnop,232 Variable: var1=`grep 2 sourcefile | awk '{print$1}' essentially, echo "$var1" would read:... (2 Replies)
Discussion started by: kopfgeldjagar
2 Replies

4. Shell Programming and Scripting

If condition and for loop within sed statement

Hi, I tried to go through a lot of online material but could not find concrete solution. My issues is like this : I've got a input file like this : <a> <startDate>19700101000000</startDate> <endDate>20300101000000</endDate> </a> ... (12 Replies)
Discussion started by: Shaishav Shah
12 Replies

5. Shell Programming and Scripting

how to create a loop in an if statement

Hey guys, a=`cat abc | wc -l` b=`cat def | wc -l` if $a== $b then echo "a" else echo "b" fi I want the if condition to retry itself , untill a==b. I can't use goto statemt. Please help. Thanx in advance. Please use next time code tags for your code and data (5 Replies)
Discussion started by: jaituteja
5 Replies

6. Shell Programming and Scripting

Help With Loop in Case Statement script

I am writing a bash script that asks the user for input and I need it to repeat until the user selects quit.. I dont know how to write the loop for it I searched all over but i still do not get it.. if anyone could help with this it would be greatly apprciated here is my script so far: #!... (2 Replies)
Discussion started by: Emin_Em
2 Replies

7. Shell Programming and Scripting

Help with IF statement with loop Shell Script

Hello I am very new to shell and I bought some books and trying to learn it. I started trying to write a script that will take a number and count it down to 1 with commas in between. This number can only be one argument. If lower than one or higher than one argument it sends an error message. ... (4 Replies)
Discussion started by: zero3ree
4 Replies

8. Shell Programming and Scripting

For loop statement - catch error

I'm having a question about for loops. (bash) I have the following for example: for file in `ls *.txt` do read file ... done Now when there is a file present there is no problem, now when there is no file present I get the following output in my standard mail box : "No such... (4 Replies)
Discussion started by: lumdev
4 Replies

9. UNIX for Dummies Questions & Answers

if statement in a while loop

#!/usr/bin/ksh echo Please enter while read n do echo $n >> datafile done question: How can I enject an if statement that if the users enter 0 (zero) the program will exit? this is what I have but not working #!/usr/bin/ksh echo Please enter number while read n do if $n=0 then... (2 Replies)
Discussion started by: bobo
2 Replies

10. UNIX for Dummies Questions & Answers

if statement in for loop of a string

I am attempting to pass a string into awk and loop through it, and then for every occurrance of a certain character perform an action. In this case, for example, echo 1 for each time character r is found in the string. Except I can't get it to work. Could someone please tell me why? echo... (7 Replies)
Discussion started by: Sniper Pixie
7 Replies
Login or Register to Ask a Question