[ale] Linux shell process

mike at trausch.us mike at trausch.us
Fri Mar 16 16:29:44 EDT 2012


On 03/16/2012 03:37 PM, Jim Butler wrote:
> Suppose you have a list of 10 birthdays for 10 different friends, and 
> you wanted to create a task from within a bash shell environment that 
> would alert you as to when each friend's birthday came around. How would 
> you do it?

First, this is really too terribly vague.  My "answer" would be to ask
questions.  However, since that is not an option here, the thing I would
do is to have a file (~/.config/bdays.txt for example) with four fields
in it:

SURNAME GIVEN_NAME MM-DD YYYY

The reason I would put it surname first is to easily be able to use the
sort command to sort the file by surnames if desired.

Then, at login (say, in ~/.bashrc or ~/.profile or ~/.bash_profile,
depending on the options the shell was built with and where in the login
process this would be desired) I would put the following:

================================
function show_birthdays() {
    FILE="$1"

    CUR_MMDD=$(date +%m-%d)
    BIRTHDAYS=$(cat "${FILE}"|grep ${CUR_MMDD})

    if [ "${BIRTHDAYS}" = "" ]; then exit; fi

    for bday in "${BIRTHDAYS}"; do
	NAME=$(echo "${bday}"|awk '{ print $2 " " $1 }')
	AGE=$(($(date +%Y)-$(echo ${bday}|awk '{ print $4 }')))

	printf "%s is %d years old today!\n" "${NAME}" "${AGE}"
    done
}
================================

Then call the function:

================================
show_birthdays "${HOME}/.config/bdays.txt"
================================

On days with no birthdays, nothing shows up.

Using this test file:

================================
Foobar Spamneggs 03-16 1927
Plopp Booker 03-17 1933
================================

I will get this if I run it today:

================================
mbt at aloe ~ $ ./bdays.sh
Spamneggs Foobar is 85 years old today!
================================

And for tomorrow:

================================
mbt at aloe ~ $ TZ=Hongkong ./bdays.sh
Booker Plopp is 79 years old today!
================================

For any other day, there will be no output.

	--- Mike

-- 
A man who reasons deliberately, manages it better after studying Logic
than he could before, if he is sincere about it and has common sense.
                                   --- Carveth Read, “Logic”

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 729 bytes
Desc: OpenPGP digital signature
Url : http://mail.ale.org/pipermail/ale/attachments/20120316/c9a0eae0/attachment.bin 


More information about the Ale mailing list