1

I need to write few line in a code that I use in my study, and I really gave up! currently, i can list the days of a month and the equivalent day number but I can't link this to the system clock so it pulls out the correct number of the date in each month and automatically every month.

here is what I did:

#!/bin/bash
for (( i = 0; i < 30; ++i )); do
    echo -e "$( date -d "now +${i} days" +"%a" )", ${i}
done

output:

Fri, 0 
Sat, 1 
Sun, 2 
Mon, 3 
Tue, 4 
Wed, 5 
Thu, 6 
Fri, 7 
Sat, 8 
Sun, 9 
Mon, 10 
Tue, 11 
Wed, 12 
Thu, 13 
. . . .
 ..  . etc

I would like this code to list the correct number of the days in a given month. For example, in October there are /31/ days. When I run the code in November it must list 30 days, and when I run it in March it lists 31 days and in February it lists 28 or 29 days, and so on. How can I do that?

  • by "October" I assume you mean "the current month, when the script is run" ? – Jeff Schaller Oct 05 '18 at 16:53
  • Hold on, do you want the first line of output to be _the first date of the current month_ (even if the current date is the 5th)? – Kusalananda Oct 05 '18 at 16:55
  • @ Jeff Schaller no i mean every month it read the correct number of month if i run it in october it list 31 days. In I run it in november it list 30 days and so on –  Oct 05 '18 at 16:56

2 Answers2

0

Actually, you are close. Here are few minor revisions:

#!/bin/bash
var=$(cal $(date +"%m %Y") | awk 'NF {DAYS = $NF}; END {print DAYS}') # Get number of days in a month

for (( i = 1; i < ${var}+1; ++i )); do
    echo -e "$( date -d "now +${i} days" +"%a" )", ${i}
done
  • 1
    thank you goro for the help this is what i want i spent three days on this ;( –  Oct 05 '18 at 17:07
  • Note that [`cal` will highlight the current day](https://unix.stackexchange.com/questions/320871/why-does-cal-use-weird-08-h-b-terminal-code-for-highlighting-and-how-doe) but not when the output is not a terminal (as in a pipe to awk, above), so there's no risk running this on the last day of the month. – Jeff Schaller Oct 05 '18 at 17:17
  • @ Jeff Schaller thank you so much for the link and not ;-) –  Oct 05 '18 at 17:22
0

Calculate the last day by asking date for the first day of the next month, then subtract a day; then loop:

begin=1
end=$(date -d "$(date -d "$(date -d "next month" +"%Y%m01")") - 1 day" +%d)

for((i=begin;i<end;i++))
do
  date -d "$(date +%Y/%m/$i)" +"%a, $i"
done
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
  • I should be able to collapse some of those `date -d`'s... – Jeff Schaller Oct 05 '18 at 17:01
  • 1
    The `printf` is not needed. Use `date`'s format string. `+"%a, $i"`, or `+"%a, %e"` for that matter. – Kusalananda Oct 05 '18 at 17:04
  • 1
    thank you @JeffSchaller this gave me error "date: invalid date ‘2018101’ , 1 date: invalid date ‘2018102’ , 2 date: invalid date ‘2018103’ , 3 date: invalid date ‘2018104’ , 4 date: invalid date ‘2018105’ , 5 date: invalid date ‘2018106’ , 6 date: invalid date ‘2018107’ , 7 date: invalid date ‘2018108’ , 8 date: invalid date ‘2018109’ , 9 ’" –  Oct 05 '18 at 17:09
  • Fixed the single-digit days and the printf; I think I'm stuck with 3 nested `date -d`'s, though. – Jeff Schaller Oct 05 '18 at 17:13
  • 1
    @Jeff Schaller: this works with two `date` invocations on my Ubuntu 18.04 inserting the delta day into the second `date`'s format string: `date -d"$(date -d "next month" +"%Y%m01 - 1 day")" +%d` : `31` – RudiC Oct 05 '18 at 21:28