Bash Display First Friday of the next month


 
Thread Tools Search this Thread
Operating Systems Linux Bash Display First Friday of the next month
# 1  
Old 12-21-2017
CPU & Memory Bash Display First Friday of the next month

Hello,

I need to find the date of next first Friday of the month and set as a variable in a bash script

ie -

Code:
FIRSTFRIDAY=$(date -dfirst-friday +%d)

I know date -dfirst-friday doesn't work, but unsure if I can use this / cal + awk or something else to find the right date of the following first Friday

Thanks in advance
# 2  
Old 12-21-2017
Welcome to the forum.

Ususally the search function gives you a good starting point, e.g. how far would this thread get you?

Or, look at the links at this page's bottom left under "More UNIX and Linux Forum Topics You Might Find Helpful"
# 3  
Old 12-21-2017
Hi RudiC,

I tried searching the forums and I have been searching google for a quiet a period of time with no luck - I am confused the thread you linked seems to show date + number of days? where I need to find specifically the first Friday of next month.

I found a similar answer but unsure how to make First instead of last

Code:
date +"$(cal -N |awk '/^Fr/ {print $(NF)}') %B"

from linuxquestions.org/questions/linux-general-1/how-to-find-display-out-last-friday%27s-date-of-the-month-4175454956/
# 4  
Old 12-21-2017
Funny you spent so much time searching...
Looking at the first suggestion beneath ( as already suggested...) gave me an answer. so less than 5 minutes... OK in ksh but gives you and idea...

What did not say why bash, and on what OS / version you are running or did I miss it?
# 5  
Old 12-21-2017
I wish to avoid installing additional software if there is something native that can be used ie date / cal

ksh is not available /bin/ksh: bad interpreter: No such file or directory

Operating system is Ubuntu 16.04.3 LTS

Using bash to insert this into an already existing bash script.

thanks
# 6  
Old 12-21-2017
You haven't told us what version of bash you're using...

The following works at least with ksh version 93u+ and later versions. I believe it will also work unchanged with a recent release of bash:
Code:
if [ $# -eq 2 ]
then	MONTH=$1
	YEAR=$2
else	read MONTH YEAR <<-EOF
		$(printf '%(%m %Y)T')
EOF
fi

for((i = 1; i <= 12; i++))
do
	read MONTH YEAR <<-EOF
		$(printf '%(%m %Y)T' "$MONTH/01/$YEAR next month")
EOF
	DOW1ST=$(printf '%(%u)T' "$MONTH/01/$YEAR")
	if [ $DOW1ST -eq 5 ]
	then	FIRSTFRIDAY=01
	else	FIRSTFRIDAY=0$(((13 - DOW1ST) % 7))
		[ $FIRSTFRIDAY = "00" ] && FIRSTFRIDAY=07
	fi
	printf '%s/%s/%s\n' "$MONTH" "$FIRSTFRIDAY" "$YEAR"
done

If you invoke it without arguments it will give you the 1st Friday of each month for the next year. For instance, when I run it today, it prints:
Code:
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

If you invoke it with two arguments specifying a month and a year, it will give you the 1st Fridays for the year following that month and year. For example:
Code:
ksh ./tester 6 2016

prints:
Code:
07/01/2016
08/05/2016
09/02/2016
10/07/2016
11/04/2016
12/02/2016
01/06/2017
02/03/2017
03/03/2017
04/07/2017
05/05/2017
06/02/2017

If that doesn't work with your version of bash, the following should come close to working for you, but is totally untested (since I don't have access to the GNU date utility):
Code:
if [ $# -eq 2 ]
then	MONTH=$1
	YEAR=$2
else	read MONTH YEAR <<-EOF
		$(date '+%m %Y')
EOF
fi

for((i = 1; i <= 12; i++))
do
	read MONTH YEAR <<-EOF
		$(date -d "$MONTH/01/$YEAR next month" '+%m %Y')
EOF
	DOW1ST=$(date -d "$MONTH/01/$YEAR" +%u)
	if [ $DOW1ST -eq 5 ]
	then	FIRSTFRIDAY=01
	else	FIRSTFRIDAY=0$(((13 - DOW1ST) % 7))
		[ $FIRSTFRIDAY = "00" ] && FIRSTFRIDAY=07
	fi
	printf '%s/%s/%s\n' "$MONTH" "$FIRSTFRIDAY" "$YEAR"
done

I assume that you'll be able to simplify either of these scripts to get what you want.
# 7  
Old 02-23-2018
Code:
year=2018
for month in {1..12}
do
    cal $month $year |awk -v month=$month -v year=$year '/Fr/{getline;if(NF==1){getline;}printf("%02d/%02d/%04d\n",month,$(NF-1),year);}'
done

output:
Code:
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

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