-1
#!/bin/bash
#organization: Seneca College
#Purpose: Validate a date
#Usage: chkdate year month day
#

year=$1; month=$2; day=$3; extra=$4
if [[ "$year" == "" || "$month" == "" || "$day" == "" ]]; then
        # Not enough data!
        echo Usage: chkdate year month day
        exit 0
fi
if [[ ! ( $year =~ ^[0-9]+$ && $month =~ ^[0-9]+$ && $day =~ ^[0-9]+$ ) ]]; then
        # Date not numeric!
        echo Usage: chkdate year month day
        exit 1
fi
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 2
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 3
fi
if [[ ( $month == 4 || $month == 6 || $month == 9 || $month == 11 ) && $day -gt 30 ]]; then
        # Invalid day!
        echo Usage: chkdate year month day
        exit 4
fi
if [[ ($month == 9) && ($year == 1752) && ( $day -gt 2) && $day -le 14  ]] ; then
        #invalid day!
        echo Usage: chkdate year month day
        exit 5

date -d $2/$3/$1 > /dev/null 2>&1
if [[ "$@" ]] ; then
        echo "valid date"
else
        echo "not a valid"
fi

it says my script has line 44 however my script total lines are 43

  • You've been downvoted because Google is your friend. See Case 4, and add Ramesh's answer. Your script does have 44 lines. Kate confirms it. Delete the extra carriage return. – eyoung100 Oct 9 '14 at 23:33
2

You have an unterminated if loop. Add a fi for this if loop.

if [[ ($month == 9) && ($year == 1752) && ( $day -gt 2) && $day -le 14  ]] 

After adding an fi, when I run your script, this is what I get as output.

chkdate 1342 1 21
valid date
chkdate 13as 1 21
Usage: chkdate year month day
  • ye i missed a fi i fixed it thou thx for rply – Hajir Golmohammadi Oct 9 '14 at 23:38
  • 1
    @HajirGolmohammadi If it solved your issue, please mark this answer as accepted. – John WH Smith Oct 10 '14 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.