While Do Loops Problems


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting While Do Loops Problems
# 1  
Old 10-18-2007
While Do Loops Problems

So i'm coding this loop for a class I have. I think it goes without saying that I don't have much experience with this. This loops is supposed to check the input entered by the user. If it's entered in the command line then it checks the validity of the data entered, if it's not entered on the cmd line, the use is prompted to enter it. I need this loop to check the validity of the data - if it doesn't meet requirements, tell them the error, get them to re enter the data (their data of birth) then re-check the information they've entered. It's supposed to stay in the loop until the data they've entered meets requirements. If you guys could help me it would be GREATLY appreciated.

This is what I have so far... PLEASE NOTE : The error checking logic works fine, I just need help with the loop.

Code:
#!/bin/bash

#####
## VARIABLES
#####
status=0
iyear=${birthday:0:4}
imonth=${birthday:4:2}
iday=${birthday:6:2}
curdate=$(date +"%Y%m%d")
cal $imonth $iyear 2> /dev/null | grep $iday > /dev/null && daterr=0 || daterr=1

while [ $status -eq 0 ] 
do

[ $# = 0 ] && read -p "Please enter your birthday in the format YYYYMMDD : " birthday || birthday=$1
#####
## DATE VERIFICATION
#####

# DATE MUST BE 8 CHARACTERS
if [ ${#birthday} -ne 8 ]
then
	echo "You entered ${#birthday} characters, please enter your birthdate in the form YYYYMMDD" 
	ErrMsg="You entered ${#birthday} characters, please enter your birthdate in the form YYYYMMDD"
	status=0
	continue

# DATE CANNOT CONTAIN LETTERS
elif echo "$birthday" | grep "[^0-9]" > /dev/null
then
	echo "Your entry contains non numeric characters, please enter your birthdate in the form YYYYMMDD : "
	status=0
	continue

# DATE MUST ACTUALLY EXIST
elif [ $dateerr -eq  1 ]
then
	echo "You entered $birthday which is an invalid date, please enter your birthday in the form YYYYMMDD : "
	status=0
	continue

# DATE MUST NOT BE IN THE FUTURE
elif [ $iyear$imonth$iday -gt $curdate 2> /dev/null ]
then
	echo "You entered $birthday, which is a future date, please enter your birthday in the form YYYYMMDD : "
	status=0
	continue
else
	status=1
	break
fi
done

# 2  
Old 10-18-2007
Thread closed. Violation of the "No Homework" Rule.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Help with loops?

I'm trying to understand better the while and until loops, can someone help me with this example? #!/bin/bash # Listing the planets. for planet in Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune Pluto do echo $planet # Each planet on a separate line. done echo; echo for... (3 Replies)
Discussion started by: jose2802
3 Replies

2. Shell Programming and Scripting

Two Loops

Hi please help. I have a file with two columns. I want to insert the value of the first column and second column in different sections of a line. code: for line in `cat $1`; for x in `cat $1 |awk '{print $2}'`; do do echo "print $line and then print $x" done done It works... (8 Replies)
Discussion started by: cinderella
8 Replies

3. Shell Programming and Scripting

loops

Hi All I have some directories on our server which are containing .csv files. i need to print value of cell "B2" from those csv files. Please advise. I have tried head command as example: head -2 */Book_Collection_Report_1_-_Collection_Requests_trials.csv | sed -n "3p" | awk -F","... (4 Replies)
Discussion started by: yash1978
4 Replies

4. UNIX for Dummies Questions & Answers

Help with for loops

Hi, I am starting to enhance my scripting knowledge and need some assistance with simple 1 line for loops. This may help to do a mass permissions change on a big apache doc root while I have an insane customer on my phone. What is the best resource to learn this skill and what are some that... (5 Replies)
Discussion started by: Oshie74
5 Replies

5. Shell Programming and Scripting

Loops

Hi All, I want to execute a script the number of times a user enters. Please can you advise on hor can I do the same. Many Thanks, Shazin (4 Replies)
Discussion started by: Shazin
4 Replies

6. Shell Programming and Scripting

Help with the 2 for loops

#!/bin/bash IFS=$'\n' A= a c b t g j i e d B= t y u i o p counter=0 found="" for i in $(cat $A) do for j in $(cat $B) do if then found="yes" fi done if then (1 Reply)
Discussion started by: vadharah
1 Replies

7. UNIX for Dummies Questions & Answers

Help with While Loops

I am traversing down a list, and I am not quite sure how to tell the loop to break when it's done going through the file. #!/bin/sh while : do read list <&3 echo $list done is the code. The file "list" is simply 5 4 3 2 1 any advice on how to break the loop after the file is... (1 Reply)
Discussion started by: MaestroRage
1 Replies

8. UNIX for Dummies Questions & Answers

While Loops

I'm trying to create a loop that will prompt the user for 15 values, not forcing them to enter all 15. If the user enters through one or more of the prompts the null value needs to be converted to 0, otherwise set the parameter = to the value entered: ex. Please enter file no #1: 17920 ... (4 Replies)
Discussion started by: vdc
4 Replies

9. UNIX for Advanced & Expert Users

Loops

Can anybody help please. I am trying to right a script which will loop until a certain action has been performed. For example i current have two batch jobs i would like to put into a wait status. Batch Jobs A and B . The script i am trying to get to work is below. jobs="A B" COUNT=0 while... (2 Replies)
Discussion started by: mariner
2 Replies

10. UNIX for Dummies Questions & Answers

loops?

hello....very new user to unix...and i have a question..i am not sure if there is such a thing For example...the user is asked if he likes Bananas....if he says yes.... echo You like Bananas $name at the end of the script it echos all that the user has entered so they can read it.... but... (1 Reply)
Discussion started by: jonas27
1 Replies
Login or Register to Ask a Question