-1
#Purpose: Validate a date
#Usage: chkdate year month day
#
if [ $# -eq 0 ]; then
        echo Usage: chkdate year month day
        exit 0
fi
year=$1; month=$2; day=$3; extra=$4
if [[ "$extra" != "" ]]; then
        # Too much data!
        echo Usage: chkdate year month day
        exit 1
fi
if [[ "$year" == "" || "$month" == "" || "$day" == "" ]]; then
        # Not enough data!
        echo Usage: chkdate year month day
        exit 2
fi
if [[ ! ( $year =~ ^[0-9]+$ && $month =~ ^[0-9]+$ && $day =~ ^[0-9]+$ ) ]]; then
        # Date not numeric!
        echo Usage: chkdate year month day
        exit 3
fi
#Remove leading zeros if any
year=$(echo "$year" | bc)
month=$(echo "$month" | bc)
day=$(echo "$day" | bc)
if [[ $year -lt 1 || $year -gt 9999 || $month -lt 1 || $month -gt 12 || $day -lt 1 || $day -gt 31 ]]; then
        # Date out of range!
        echo Usage: chkdate year month day
        exit 4
fi
if [[ ( $month == 1 || $month == 3 || $month == 5 || $month == 7 || $month == 8 || $month == 10 || $month == 12 ) && $day -gt 31 ]]; then
        # Invalid day!
        echo Usage: chkdate year month day
        exit 5
fi
if [[ ( $month == 4 || $month == 6 || $month == 9 || $month == 11 ) && $day -gt 30 ]]; then
        # Invalid day!
        echo Usage: chkdate year month day
        exit 6
fi
if [[ $month == 2 && $(($year%100)) == 0 ]]; then
        if [[ $(($year%400)) == 0 ]]; then
                isLeapYear=1
        else
                isNotLeapYear=1
        fi
elif [[ $month == 2 && $(($year%4)) == 0 ]]; then
        isLeapYear=1
else
        isNotLeapYear=1
fi
if [[ $month == 2 && $day -gt 28 && $isNotLeapYear ]]; then
        # Not Leap year!
        echo Usage: chkdate year month day
        exit 7
fi
if [[ $month == 2 && $day -gt 29 && $isLeapYear ]]; then
        # Invalid day!
        echo Usage: chkdate year month day
        exit 8
fi
while [[ $# -ne [1-8] ]]; do
cal << ( $year $month $day )
fi
echo $year $month $day is valid date
9
  • See What are the shell's control and redirection operators? and look at the paragraphs on "here documents" and "here strings". Oct 7, 2014 at 19:24
  • 1
    Also, please do some debugging on your own before you ask for help. If you know that you have a problem at the end of your script, you should post just the part that has the problem. And explain what is going wrong. Oct 7, 2014 at 19:25
  • The end of a while ...; do should be marked with done, not fi. And the loop will run infinitely, because the body never changes $#.
    – Barmar
    Oct 7, 2014 at 19:37
  • What is the while loop for, anyway? You already checked the arguments at the top of the script, why are you checking the number of arguments.
    – Barmar
    Oct 7, 2014 at 19:41
  • The cal command takes a year and month, not a day. It shows a calendar for the whole month. What are you expecting your script to show from that line?
    – Barmar
    Oct 7, 2014 at 19:44

1 Answer 1

2
  • Add shebang, since you are using [[ most probably #!/bin/bash is what you want
  • Try to stick with one test syntax (either single bracket [ or double [[)
  • Instead of << (here document) most probably you want <<< (here string)
  • At the end of the script while loop is not closed, expect done somewhere
  • There is one too many fi. (instead of done???)
  • It is good idea to quit from loop somehow...
9
  • 1
    Actually, he probably doesn't want a here string, either. cal takes its parameters as arguments, not stdin.
    – Barmar
    Oct 7, 2014 at 19:39
  • That's true, but with here string cal at least outputs something, although not what one could expect.
    – jimmij
    Oct 7, 2014 at 19:43
  • im trying to use cal commands to validate dates and i get the error Oct 7, 2014 at 21:20
  • while [[ $# -ne [1-8] ]]; do cal <<< ( $year $month $day ) done echo $year $month $day is valid date "check" 71L, 1821C written hgolmohammadi1@matrix:~/Lab2> check 1989 06 17 ./check: line 69: syntax error near unexpected token (' ./check: line 69: cal <<< ( $year $month $day )' Oct 7, 2014 at 21:21
  • k i checked everything and the only problem i get is when i run this check 1989 06 16 it gives me ./check: line 68: [: [1-8]: integer expression expected Oct 7, 2014 at 22:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .