Visit Our UNIX and Linux User Community

Full Discussion: While Do Loops Problems
Top Forums Shell Programming and Scripting While Do Loops Problems Post 302141287 by DKNUCKLES on Thursday 18th of October 2007 12:47:58 PM
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

 
Test Your Knowledge in Computers #513
Difficulty: Easy
Most programming languages are not case sensitive.
True or False?

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
break(1)                                                           User Commands                                                          break(1)

NAME
break, continue - shell built-in functions to escape from or advance within a controlling while, for, foreach, or until loop SYNOPSIS
sh break [n] continue [n] csh break continue ksh *break [n] *continue [n] DESCRIPTION
sh The break utility exits from the enclosing for or while loop, if any. If n is specified, break n levels. The continue utility resumes the next iteration of the enclosing for or while loop. If n is specified, resume at the n-th enclosing loop. csh The break utility resumes execution after the end of the nearest enclosing foreach or while loop. The remaining commands on the current line are executed. This allows multilevel breaks to be written as a list of break commands, all on one line. The continue utility continues execution of the next iteration of the nearest enclosing while or foreach loop. ksh The break utility exits from the enclosed for, while, until, or select loop, if any. If n is specified, then break n levels. If n is greater than the number of enclosing loops, the outermost enclosing loop shall be exited. The continue utility resumes the next iteration of the enclosed for, while, until, or select loop. If n is specified then resume at the n- th enclosed loop. If n is greater than the number of enclosing loops, the outermost enclosing loop shall be used. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words that follow a command preceded by ** that are in the format of a variable assignment are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign, and also that word splitting and file name genera- tion are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
csh(1), exit(1), ksh(1), sh( 1), attributes(5) SunOS 5.10 17 Jul 2002 break(1)

Featured Tech Videos

All times are GMT -4. The time now is 02:33 AM.
Unix & Linux Forums Content Copyright 1993-2020. All Rights Reserved.
Privacy Policy