Need help with "date" in ksh script

The script below works just fine.
Now i want to instead of just printing the last 7 days i want to print all the days last month. So if the script runs now, it will show all the 30 days in november.
I dont want any days to be shown in this month....
Im stuck, dont know how i will gett this to work?
******************************************

#!/bin/ksh

TEMP=/file/home/test.txt
trap "rm -f $TEMP" SIGHUP SIGINT EXIT

z=0
for i in 1 2 3 4 5 6 7 ; do

for w in `printf '%(%Y-%m-%d)T\n' ''$i' days ago'`; do XXX=$(grep $w /honey/logs/statistics/s* | wc -l ) ; done
echo "S" $w " :"$XXX >> $TEMP
z=$((z+$XXX))
done
echo "" >> $TEMP
echo "S total: $z" >> $TEMP
echo "" >> $TEMP


mailx -s "TEST" test@test.com < $TEMP
exit

---------- Post updated at 09:55 PM ---------- Previous update was at 09:39 PM ----------

I forgot to mention that /honey/logs/statistics/s* files
looks like this:

s_201612
s_201611
s_201610

And for example the s_201612 looks like inside:

2016-12-08 23:46:29:765          S  ERROR
2016-12-08 23:59:07:468          S  ERROR
2016-12-09 00:00:42:008          S  SUCCESS
2016-12-09 00:01:09:097          S  ERROR

and the script output (mail output) right now something like this:

S 2016-12-11  :100
S 2016-12-10  :200
S 2016-12-09  :100
S 2016-12-08  :300
S 2016-12-07  :500
S 2016-12-06  :100
S 2016-12-05  :400

S total: 1700

Maybe something like:

wc -l < "$(printf '%(s_%Y%m)T' '1 month ago')"

Thanks but its not what im looking for..

In what way is it not what you're looking for? Be specific.

Yes, some idea what you expect might help. Howsoever, try

eval seq "$(date -d $(date +"-%ddays") +'-s" %b %n" 1 %d')"
1 Nov 
2 Nov 
3 Nov 
4 Nov 
5 Nov 
6 Nov 
7 Nov 
8 Nov 
9 Nov 
10 Nov 
11 Nov 
12 Nov 
13 Nov 
14 Nov 
15 Nov 
16 Nov 
17 Nov 
18 Nov 
19 Nov 
20 Nov 
21 Nov 
22 Nov 
23 Nov 
24 Nov 
25 Nov 
26 Nov 
27 Nov 
28 Nov 
29 Nov 
30

It has two drawbacks:

  • the last month name is missing (as the seq separator string is used)
  • it needs the (dangerous) eval command.

Still it might be something to start with.

EDIT: Hoppla, reading carefully and completely sometimes helps: seq allows for a floating format specifier. Try

eval seq "$(date -d $(date +"-%ddays") +'-f"%%.0f %b" 1 %d')"
1 Nov
2 Nov
.
.
.
29 Nov
30 Nov

Could you not just have a case statement to define the number of days in each month or perhaps base it on the cal command? You will need a way to work out what last month is from the current date and/or prompt for input.

I must say I'm confused by the output you suggest. I cannot see how you generate it from the input. Are you trying to summarise some figures for each day that are recorded in a mixed up input file from a number of tills/stores/areas? It's difficult to tell.

Can you elaborate a bit more? It might be possible to streamline the process with an awk but you would need to be very specific with the input, calculations and the output required.

Kind regards,
Robin

This was my first impulse too: why would you fumble around with some date -shenanigans when cal would provide all dates in a given month from the start?

I hope this helps.

bakunin

With this sort of thing I like to just get epoch times for the dates I'm working with and then use simple integer math to navigate like this:

#!/bin/ksh

TEMP=/file/home/test.txt
trap "rm -f $TEMP" SIGHUP SIGINT EXIT

day=$(printf "%(%s)T" "last month")
to=$(printf "%(%s)T" "this month")

while [ $day -lt $to ]
do
   w=$(printf "%(%Y-%m-%d)T\n" \#$day)
   XXX=$(grep $w /honey/logs/statistics/s* | wc -l )
   echo "S" $w " :"$XXX >> $TEMP
   let day=day+60*60*24
done