Get the date (a day before current time) in Bash

后端 未结 17 2147
臣服心动
臣服心动 2020-11-30 17:54

How can I print the date which is a day before current time in Bash?

17条回答
  •  孤城傲影
    2020-11-30 18:17

    #!/bin/bash
    OFFSET=1;
    eval `date "+day=%d; month=%m; year=%Y"`
    # Subtract offset from day, if it goes below one use 'cal'
    # to determine the number of days in the previous month.
    day=`expr $day - $OFFSET`
    if [ $day -le 0 ] ;then
    month=`expr $month - 1`
    if [ $month -eq 0 ] ;then
    year=`expr $year - 1`
    month=12
    fi
    set `cal $month $year`
    xday=${$#}
    day=`expr $xday + $day`
    fi
    echo $year-$month-$day
    

提交回复
热议问题