change the number of week by the range of dates in bash
This will work on linux with bash, assuming that by "week number", you mean the ISO week number. ISO weeks start on Monday and a week counts as part of a year if four of its days are in the year.
dates_for_week() {
printf "January 1 $1 %+d days\n" {-3..368} |
date +%G%V_%Y-%m-%d -f - |
grep $(printf %d%02d $1 $2) |
cut -c8-
}
EDIT: Here's a version which does less work to get the same result:
dates_for_week () {
printf "$1-01-01 +$2 weeks %+d days\n" {-10..2} |
date +%V_%Y-%m-%d -f - |
grep '^0\?'$2_ |
cut -c4-
}
eg:
$ dates_for_week 2013 1
2012-12-31
2013-01-01
2013-01-02
2013-01-03
2013-01-04
2013-01-05
2013-01-06
$ dates_for_week 2013 46
2013-11-11
2013-11-12
2013-11-13
2013-11-14
2013-11-15
2013-11-16
2013-11-17
$ dates_for_week 2013 52
2013-12-23
2013-12-24
2013-12-25
2013-12-26
2013-12-27
2013-12-28
2013-12-29
How to send out current week in shell scripts
The statement
WEEK_NUMBER = %V
(with spaces) will try to run the WEEK_NUMBER
program, passing =
and %V
as arguments.
What you need is what you have on the OUTPUT_SUMMARY
line above, an assignment with no spaces:
WEEK_NUMBER=%V
In any case, that would simply set WEEK_NUMBER
to the literal %V
. Since those %
-arguments are meant for the date
command, you should probably use:
WEEK_NUMBER=$(date +%V) # Store output of date week-no.
YEAR=$(date +%Y) # Store output of date year.
WORK_WEEK=${YEAR}.${WEEK_NUMBER} # Construct from variables.
Of course, if the only thing you want to use is the WORK_WEEK
variable, it can be simplified quite a lot, as date
allows you to insert arbitrary text into its output:
WORK_WEEK=$(date +%Y.%V)
This will also get rid of the potential bug when running your script very late on December 31st. In that case, it's possible for the week number is extracted in year N
and the year extracted in year N+1
. Unlikely, yes, but I'm paid to worry about such things :-)
How to cut the first Sunday to Saturday of each month in a year?
I suggest the following alternative to awk:
#! /usr/bin/env bash
for month in {01..03}; do
for day in {01..13}; do
date -d "2020-$month-$day" '+%A %F'
done |
grep -A6 -m1 -F Sunday
done
The script is not very efficient, but does the job. For each month, we simply print the dates of the 13 first days in that month. We know that the green zone has to be in that area, therefore we do not need the remaining days of the month.
The date format is Weekday YYYY-MM-DD
. We use grep
to find and print the first Sunday, print the 6 days behind that Sunday (-A6
) and exit because we limited the search to one match (-m1
).
The procedure described above is done for each of the months 1 to 3.
How to pass a range of dates through the for loop in bash?
Don't do it yourself. Dates are tricky, months have variable number of days, etc. Your script also needs additional handling of years. Let date
handle calculations with dates.
for i in {1..7}; do ./scriptname.sh $(date --date="-$i days" "+%Y %m %d"); done
.g the week running from 29th March through 4th April
The way I would do it is to take 29th Match as number of seconds since epoch. Then add half a day so that leap seconds if any are not an isssue. Then just increment it by number of seconds in a day and use date
to convert it back to a date.
then=$(date --date='2020/03/29 12:00:00' +%s);
for i in {0..6}; do
./scriptname.sh $(date --date="@$((then + $i * 60 * 60 * 24))" "+%Y %m %d");
done
Alternatively, you can do it without then
variable, but you have to remember that the number +0
immediately coming after date is interpreted as timezone.
for i in {0..6}; do ./scriptname.sh $(date --date="2020/03/29 12:00:00 +0 +$i days" '+%Y %m %d'); done
Because I like streaming-like parsing in bash, I would parse the output with xargs
:
seq 6 | xargs -I{} date -d '2020/03/29 12:00:00Z +{} days' '+%Y %m %d' | xargs -n3 ./scriptname.sh
Get the date (a day before current time) in Bash
Sorry not mentioning I on Solaris system.
As such, the -date switch is not available on Solaris bash.
I find out I can get the previous date with little trick on timezone.
DATE=`TZ=MYT+16 date +%Y-%m-%d_%r`
echo $DATE
Related Topics
Profiling Arbitrary Cuda Applications
How to Use/Learn Video4Linux2 (On Screen Display) Output APIs
Extract Parent Domain Name from a List of Url Through Bash Shellscripting
How to Change Qmake Prefix Location
Cmake: Don't Set Rpath for a Single Library Used in Link
Detect When Reader Closes Named Pipe (Fifo)
What Is The Equivalent of _Emit on Linux
System Calls: Difference Between Sys_Exit(), Sys_Exit and Exit()
Disable CPU Caches (L1/L2) on Armv8-A Linux
How to Change File Extension in Linux Shell Script
How to Compile a Node C++ Addon So That I Can Use Distribute It on Amazon Aws
Run 'Perf Stat' on The Output of 'Perf Record'
Error: You Must Install at Least One Postgresql-Client-<Version> Package
Difference Between Archiving and Compression
Update .Bashrc from Provisioning Shell Script with Vagrant
Tk Initialization Failed: No Display Name and No $Display Environment Variable