I need to find the date of next first Friday of the month and set as a variable in a bash script
ie -
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
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
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?
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:
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):
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.
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