3

I have a script that will get the last 5 days from the data input by the user.

Example:

Choose the month: 
1 - Jan
2 - Feb
...
12- Dec  // I used if else here 

Insert day:
30

My expected output would be this:

Jun 30
Jun 29
Jun 28
Jun 27
Jun 26

Is this possible? I already check date command but I can only replicate getting the date today then use -d command. I can't input the date I want to check.

1
  • Side note: You might want to take a look into the CASE selector instead of using an IF loop
    – FelixJN
    Commented Aug 5, 2016 at 8:11

1 Answer 1

1

With GNU date(1):

start_day=3
start_month=Jun
for d in {0..4}; do
    date -d "$start_month $start_day - $d days" +'%b %d'
done

Output:

Jun 03
Jun 02
Jun 01
May 31
May 30

Doing the same without GNU date(1) is possible, but potentially a lot more painful.

2
  • okay let me try this.
    – Edmhar
    Commented Aug 5, 2016 at 5:21
  • I just replace the date -d "$start_month $start_day - $d days" +'%b %d' to date -d "$start_month $start_day - $d days" +'%b %_d' to have space on the non-double digit.
    – Edmhar
    Commented Aug 5, 2016 at 6:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .