0

I need to take string as a input covert to date and compare with current date in ksh need something as below

read expdate
today=`date +"%d-%m-%Y"`
expD =`$expdate +"%d-%m-%Y"` -- this is not working
if [today -gt expD] ; then
  echo "Today is greater"
else
  echo " Today is gone"
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Jason
  • 129
  • 1
  • 5
  • 19
  • Use format %Y%m%d and then the inequality (as an integer or string) will work. – Jeff Holt May 22 '23 at 19:04
  • @JeffHolt how to format the expdate from string to date ? – Jason May 22 '23 at 19:05
  • Convert the user-entered string to a date in a different format. [This question](https://stackoverflow.com/questions/6508819/convert-date-formats-in-bash) will probably help. – Jeff Holt May 22 '23 at 19:06
  • Read [How to create a Minimal, Complete, and Verifiable Example.](https://stackoverflow.com/help/mcve): show us a possible user input – Gilles Quénot May 22 '23 at 19:06
  • @GillesQuénot expdate would be "03-02-2022" , expD needs to be 2022-02-03, something like this – Jason May 22 '23 at 19:09
  • @JeffHolt `date -d` is not working for ksh – Jason May 22 '23 at 19:10
  • `date` is not from `ksh`. Which OS do you run? And you don't provide a `date -d` example – Gilles Quénot May 22 '23 at 19:13
  • And add `03-02-2022` to your post by editing it – Gilles Quénot May 22 '23 at 19:14
  • Please tell me you're not using AIX. – Jeff Holt May 22 '23 at 19:17
  • @Jason please update the question to indicate the format of `expdate` and whether or not your user knows to enter date in said format; right now ... `03-02-2022` could be `MM-DD-YYYY` or `DD-MM-YYYY` .... which is it? – markp-fuso May 22 '23 at 20:53
  • 1
    consider entering your code block, along with an appropriate shebang (eg, `#!/bin/ksh`), at [shellcheck.net](https://www.shellcheck.net/) and make the suggested changes; once that code comes back clean then update the question with the latest/clean set of code – markp-fuso May 22 '23 at 20:57
  • There are several different problems with this code. Note that we expect each question to be about _one_ specific technical issue. – Charles Duffy May 22 '23 at 21:25
  • @JeffHolt i m using aix 7 – Jason May 23 '23 at 04:39

1 Answers1

0

Since the input date format seems to be either MM-DD-YYYY or DD-MM-YYYY, it is not necessary to have a date that understands -d. A simple numeric comparison is enough:

IFS=- read d m y
if [ $(date +%Y%m%d) -gt "$y$m$d" ]; then
    echo "Today is greater"
else
    echo " Today is gone"
fi

Swap m and d in the read command, if necessary.

Add appropriate error-checking for the input format.

jhnc
  • 11,310
  • 1
  • 9
  • 26