Bash Display First Friday of the next month


 
Thread Tools Search this Thread
Operating Systems Linux Bash Display First Friday of the next month
# 8  
Old 02-26-2018
Bash/Gnu Date version
Code:
offsets=(5 4 3 2 1 0 6)
year=2018
for month in {01..12}
do
   date +"%m/%d/%Y" --date "$year/$month/01 + ${offsets[$(date +%w --date ${year}/${month}/01)]} days"
done
01/05/2018
02/02/2018
03/02/2018
04/06/2018
05/04/2018
06/01/2018
07/06/2018
08/03/2018
09/07/2018
10/05/2018
11/02/2018
12/07/2018

Of course, the disadvantage of my solution is that you are running date twice.
Or:
Code:
starts=(6 5 4 3 2 1 7)
year=2018
for month in {01..12}
do
    dstart=$(date +%w --date "$year/$month/01")
    printf "%02d/%02d/%d\n" ${month#0} ${starts[$dstart]} $year
done

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to add decimal month to some month in sql, php, perl, bash, sh?

Hello, i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m i need to write this script in php or bash or sh or mysql or perl in normal time o unix time i`m asking or there are any simple way to add partial number of month to some... (14 Replies)
Discussion started by: bacarrdy
14 Replies

2. Red Hat

How to find/display out last Friday's date of the month?

Hello, Can you please help me find/display out last Friday's date of the month using command in Unix/Linux (3 Replies)
Discussion started by: sunnysthakur
3 Replies

3. UNIX for Dummies Questions & Answers

[Solved] Cron - job to run every 3rd Friday of the month only

Hi Expert Please help me to set a cron job schedule, Ihave a job that run every 3rd Friday of the month at 1030am. I tried to set up like this, but the job still runs every friday at 1030am. I want the job to run every 3rd Friday of the month at 1030am only 30 10 15,16,17,18,19,20,21... (2 Replies)
Discussion started by: kaibiganmi
2 Replies

4. Shell Programming and Scripting

cron job to run on second to last friday each month

I needed a cron job to run on the second to last friday of every month. Our servers are running HP-UX, and the HP-UX date command is pretty basic and does not have all of the fancy options that Linux date command does, and it does not have the ability at all to return future dates. So I had to... (0 Replies)
Discussion started by: lupin..the..3rd
0 Replies

5. Shell Programming and Scripting

Display month for Previous day

Hello - I have one question regarding the date. I wanted to display the month name for previous day. The output should be as follows... 5-Feb-09 => February 1-Feb-09 => January 28-Feb-09=> February Here is the code i am using to get the output.... date '+%m %d %Y' | { read MONTH DAY... (4 Replies)
Discussion started by: govindts
4 Replies

6. UNIX for Dummies Questions & Answers

How to enter month name and display no.

SPls help me. Need a simple shell script. To enter month name and display its no. eg: If i enter january. Then answer should be 1. (7 Replies)
Discussion started by: siddhesh2515
7 Replies

7. Shell Programming and Scripting

Last friday of every month

Hi, I need to get the date of last friday of every month. how can i achieve this ? please guide me. Thanks in advance (3 Replies)
Discussion started by: apsprabhu
3 Replies

8. Shell Programming and Scripting

how can i find the third friday of each month?

Help please! I need to read the calendar and put the date of the third Friday of each month into a variable for comparison in an "if" statement. How would I do this? Thnx, leslie02 (10 Replies)
Discussion started by: leslie02
10 Replies

9. Shell Programming and Scripting

Need help, Every friday in a month

I am trying to write a script that shows every Friday in a month. I used cal $1 $2 | grep -v "^$" | awk '{print $6}' It doesn't work for the frist week of Friday because calendar command output has some spaces in the first line and awk '{print $6}' doesn't work. Anybody help me with this... (3 Replies)
Discussion started by: LAY
3 Replies

10. Shell Programming and Scripting

display files created in a particular month

hi, i m new to unix. I have been trying to find all the files in my home directory and its subdirectories that are created in the month of september. Can anyone please help me with this??? (1 Reply)
Discussion started by: t_harsha18
1 Replies
Login or Register to Ask a Question
CAL(1)							    BSD General Commands Manual 						    CAL(1)

NAME
cal, ncal -- displays a calendar and the date of Easter SYNOPSIS
cal [-3hjy] [-A number] [-B number] [[month] year] cal [-3hj] [-A number] [-B number] -m month [year] ncal [-3hjJpwy] [-A number] [-B number] [-s country_code] [[month] year] ncal [-3hJeo] [-A number] [-B number] [year] ncal [-CN] [-H yyyy-mm-dd] [-d yyyy-mm] DESCRIPTION
The cal utility displays a simple calendar in traditional format and ncal offers an alternative layout, more options and the date of Easter. The new format is a little cramped but it makes a year fit on a 25x80 terminal. If arguments are not specified, the current month is dis- played. The options are as follows: -h Turns off highlighting of today. -J Display Julian Calendar, if combined with the -e option, display date of Easter according to the Julian Calendar. -e Display date of Easter (for western churches). -j Display Julian days (days one-based, numbered from January 1). -m month Display the specified month. If month is specified as a decimal number, it may be followed by the letter 'f' or 'p' to indicate the following or preceding month of that number, respectively. -o Display date of Orthodox Easter (Greek and Russian Orthodox Churches). -p Print the country codes and switching days from Julian to Gregorian Calendar as they are assumed by ncal. The country code as deter- mined from the local environment is marked with an asterisk. -s country_code Assume the switch from Julian to Gregorian Calendar at the date associated with the country_code. If not specified, ncal tries to guess the switch date from the local environment or falls back to September 2, 1752. This was when Great Britain and her colonies switched to the Gregorian Calendar. -w Print the number of the week below each week column. -y Display a calendar for the specified year. -3 Display the previous, current and next month surrounding today. -A number Display the number of months after the current month. -B number Display the number of months before the current month. -C Switch to cal mode. -N Switch to ncal mode. -d yyyy-mm Use yyyy-mm as the current date (for debugging of date selection). -H yyyy-mm-dd Use yyyy-mm-dd as the current date (for debugging of highlighting). A single parameter specifies the year (1-9999) to be displayed; note the year must be fully specified: ``cal 89'' will not display a calendar for 1989. Two parameters denote the month and year; the month is either a number between 1 and 12, or a full or abbreviated name as speci- fied by the current locale. Month and year default to those of the current system clock and time zone (so ``cal -m 8'' will display a calen- dar for the month of August in the current year). Not all options can be used together. For example ``-3 -A 2 -B 3 -y -m 7'' would mean: show me the three months around the seventh month, three before that, two after that and the whole year. ncal will warn about these combinations. A year starts on January 1. Highlighting of dates is disabled if stdout is not a tty. SEE ALSO
calendar(3), strftime(3) HISTORY
A cal command appeared in Version 5 AT&T UNIX. The ncal command appeared in FreeBSD 2.2.6. AUTHORS
The ncal command and manual were written by Wolfgang Helbig <helbig@FreeBSD.org>. BUGS
The assignment of Julian-Gregorian switching dates to country codes is historically naive for many countries. Not all options are compatible and using them in different orders will give varying results. BSD
March 14, 2009 BSD