Script Teez: Uptiming your e-mail

In this month's Script Teez, Vincent Danen shows you a Perl script that includes your current Linux uptime in your e-mail signature.

This month, we will look at a Perl script that includes your current Linux uptime in your e-mail signature. Because it’s a simple Perl script, you can use this for e-mail, news, or pretty much anything else that strikes your fancy. It makes use of a little program called linux_logo, which is available from Project Glue and is written by Vince Weaver.

You’ll need to downloadlinux_logo and install it to make use of the uptime features of this little script. It’s basically a quick and dirty way to get uptime information in your e-mail signature without a lot of added complexity—which makes it great for people learning the basics of scripting in general and Perl in particular.

The following is the script you’ll need to write. Save it as Omit the line numbers at the beginning of each line. They are there simply for reference and are not actual parts of the script.
1: #!/usr/bin/perl
3: $home = $ENV{"HOME"};
5: # get current uptime
6: $uptime = `linux_logo _g _a _F "#U"`;
7: @uptime = split(/Uptime /,$uptime);
8: @disp = split(/minutes/,$uptime[1]);
9: $disp = "Current Linux uptime: $disp[0]minutes.";
11: if ($#ARGV lt 0 or $#ARGV gt 1) {
12: print "Usage: $^X sigfile\n";
13: exit(1);
14: }
16: open (SIG, "$ARGV[0]") or die "Can't open $ARGV[0]";
17: while (<SIG>) {
18: $sig .= $_;
19: }
21: print "$sig\n\n$disp\n";
23: #open (SIG,">$home/.sig");
24: #print SIG "__ \n$sig\n\n$disp";
25: #close SIG;
27: exit(0);

Line by line
NOTE: The (`) character is not a single quote but a backquote, which is on the tilde (~) key.

The first line of this script points to your Perl interpreter, which is usually /usr/bin/perl or /usr/local/bin/perl. The third line sets the value of the $home variable to your current home directory. This way, any user on your system can use it without worrying about hard-coding the home directory.

The sixth line calls linux_logo with a few simple parameters. See the man page for linux_logo for the many other command-line options you can use. In this case, we are simply giving linux_logo the commands to provide system information only (no displaying the interesting ANSI penguin it generates by default) and to make the display ASCII instead of ANSI (no color). We also provide it with a format string of #U, which tells it to display the uptime only. Give it a try. On the command line, type:
linux_logo -g -a -F #U

and you’ll see that the resulting output is something like:
Uptime 20 hours 57 minutes

Now, this isn’t very interesting, so we’re going to take that output, format it a bit to make it a little prettier, and then incorporate it into our signature. Keep in mind, however, that to use the output from another program, we must place the command within single back quotes (the character on the same key as the tilde) with Perl. This will take the output of the command we place inside the back quotes and assign it to the variable, which in this case is $uptime.

Line 7 takes the value of $uptime and splits it into an array, which we call @uptime. (The @ symbol references an array in Perl.) In this case, we are splitting the variable $uptime into two parts, consisting of the spaces preceding the word Uptime and the text after that word. We do this because linux_logo centers its display, and we don’t want our text centered. We now have two variables: $uptime[0], which is just blank spaces, and $uptime[1], which contains the number of hours and minutes the system has been up.

Line 8 takes the value of $uptime[1] and splits it into another array, this time called @disp. We do this for the same reason we split $uptime back on line 7. We don’t need the trailing spaces from linux_logo’s formatting, so we drop them and use the word minute as our split marker. We now have two variables: $disp[0], which contains the number of hours and minutes (less the actual word minutes or minute), and $disp[1], which will contain only spaces or the letter s and a series of blank spaces.

On line 9, we create a new variable called $disp, which contains the text Current Linux uptime: followed by the value of $disp[0]. Using the above output of linux_logo, $disp[0] would contain the words 20 hours 57. Because there’s a space at the end of the string, we don’t put a space between $disp[0] and the word minutes when we create our variable. So, the value of:
$disp = ‘Current Linux uptime: $disp[0]minutes.’;

Current Linux uptime: 20 hours 57 minutes.

Since this is supposed to be tagged onto the end of a signature, we have to call with a single argument—namely, the file to append the uptime information to. Line 11 checks to make sure that we pass only one argument to the file. If you were to pass no arguments, or more than one argument, line 12 displays the syntax required to run the program, and line 13 exits with an errorlevel of 1.

Line 16 tries to open the signature file and assigns it to the file handle SIG. Lines 17 through 19 read the file associated with the file handle SIG line by line until it reaches the EOF (End of File) marker. The signature is assigned, line by line, to the variable $sig, as seen on line 18.

Finally, on line 21, we print the signature, add two line feeds to give us a blank line between the signature and the uptime display (the \n character in the string is the newline character), and finally print the uptime display variable, $disp.

Lines 23 through 25 can be used to write the signature and uptime display to a specified file in the home directory called ~/.sig. This might be necessary for some e-mail or news clients that will not read the signature from STDOUT. Because line 21 prints the signature to STDOUT (or standard output), the program calling must be able to read STDOUT as input. If the program you use does not do this, you may choose to uncomment lines 23 through 25 and tell your e-mail or news client to use ~/.sig as the signature file. To keep the signature file from getting stale, you may choose to run in a cron job every five minutes so that it is accurate within five minutes.

Finally, line 27 exits the program with errorlevel 0, indicating that everything worked perfectly.

Obviously, using a client that can read STDOUT as the signature input will give you more accurate results. Because this script is so simple, you could probably even have it run as a cron job every minute without having any adverse results.

And now you know
And that’s it! Now you know how to write a very simple Perl script to insert the current system uptime into your e-mail or news signatures. Not very useful, but nice to use when you want to brag to friends how long your Linux system has been running. Enjoy!

Vincent Danen, a native Canadian in Edmonton, Alberta, has been computing since the age of 10, and he’s been using Linux for nearly two years. Prior to that, he used OS/2 exclusively for approximately four years. Vincent is a firm believer in the philosophy behind the Linux "revolution,” and heattempts to contribute to the Linux causein as many ways as possible—from his FreezerBurn Web site to building and submitting custom RPMs for the Linux Mandrake project. Vincent also has obtained his Linux Administrator certification from Brainbench .He hopes to tackle the RHCE once it can be taken in Canada.

The authors and editors have taken care in preparation of the content contained herein, but make no expressed or implied warranty of any kind and assume no responsibility for errors or omissions. No liability is assumed for any damages. Always have a verified backup before making any changes.


Vincent Danen works on the Red Hat Security Response Team and lives in Canada. He has been writing about and developing on Linux for over 10 years and is a veteran Mac user.

Editor's Picks

Free Newsletters, In your Inbox