2

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.

  • Side note: You might want to take a look into the CASE selector instead of using an IF loop – Fiximan Aug 5 '16 at 8:11
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.

  • okay let me try this. – Edmhar Aug 5 '16 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 Aug 5 '16 at 6:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.