shell 将字符串转换为日期并在ksh [duplicate]中比较日期

xwmevbvl  于 2023-05-29  发布在  Shell
关注(0)|答案(1)|浏览(373)

此问题已在此处有答案

Compare two dates in shell script(2个答案)
Why does a space in a variable assignment give an error in Bash? [duplicate](3个答案)
Why should there be spaces around '[' and ']' in Bash?(5个答案)
6天前关闭
我需要采取字符串作为输入隐蔽的日期和比较与当前日期在ksh需要的东西如下

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"
5sxhfpxr

5sxhfpxr1#

由于输入日期格式似乎是MM-DD-YYYY或DD-MM-YYYY,因此没有必要让date理解-d。一个简单的数字比较就足够了:

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

如有必要,在read命令中交换md
为输入格式添加适当的错误检查。

相关问题