In a unix shell, how to get yesterday's date into a variable?

前端 未结 17 1378
谎友^
谎友^ 2020-12-04 21:51

I\'ve got a shell script which does the following to store the current day\'s date in a variable \'dt\':

date \"+%a %d/%m/%Y\" | read dt
echo ${dt}
         


        
17条回答
  •  失恋的感觉
    2020-12-04 22:17

    You have atleast 2 options

    1. Use perl:

      perl -e '@T=localtime(time-86400);printf("%02d/%02d/%02d",$T[4]+1,$T[3],$T[5]+1900)'
      
    2. Install GNU date (it's in the sh_utils package if I remember correctly)

      date --date yesterday "+%a %d/%m/%Y" | read dt
      echo ${dt}
      
    3. Not sure if this works, but you might be able to use a negative timezone. If you use a timezone that's 24 hours before your current timezone than you can simply use date.

提交回复
热议问题