Find the answer to your Linux question:
Results 1 to 8 of 8
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 ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Posts
    4

    Need Help w/ DO WHILE loop

    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. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Mind telling us how this script is misbehaving? (In detail, preferably.)

  3. #3
    Linux Enthusiast carlosponti's Avatar
    Join Date
    Dec 2004
    Location
    Oklahoma
    Posts
    560
    is this homework? if it is there are forum rules about us helping you.
    Blog
    Registered Linux user 396557

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Oops. My bad. My intent was to play the tutor and lead him step by step to see what his problem was. But the rules are the rules, and that's fine.

  5. #5
    Just Joined!
    Join Date
    Oct 2007
    Posts
    4
    Where are these rules that you speak of? I don't see any of them.

    Plus I wasn't asking for anyone to do it for me, i was asking what I was doing wrong.

    If I can't come to a forum to seek help then what the hell is the point of one?

  6. #6
    Just Joined!
    Join Date
    Oct 2007
    Posts
    37
    You haven't mentioned what you need help with specifically. Is it not staying in the loop? If so, try this and see if the last condition is met before it exits:

    else
    status=1
    break
    fi

    change to:

    else
    echo "FINAL CONDITION MET; ISSUE IS WITH LOGIC";
    status=1
    break
    fi

  7. #7
    Just Joined!
    Join Date
    Oct 2007
    Posts
    4
    I got the loop working fine now.

    Thanks

  8. #8
    Linux Enthusiast carlosponti's Avatar
    Join Date
    Dec 2004
    Location
    Oklahoma
    Posts
    560
    http://www.linuxforums.org/forum/lin...ums-rules.html

    there is a link to the forum rules. I didnt make them just passing along the message.
    Blog
    Registered Linux user 396557

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...