How to resolve the algorithm Last Friday of each month step by step in the UNIX Shell programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Last Friday of each month step by step in the UNIX Shell programming language

Table of Contents

Problem Statement

Write a program or a script that returns the date of the last Fridays of each month of a given year. The year may be given through any simple input method in your language (command line, std in, etc).

Example of an expected output:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Last Friday of each month step by step in the UNIX Shell programming language

Source code in the unix programming language

#!/bin/sh

if [ -z $1 ]; then exit 1; fi

# weed out multiple erros due to bad year
ncal 1 $1 > /dev/null && \
for m in 01 02 03 04 05 06 07 08 09 10 11 12; do
	echo $1-$m-`ncal $m $1 | grep Fr | sed 's/.* \([0-9]\)/\1/'`
done


#!/bin/sh

# usage: last_fridays [ year]

year=${1:-`date +%Y`}    # default to current year
month=1
while [ 12 -ge $month ]; do
    # Ensure 2 digits: if we try to strip off 2 characters but it still
    # looks the same, that means there was only 1 char, so we'll pad it.
    [ "$month" = "${month%??}" ] && month=0$month

    cal $month $year | awk '{print $6}' | grep . | tail -1 \
        | sed "s@^@$year-$month-@"

    # Strip leading zeros to avoid octal interpretation
    month=$(( 1 + ${month#0} ))
done


#!/bin/sh

# Free code, no limit work
# $Id: lastfridays,v 1.1 2011/11/10 00:48:16 gilles Exp gilles $

# usage :
# lastfridays 2012 # prints last fridays of months of year 2012

debug=${debug:-false}
#debug=true

epoch_year_day() {
	#set -x
	x_epoch=`expr ${2:-0} '*' 86400 + 43200`
	date --date="${1:-1970}-01-01 UTC $x_epoch seconds" +%s
}

year_of_epoch() {
	date --date="1970-01-01 UTC ${1:-0} seconds" +%Y
}
day_of_epoch() {
	LC_ALL=C date --date="1970-01-01 UTC ${1:-0} seconds" +%A
}
date_of_epoch() {
	date --date="1970-01-01 UTC ${1:-0} seconds" "+%Y-%m-%d"
}
month_of_epoch() {
	date --date="1970-01-01 UTC ${1:-0} seconds" "+%m"
}

last_fridays() {
	year=${1:-2012}

        next_year=`expr $year + 1`
        $debug && echo "next_year $next_year"

        current_year=$year
        day=0
        previous_month=01

        while test $current_year != $next_year; do

        	$debug && echo "day $day"

        	current_epoch=`epoch_year_day $year $day`
        	$debug && echo "current_epoch $current_epoch"

        	current_year=`year_of_epoch $current_epoch`

        	current_day=`day_of_epoch $current_epoch`
        	$debug && echo "current_day $current_day"

        	test $current_day = 'Friday' && current_friday=`date_of_epoch $current_epoch`
        	$debug && echo "current_friday $current_friday"

        	current_month=`month_of_epoch $current_epoch`
        	$debug && echo "current_month $current_month"

        	# Change of month => previous friday is the last of month
        	test "$previous_month" != "$current_month" \
        		&& echo $previous_friday
        	
        	previous_month=$current_month
        	previous_friday=$current_friday
        	day=`expr $day + 1`
        done
}

# main
last_fridays ${1:-2012}


  

You may also check:How to resolve the algorithm Sum and product of an array step by step in the SNOBOL4 programming language
You may also check:How to resolve the algorithm Active object step by step in the J programming language
You may also check:How to resolve the algorithm Inconsummate numbers in base 10 step by step in the 11l programming language
You may also check:How to resolve the algorithm Find the last Sunday of each month step by step in the Groovy programming language
You may also check:How to resolve the algorithm Sum and product of an array step by step in the Ol programming language