Shell script to write today’s and next weekday’s date

As is this script has limited purpose, and includes RTF markup not useful to anyone else other than as example.

But, what is useful is the date function that increments to the next ‘weekday’ day of week. Be aware that this script formats into Americanized short date (e.g., 2-10-21). Which may seem odd since I just wrote (and re-wrote a couple dozen times) a script to obliterate that format in file names. Except that I’m using this script to fill out the dates on an order form where the dates are expected to be the familiar, informal conversational format used in the US. Whereas for a file name in a computer, YYYY-MM-DD is a useful sorting method.

A lot of ‘others’ don’t take the time to realize that. I’ve run into vandalized man pages for date that attempt to make admonitions against the US informal conversational date format into some moral indictment. That format appears from the way one would speak the date in the US: February tenth, two thousand twenty one. The same as one would use more words to say: The tenth of February, two thousand twenty one. It’s not hard, Europeans. 😀

Also posting the plist used with launchd to run this script every morning.

#! /bin/sh

# Used with launchd to run every morning user is logged in.
# Runs "at load" in case log in happens after the scheduled time (8:15).
# Purpose: $outfile is meant to be placed as a linked text object in InDesign document.
# But, InDesign (CS2) won't keep text style when updating the link
# unless you load it up with (double escaped) RTF markup. :|

# ProTip: Use '$todate' and '$nextdate' as placeholder text in your
#         RTF file.


# Today's (formatted) date.
todate=$(date -j "+%m-%d-%y" | sed -E 's/0([0-9])/\1/g')

# Next weekday:
# Today's date +3 if today is Friday ('%w==5'); else
# Today's date +1 if today is not Friday.
nextdate=$(date -j -v+$(( ( $(date +%w)==5 )?3:1))d "+%m-%d-%y" | sed -E 's/0([0-9])/\1/g')

{\\\fonttbl\\\f0\\\fswiss\\\fcharset0 Helvetica;}

\\\f0\\\fs32 \\\cf0 $todate\\\


printf "$rtf" > "$outfile"

Launchd properties file (reflects the above code being named “” and placed into the user’s ‘Documents’ folder): ~/Library/LaunchAgents/date-today.write-to-file.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "">
<plist version="1.0">

Leave a Reply

Your email address will not be published. Required fields are marked *