#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
Asked
Active
Viewed 147 times
-1

HalosGhost
- 4,790
1 Answers
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, expectdone
somewhere - There is one too many
fi
. (instead ofdone
???) - It is good idea to quit from loop somehow...

jimmij
- 47,140
-
1Actually, he probably doesn't want a here string, either.
cal
takes its parameters as arguments, not stdin. – Barmar Oct 07 '14 at 19:39 -
That's true, but with here string
cal
at least outputs something, although not what one could expect. – jimmij Oct 07 '14 at 19:43 -
im trying to use cal commands to validate dates and i get the error – Hajir Golmohammadi Oct 07 '14 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 )' – Hajir Golmohammadi Oct 07 '14 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 – Hajir Golmohammadi Oct 07 '14 at 22:02
-
k i changed my script to this do u know why it is not running cal command still? – Hajir Golmohammadi Oct 07 '14 at 23:53
-
i cant send script here for some reason it says its too long – Hajir Golmohammadi Oct 07 '14 at 23:59
-
hey is there a possibilty that u can delete this answer becuase i dont want other people from my course see my script even though it is wrong – Hajir Golmohammadi Oct 08 '14 at 00:07
-
if [$# == $year $month $day ] ; then cal $day $month $year i want to know whats the problem here with this – Hajir Golmohammadi Oct 08 '14 at 00:38
while ...; do
should be marked withdone
, notfi
. And the loop will run infinitely, because the body never changes$#
. – Barmar Oct 07 '14 at 19:37while
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:41cal
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:44cal --version
outputscal from util-linux 2.24.2
, andcal --help
shows usagecal [options] [[[day] month] year]
. Giving a specific day shows that month with the day highlighted. – glenn jackman Oct 07 '14 at 19:47cal
, it doesn't even have--version
. – Barmar Oct 07 '14 at 19:50hgolmohammadi1@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