I want to write a script that reads my input (for example if my script is called "check", then I would type "check 9 4 1993" and that input would go through the cal command and will check through the calendar whether it is a valid date or not).
I've got the bulk of my idea but I'm not sure what approach to use in my syntax. (I'm fairly new to bash scripting so bear with me please).
My idea below is that if the input that goes through the cal command gives an error it will mean that it's not a valid date, and vice-versa if there's no error than the date is valid. I do realize there's something terribly wrong with this draft (I can't figure out how to make it so that my input will go through the cal command), but I will appreciate some suggestions. Here's the draft anyways:
#!/bin/bash
day=$1; month=$2; year=$3
day=$(echo "$day" | bc)
month=$(echo "$month" | bc)
year=$(echo "$year" | bc)
cal $day $ month $year 2> /dev/null
if [[$? -eq 0 ]]; then
echo "This is a valid date"
else
echo "This is an invalid date"
fi
day=$(echo "$day" | bc)
? Input validation? Removal of leading zeros?. In either case you could also doday=$((10#$day))
which uses the shell's own arithmetic expansion instead of forking abc
process. – DigitalTrauma Oct 8 '14 at 16:28