Getting yesterday's date in Unix csh script

3.2k Views Asked by At

I currently have the below lines as part of my script:

#! /bin/csh

set thedate = `date "+%d_%m_%y"`

This sets thedate to today's date.

Does anyone know what I need to add to get the script to pick up yesterday's date instead?

I have tried

date -v "-1d"
date -d "-1 day"

but I get the errors

date: illegal option -- v
date: illegal option -- d
usage:  date [-u] mmddHHMM[[cc]yy][.SS]
    date [-u] [+format]
    date -a [-]sss[.fff]

I have then tried

set thedate = `date "+%d_%m_%y" --date="1 day ago"`

which when echoing thedate, still gives today's date.

3

There are 3 best solutions below

0
Sunny Shukla On
$(date "+%d_%m_%y" --date="1 day ago")
0
BeschtPlaier On
date -d "-1 days" +"%a %d/%m/%Y"

The -d "-1 days" makes sure that the date of yesterday gets calculated.

The +"%a %d/%m/%Y" takes care of the output format (name, day, month, year).

5
zappy On
#!/bin/sh

thedate=`date "+%d_%m_%y" --date="1 day ago"`

echo "${thedate}"

Working fine for me. I dont know why you are using C-shell. I dont have C-Shell in my target. use shell(/bin/sh) or bash (/bin/bash)