2

I need only the business date (Mon-Fri) from the below (in bash)

#!/bin/bash
startdate=20141030
enddate=20141120

dates=()
for (( date="$startdate"; date != enddate; )); do
    dates+=( "$date" )
    date="$(date --date="$date + 1 days" +'%Y%m%d')"
done
echo "${dates[@]}"

1 Answer 1

3

Change your script the following way:

#!/bin/bash
startdate=20141030
enddate=20141120

dates=()
for (( date="$startdate"; date != enddate; )); do
    if [ "$(date --date="$date" +'%u')" -le "5" ]; then
      dates+=( "$date" )
    fi
    date="$(date --date="$date + 1 days" +'%Y%m%d')"
done
echo "${dates[@]}"

I just added the if-clause before you add the date to the array. The %u sequence of date represents:

    %u     day of week (1..7); 1 is Monday

So, if that value is less or equal (-le) to 5, it is monday - friday.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .