After running script above following will be the output.
./script.ksh 12:34:21
good
./scrpit.ksh 26:67:92
bad
./script.ksh 24:17:12
bad
You could use string Invalid in place of bad which I have used above. Also on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk , /usr/xpg6/bin/awk , or nawk .
Could you please try following and let me know if this helps.
cat script.ksh
echo $1 | awk -F ':' '{match($0,/[0-2][0-4]:[0-5][0-9]:[0-5][0-9]/);A=substr($0,RSTART,RLENGTH);if(A && $1 !~ /24/){ print "good";exit 0} else {print "bad";exit 1;}}'
if [[ $? == 0 ]]
then
echo "we passed GOOD time test"
else
exit;
fi
When I run the script with different arguments to check it's functionality it give following output then.
./script.ksh 24:17:12
bad
./script.ksh 23:17:12
good
we passed GOOD time test
Similarly you could change it according to your need like in spite of printing lines which I did after checking $? status, where $? will check the exit status for last command.
but there is no need to fire up awk for this at all:
#!/bin/ksh
case "$1" in
([01][0-9]:[0-5][0-9]:[0-5][0-9]|2[0-3]:[0-5][0-9]:[0-5][0-9])
echo okay;;
(*) echo invalid
exit 1;;
esac
echo 'Continuing in script after time verification.'