E-Mail:

More Signature Fun

More Signature Fun

Email signatures are something we’ve covered in earlier Penguin Shell issues. Recently, though, I’ve had a lot if inquiries into how my personal email signature is generated. If you’ve sent me an email and gotten a response (fairly rare in the past week or so), you’ve seen it. At this minute, it looks like this:

Tony Steidler-Dennison
Partner, Fullbrain Technologies, LC
Author, Lockergnome Penguin Shell

__–__–__–__–__–__–__–__–__–

Registered Linux User #247072
Current Uptime Stats:
  10:53am up 7 days, 3:02, 3 users, load average: 0.00, 0.00, 0.00

–__–__–__–__–__–__–__–__–__

Freedom’s just another word for nothing left to lose.
      – Kris Kristofferson, “Me and Bobby McGee”

I said, “at this minute,” because it changes every 60 seconds. By utilizing both the uptime and fortune programs, you, too can generate a signature that changes from minute to minute. Let me first show you the script that makes this possible.

#!/bin/sh

# copy the signature header to the /home/tony/.nsig file
cp /home/tony/sig-stuff/header /home/tony/.nsig

# execute the uptime command and append
# the /home/tony/.nsig file with the results.
uptime >> /home/tony/.nsig

# copy the signature footer to the /home/tony/footer file
cp /home/tony/sig-stuff/footer /home/tony/footer

# execute the fortune program and append
# the /home/tony/footer file with the results
/usr/games/fortune >> /home/tony/footer

# read the /home/tony/footer file and append
# the results to the /home/tony/.nsig file
cat /home/tony/footer >> /home/tony/.nsig

# clean things up a bit
rm -f /home/tony/footer

# you’re outta here
exit

This, really, makes fun use of the ability in Linux to manipulate file contents. Let’s take a look at how this script works to generate a random signature.

First, I’ve created a file in /home/tony/sig-stuff called header. It looks like this:

Tony Steidler-Dennison
Partner, Fullbrain Technologies, LC
Author, Lockergnome Penguin Shell

__–__–__–__–__–__–__–__–__–

Registered Linux User #247072
Current Uptime Stats:

This is the first chunk of the signature. In fact, it’s the only element that remains unchanged from email to email. The first action in the script is to copy this file to the /home/tony directory with the name .nsig. This will start the “random” signature. Remember, we’re building this signature file one step at a time.

Next, we execute the uptime command to display, well, the current uptime stats on the system. This file is located in a directory in the PATH variable, so we don’t need to include the full path to the program. Rather than displaying the results of the fortune command to the screen, we add it to the end of the .nsig file we just created. The signature now looks like this:

Tony Steidler-Dennison
Partner, Fullbrain Technologies, LC
Author, Lockergnome Penguin Shell

__–__–__–__–__–__–__–__–__–

Registered Linux User #247072
Current Uptime Stats:

  10:53am up 7 days, 3:02, 3 users, load average: 0.00, 0.00, 0.00

So, we’ve added real time data to a static file by appending the results of uptime to .nsig.

Next, I copy the footer file from /home/tony/sig-stuff to the file /home/tony/footer. In its unaltered state, this file looks like this:

–__–__–__–__–__–__–__–__–__

Really. That’s it. All the footer file does is serve as the divider between the data we’ve already added and the data we’re about to add. The script executes the fortune program and, again, appends the output to the footer file rather than displaying it to the screen. The actual signature file remains unchanged, though we’ve now built the footer to add to the overall signature.

Next, we pull the two files together. Remember, the signature currently exists as two files - .nsig and footer. You can probably make a good guess as to how we’re going to make this one big file. Using cat, we’ll read the footer file and append the output to the .nsig file:

    cat /home/tony/footer >> /home/tony/.nsig

With that done, the signature file has all the elements; the static signature, the current system uptime and a random quote or thought from the fortune program. All that’s left is to clean things up a bit by removing the existing /home/tony/footer file and exit the program.

If you’ve created this script within the /home/user/bin directory, you’ll need to make sure it has executable permissions. Simple enough:

    chmod u+x sigscript.sh

For redunancy’s sake, you’ll probably want to check to be sure that both the header and footer files have read permissions, as well.

The next step in generating this signature is to add execution of the script to the /etc/crontab file. Using your favorite editor as root, open the /etc/crontab file and add the following line, making adjustments for the location of the sigscript.sh file on your machine:

# generate a custom email sig every minute
* * * * * tony sh /home/tony/bin/sigscript.sh

As always, the first line is a comment to yourself, making it much easier to recognize the intent of the crontab entry when you look at it later. The crontab entry itself executes the signgen.sh file you’ve just created, provided you’ve pointed crontab to the right directory. Quite simply, it executes the file every minute of every day of every month ( * * * * * ) using the tony user.

Does it work on your machine? You should find a .nsig file in your home directory with all the components of the signature pulled together. If the file’s there, use a text editor to check its contents.

Finally, you’ll want to choose the .nsig file as the default signature file in your email program of choice. They vary so widely that I won’t even venture into covering making this change.

And there you have it - a multipart random signature file you “set and forget.” It’s a great example of using the power of Linux to manipulate text files.

What Do You Think?

 

Want to Start a Blog Here for Free?

Are you an expert in one subject or another? If your goal is to help others and dispense hard-earned information back to the community, stake a claim on your very own Lockergnome blog today! You can write about anything - no matter the topic. Sign-up to start blogging!

64 queries / 0.309 seconds.