如何从终端读取日期并与当前日期进行比较 [英] How to read date from terminal and compare with current date
本文介绍了如何从终端读取日期并与当前日期进行比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道从终端读取日期并使用shell脚本与当前日期进行比较的适当方式,
I want to know the appropriate way of reading date from terminal and comparing with current date using shell script,
我有以下脚本,
a=`date +%Y-%m-%d`
while [ 1 ] ; do
echo "Enter Date"
read todate
if [ $todate < $a ];then
break;
fi
echo "todate greater than curDate"
done
它没有按预期运行。
更新
这是我的最终版本,
Here is my final version,
#! /bin/bash
DATE=$(date '+%s')
while [ 1 ] ; do
echo "Enter Date[DD MM YYYY]:"
read D M Y
THIS=$(date -d "$Y-$M-$D" '+%s')
if (( THIS < DATE )) ; then
break
fi
done
谢谢大家!
推荐答案
7.3。其他比较运算符
7.3. Other Comparison Operators
...
字符串比较
...
<
is less than, in ASCII alphabetical order
if [[ "$a" < "$b" ]]
if [ "$a" \< "$b" ]
Note that the "<" needs to be escaped within a [ ] construct.
所以,您的
if [ $todate < $a ];then
成为
if [ $todate \< $a ];then
或
if [[ $todate < $a ]];then
这篇关于如何从终端读取日期并与当前日期进行比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文