Converting String Date into UNIX Date

Hi,
I have a string date to my unix script(sun solaris). I wanted to convert it into unix date so that I can use it in a conditional statement. Please see below:

 
 MyTest.sh -s 2018-05-09
  
suppdt=$1 # string date passed via arguement as 2018-04-09
 
curryr=`date '+%Y'`
nextyr=`expr $curryr + 1`
 
StartDt_v=`echo "${curryr}-05-01"`
EndDt_v=`echo "${nextyr}-08-15"`
  
 curryrfilename=test-import-data-${curryr}.txt
 nextyrfilename=test-import-data-${nextyr}.txt
  
 if [ ${suppdt} -ge ${StartDt_v} ] && [ ${suppdt} -le ${EndDt_v} ]; then
     rm -f nextyrfilename;
 else
    echo " Do nothing"
 fi
 

The issue is every time the conditional statement is evaluated it goes into else part of IF statement.

I think this might be happening due to the StartDt_v and EndDt_v being a String date instead of proper Date variable. Would appreciate any advice from experts.

Thanks

You are using integer operators to compare string variables - that won't work. You have two options here:

  • use string operators > and < BUT make sure to escape them as the shell might treat them as redirections otherwise.
  • make the dates integers using shell expansion like if [ ${suppdt//-} -ge ${StartDt_v//-} ] && . . .

try:

if [[ $(expr "${suppdt}" \>= "${StartDt_v}") -eq 1 && $(expr "${suppdt}" \<= "${EndDt_v}") -eq 1 ]]

or

if [[ "${suppdt}" > "${StartDt_v}" || "${suppdt}" = "${StartDt_v}" ]] && [[ "${suppdt}" < "${EndDt_v}" || "${suppdt}" = "${EndDt_v}" ]]