Ksh date validation YYYYMMDD - shell

I'm having a weird problem with the below simple date validation.
It fails validation for some dates.
An example is: 20140717
Other dates pass validation ok? i.e: 20140727
validate_date()
{
typeset my_date=$1
if [[ $my_date = [0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] ]] ; then
return 0;
else
return 1
fi
}
Can someone run it on ksh and see if the same happens?

I cannot see why the code is given improper result for your input .
I think one of the better ways of validating for date would be to use date -d "datestring"
it return the time displayed by the string in case of success and error in case date validation fails
so after date -d "datestring" .. you can check the value of $? so know success or failure of validation .
for example
ajay#pavilion:~$ date -d 20140730
Wed Jul 30 00:00:00 IST 2014
ajay#pavilion:~$ echo $?
0
ajay#pavilion:~$ date -d 20140750
date: invalid date ‘20140750’
ajay#pavilion:~$ echo $?
1
ajay#pavilion:~$

Related

Syntax for comparing dates on HP-UX box - not found error

This is regards checking SSL certificate expiry on a HP-UX box. No date -d available.
I have the following;
#!/bin/bash
# Exit script if program fails or an unset variable is used
set -eu
server="BLABLA"
port="443"
graceperiod_days="30"
# Get expiry date of SSL certificate, in format 'Jan 31 11:59:00 2018 GMT'
enddate="$(openssl s_client -connect "$server:$port" 2>/dev/null | openssl x509 -noout -enddate | sed -e 's#notAfter=##')"
# Get today's date in format DD-MM-YYYY
todaysdate="$(date "+%d-%m-%Y")"
echo "Today's date is $todaysdate"
# Convert $enddate to format DD-MM-YYYY
enddate_formatted=$(printf '%s\n' "$enddate" | awk '{printf "%02d-%02d-%04d\n",$2,(index("JanFebMarAprMayJunJulAugSepOctNovDec",$1)+2)/3,$4}')
echo "Certificate expiry date is $enddate_formatted"
# Compare expiry date with today's date
if "$todaysdate" -ge "$("$enddate_formatted" - "$graceperiod_days")"
then echo "$todaysdate is greater than $enddate_formatted. SSL certificate has expired!"
elif "$todaysdate" -lt "$("$enddate_formatted" - "$graceperiod_days")"
then echo "$todaysdate is before $enddate_formatted. Everything is OK!"
else
echo "ERROR"; fi
As far as I can tell, this should work, however the output is;
Today's date is 29-08-2018
Certificate expiry date is 21-07-2018
./test[22]: 21-07-2018: not found.
./test[22]: 29-08-2018: not found.
./test[24]: 21-07-2018: not found.
./test[24]: 29-08-2018: not found.
ERROR
What's going wrong?
First, you need dates in a form you can perform arithmetic on:
todaysdate_seconds=$(date +%s --date "$todaysdate") # assuming GNU date
gp_seconds=$((graceperiod_days * 86400))
enddate_seconds=$(date +%s --date "$enddate_formatted")
Second, your if statement is missing a command whose exit status it can check. All you have are the arguments to such a command. Instead, use
if test "$todaysdate_seconds" -ge "$("$enddate_seconds" - "$gp_seconds")"; then
or the simpler bash arithmetic command
if (( todaysdate_seconds >= enddate_seconds - gp_seconds )); then

Custom date verification - BASH - using just grep or awk etc

I saw couple of posts (some depends upon date -d $xyz to verify) but I'm trying to create an until loop where the user should be re-prompted to enter the value of a date format until it matches the custom date format.
My date format (what I need for Splunk) is m/d/yyyy:h:m:s or mm/dd/yyyy:hh:mm:ss
which means, if m (month number) is a single digit lets say 1 for January, then both 1 or 01 values are possible for date format but 0 or 00 is NOT a valid value. Value range is 01-to->12 or 1-to->12 but not greater than 12.
Similarly, the same rule applies to d (day number), it can be 01-to->10-to->31 or 1-to->31 but not 00 or more than 31 and all other yyyy (year), h (hour), m (minute), s (second) part.
What could be a minimal code (obfuscated is fine) to do this verification in BASH? It seems like date -d ??? doesn't provides this custom kind of verification for date/times!
OK, I can write one verifyDateFormatfunc() to do this, but I know there are people who have already written a one-liner / minimal snippet to verify this for sure. grep -f .. (where bunch of regex are listed line by line for all possible combinations, again the main code will look very minimal if I follow this? as the patterns sitting in -f file for grep will be transparent to a user) -or creating a map funcation (based on delimiters) for value ranges?
Possible values:
1/03/2017:23:0:15
02/4/2017:0:1:2
09/05/2017:10:10:0
10/6/2017:12:14:16
Here's an unholy extended regular expression (POSIX ERE):
^([1-9]|1[0-2]|0[1-9])/([1-9]|0[1-9]|[12][0-9]|3[01])/[0-9]{4}:([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$
that will test for the date/time patterns you specified (m/d/yyyy:h:m:s and mm/dd/yyyy:hh:mm:ss), with:
month: 1-12, 01-12
day: 1-31, 01-31
year: 0000-9999
hour: 0-23, 00-23
minute: 0-59, 00-59
second: 0-59, 00-59
You can use in an awk program that will exit with success (exit code 0) if the (first) line is a valid date/time (wrapped in a shell function that tests the first argument, for convenience):
#!/bin/bash
is_datetime_valid() {
awk '{exit $0!~"^([1-9]|1[0-2]|0[1-9])/([1-9]|0[1-9]|[12][0-9]|3[01])/[0-9]{4}:([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$"}' <<<"$1"
}
Or, if you prefer a pure bash solution (with ERE support in bash v3.0+):
#!/bin/bash
is_datetime_valid() {
local pat='^([1-9]|1[0-2]|0[1-9])/([1-9]|0[1-9]|[12][0-9]|3[01])/[0-9]{4}:([0-9]|0[0-9]|1[0-9]|2[0-3]):([0-9]|[0-5][0-9]):([0-9]|[0-5][0-9])$'
[[ $1 =~ $pat ]]
}
You can use it like:
if is_datetime_valid "1/03/2017:23:0:15"; then
# yup, it's valid
else
# ney, it's invalid
fi
Tested on a few examples:
#!/bin/bash
samples=(
"1/03/2017:23:0:15" "02/4/2017:0:1:2" "09/05/2017:10:10:0" "10/6/2017:12:14:16"
"00/03/2017:23:0:15" "1/33/2017:23:0:15"
)
for dt in "${samples[#]}"; do
if is_datetime_valid "$dt"; then
echo "$dt is valid"
else
echo "$dt is invalid"
fi
done
Gives:
1/03/2017:23:0:15 is valid
02/4/2017:0:1:2 is valid
09/05/2017:10:10:0 is valid
10/6/2017:12:14:16 is valid
00/03/2017:23:0:15 is invalid
1/33/2017:23:0:15 is invalid
I do not know whether using BSD date is an option for you, but it has what you are looking for.
There the date checker function can look like this
is_datetime_valid() {
date -j -f "%m/%d/%Y:%T" $1 1> /dev/null 2>&1
return $?
}

How could I use bash to work out how many tuesdays there are in a month? [duplicate]

I need to sort data on a weekly base and all i have are dates in a logfile.
Therefore to sort out data per week i would like to create a list with the dates of all mondays for a given year. I have tried to work something out and the only idea i currently have is to use ncal with year and month as argument looping over all months and extracting all mondays. Isn't there a more efficient way?
To get all mondays, by getting all dates and filtering by Mondays:
for i in `seq 0 365`
do date -d "+$i day"
done | grep Mon
Of course, you could also take a monday and keep incrementing by 7 days.
hope that's what you mean. Below can be changed to vary the output formats of the dates.
date command can be used for that, dunno if ncal is any more/less efficient.
I know you went for "binning" now, but here is a more readable v.
$ cat /tmp/1.sh
#!/bin/bash
test -z "$year" && {
echo "I expect you to set \$year environment variable"
echo "In return I will display you the Mondays of this year"
exit 1
}
# change me if you would like the date format to be different
# man date would tell you all the combinations you can use here
DATE_FORMAT="+%Y-%m-%d"
# change me if you change the date format above. I need to be
# able to extract the year from the date I'm shoing you
GET_YEAR="s/-.*//"
# this value is a week, in milliseconds. Changing it would change
# what I'm doing.
WEEK_INC=604800
# Use another 3-digit week day name here, to see dates for other week days
DAY_OF_WEEK=Mon
# stage 1, let's find us the first day of the week in this year
d=1
# is it DAY_OF_WEEK yet?
while test "$(date -d ${year}-1-${d} +%a)" != "$DAY_OF_WEEK"; do
# no, so let's look at the next day
d=$((d+1));
done;
# let's ask for the milliseconds for that DAY_OF_WEEK that I found above
umon=$(date -d ${year}-1-${d} +%s)
# let's loop until we break from inside
while true; do
# ndate is the date that we testing right now
ndate=$(date -d #$umon "$DATE_FORMAT");
# let's extract year
ny=$(echo $ndate|sed "$GET_YEAR");
# did we go over this year? If yes, then break out
test $ny -ne $year && { break; }
# move on to next week
umon=$((umon+WEEK_INC))
# display the date so far
echo "$ndate"
done
No need to iterate over all 365 or 366 days in the year. The following executes date at most 71 times.
#!/bin/bash
y=2011
for d in {0..6}
do
if (( $(date -d "$y-1-1 + $d day" '+%u') == 1)) # +%w: Mon == 1 also
then
break
fi
done
for ((w = d; w <= $(date -d "$y-12-31" '+%j') - 1; w += 7))
do
date -d "$y-1-1 + $w day" '+%Y-%m-%d'
done
Output:
2011-01-03
2011-01-10
2011-01-17
2011-01-24
2011-01-31
2011-02-07
2011-02-14
2011-02-21
2011-02-28
2011-03-07
. . .
2011-11-28
2011-12-05
2011-12-12
2011-12-19
2011-12-26
Another option that I've come up based on the above answers. The start and end date can now be specified.
#!/bin/bash
datestart=20110101
dateend=20111231
for tmpd in {0..6}
do
date -d "$datestart $tmpd day" | grep -q Mon
if [ $? = 0 ];
then
break
fi
done
for ((tmpw = $tmpd; $(date -d "$datestart $tmpw day" +%s) <= $(date -d "$dateend" +%s); tmpw += 7))
do
echo `date -d "$datestart $tmpw day" +%d-%b-%Y`
done
You can get the current week number using date. Maybe you can sort on that:
$ date +%W -d '2011-02-18'
07

Add mins in existing custom date format via shell

I have problem that is using custom date format like date +%y%j.%H%M%S,
And what i want is to add 15 mins on the this date on just on current date of system. so that i can use for further calculation into my process.
I have tried with below code -
$uprBond=`date +%y%j.%H%M%S`
$ echo $uprBond
16079.031135
$ date -d "$(uprBond) + 5 minutes" +%y%j.%H%M%S
op > bash: uprBond: command not found
16079.035920
I am failing while passing the above date format , Can anybody please help on this.
Just for note, below is the piece of code is working when i used date function instead of defined date variable i.e. $uprBond (I don't want to use predefined date because we have some old same formatted date which needs that adding of mins).
date +%y%j.%H%M%S -d "`date` + 5 minutes";
op > 16079.040724
With GNU date, GNU bash 4 and its Parameter Expansion:
#!/bin/bash
uprBond="$(date +%y%j.%H%M%S)"
year="20${uprBond:0:2}"
doy="${uprBond#${uprBond:0:2}}"
doy="${doy%.*}"
time="${uprBond#*.}"
time="${time:0:2}:${time:2:2}:${time:4:2}"
in5min=$(date -d "${year}-01-01 +${doy} days -1 day +5 minutes ${time}" "+%y%j.%H%M%S")
echo "now: $uprBond"
echo "in 5min: $in5min"
Output:
now: 16079.145026
in 5min: 16079.145526

date comparison not working properly in shell script

I want to compare dates in shell script.
The logic I am using as below :
$date_1="Tue Nov 25 23:50:01 CST 2014"
$date_2=$(date)
if [ $date_2 -eq $date_1 ] ; then
echo "$date2 is equal to $date_1"
else
echo "$date2 is not equal to $date_1"
fi
However when I am executing the script using crontab for every minute,
I got the below message in log which is not correct :
Tue Nov 25 23:50:01 CST 2014 is not equal to Tue Nov 25 23:50:01 CST 2014
Could you please help me out of this embarrassing situation?
Assuming this job is running every minute. You can do:
date_1="Tue Nov 25 23:50:01 CST 2014"
dt1=$(date -d "$date_1" '+%Y%m%d%H%M')
dt2=$(date '+%Y%m%d%H%M')
if [ "$dt1" -eq "$dt2" ]; then
echo "$dt2 is equal to $date_1"
else
echo "$dt2 is not equal to $date_1"
fi
Best thing would be to format your date strings to look like numbers, starting with year, then month, etc. You probably would want to code a greater than or equal, or less than or equal, in case cron skips a second now or then:
date_1=$(date +%Y%m%d%H%M%S)
sleep 10
date_2=$(date +%Y%m%d%H%M%S)
if [ $date_2 -ge $date_1 ] ; then
echo "$date_2 is greater than or equal to $date_1"
else
echo "$date_2 is less than $date_1"
fi
Output:
20141126063044 is greater than or equal to 20141126063034
Beware you don't want the $ in front of the variable when you are setting it!

Resources