Shell script loop to execute some task for business day and previous business day. If previous business day is weekend then script should skip the weekend date and pick Friday as a previous business day date.

#!/bin/bash
N=5
for i in $(seq $(($N + $N / 5 * 2)) -1 1)
do
    j=$(( $i+1 ))
    [ `date --date="-$i day" +%u` -le 5 ] && business_day=$(date -d "-$i day" +"%Y-%m-%d")
    [ `date --date="-$j day" +%u` -le 5 ] && pre_business_day=$(date -d "-$j day" +"%Y-%m-%d")
    if [[ $business_day > $pre_business_day ]] ;
    then
        d1=$(date --date "$business_day" +"%a")
        d2=$(date --date "$pre_business_day" +"%a")
        echo "Business Day: $business_day Day: $d1, Previous Business Day: $pre_business_day Day: $d2"
        echo " "
    fi 
done

Output:

e.g If Monday is business day and previous business day should be Friday.


Reference: https://unix.stackexchange.com/a/240960/56407

(Visited 328 times, 2 visits today)