-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
HalosGhost
  • 4,790
  • See What are the shell's control and redirection operators? and look at the paragraphs on "here documents" and "here strings". – G-Man Says 'Reinstate Monica' Oct 07 '14 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. – G-Man Says 'Reinstate Monica' Oct 07 '14 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 07 '14 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 07 '14 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 07 '14 at 19:44
  • @Barmar, my cal does: cal --version outputs cal from util-linux 2.24.2, and cal --help shows usage cal [options] [[[day] month] year]. Giving a specific day shows that month with the day highlighted. – glenn jackman Oct 07 '14 at 19:47
  • I must have an older cal, it doesn't even have --version. – Barmar Oct 07 '14 at 19:50
  • im trying to use cal commands to validate dates and i get the error and whe i try fixing it this is what i get – Hajir Golmohammadi Oct 07 '14 at 21:22
  • 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 )'
    – Hajir Golmohammadi Oct 07 '14 at 21:23

1 Answers1

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...
jimmij
  • 47,140