| aug96.tar |
Questions and Answers
Bjorn Satdeva In the June column, there was a question about how to compute yesterday's date in a shell script. I answered that I did not know of a good way and recommended switching to Perl, if at all practical. I think that one answer generated more responses than any other comments and suggestions I have made in this column over the years. Most people pointed out that the date command from the GNU sh-utils is capable of doing this by just providing the appropriate arguments, such as:
% date Sun Jun 2 14:28:27 EDT 1996 % date _date yesterday Sat Jun 1 14:28:32 EDT 1996
Besides the suggestion to use the GNU sh-util date command, I received two other suggestions I would like to share, the first one because of its ingenuity and simplicity. Tom Henderson wrote: Regarding the question about getting the previous day's date in a shell: I picked up a tip for running a script on the last day of the month by "changing" the time zone for a command run via cron. The command makes the date report as the first of the month 24 hours early.
0 6 28-31 * * [ `TZ=PST-16PDT date +\%d` -eq 1 ] && echo "Last day of Month"
The user could do the same, in reverse, to get the date 24 hours earlier by setting TZ with an "hour adjustment" going in the opposite direction. Doing:
TZ=PST+32PDT date
would display the date for the previous day. Note: Henderson modified the +8 time adjustment by 24 hours because he's in the Pacific Standard Time Zone. Adjust as appropriate for the zone you're in. I like Henderson's solution for its simplicity. Unfortunately, it does not work across all platforms. On my workstation, it only works within plus or minus a 24-hour period. The TZ=PST+32PDT date gives me current day and time GMT, so be a bit careful with this one. The second suggestion, includes a complete set of Bourne shell functions that allow some manipulations of date and time from the shell. Judging from the large number of responses, dealing with date and time in a shell script is something many system administrators have been struggling with. Bill Gray wrote: In June's column, you had a question about producing yesterday's date in the Bourne shell. I believe you are right; there is no easy way, but the following will do it with difficulty (the functions are written for transparency, not efficiency or compactness). They have been tested, but not very extensively because the project they were written for was recoded in Perl(!). Perl's better, but perhaps the person who sent in the query is far along with sh scripts, or doesn't Perl. The full Bourne functions are shown in Listing 1. To use these functions, do this from a Bourne shell:
$ . yesterday.sh $ today $ echo Today: $Ydd $Ymm $Yyy Today: 02 06 96 $ echo Yesterday: "$Ydd $Ymm $Yyy" Yesterday: 01 06 96
Thanks to everybody who sent me email about this. Before I get to this month's questions, here's a small note on something completely different. For those of you interested in standards, I understand from the IEEE that the POSIX Software Administration document is now in the hands of the printers, with an official publication date of June 14, 1996. The tool of the month is arpwatch. On many sites, system administrators can have problems with systems that appear out of nowhere, for example when a user plugs a new PC into the network. This may happen without any notification to the system administrator, and the user may even choose an address that has been allocated for something else, making havoc out of the network. Arpwatch keeps track of Ethernet/IP address pairs and reports changes, such as a new host appearing on the network, via email. When somebody adds a new host, root will get email with the following content:
To: root Subject: new station (baldr.sysadmin.com) hostname: baldr.sysadmin.com ip address: 10.1.5.101 ethernet address: 0:a0:24:33:27:ff ethernet vendor: <unknown> timestamp: Sunday, June 16, 1996 20:43:43 +0100 (PDT)
And when that dreaded thing happens, and somebody configures a machine to use an address that has already been allocated elsewhere, the following email will be sent:
To: root Subject: changed ethernet address (baldr.sysadmin.com) hostname: baldr.sysadmin.com ip address: 10.1.5.101 ethernet address: 0:20:af:8d:a5:28 ethernet vendor: <unknown> old ethernet address: 0:0:0:33:27:ff old ethernet vendor: <unknown> timestamp: Sunday, June 16, 1996 21:25:36 +0100 (PDT) previous timestamp: Sunday, June 16, 1996 21:25:34 +0100 (PDT) delta: 2 seconds
Of course, then you have to find the guilty party, which in itself can be a nontrivial undertaking, but at least you will know what has happened.
Thank you for your time and insightful articles in Sys Admin.
% xhdr this is a test
on my workstation, the header of my xterm window will change to:
baldr.sysadmin.com: this is a test
You can use this tool to play all kinds of games, for example, if you place an alias like this in your .cshrc file:
alias vi xhdr !\* ; vi \!*; xhdr
then, when you are editing a file, you can see from the header which one it is. Before you start to add hundreds of aliases for all the commands you are using, remember that scanning the rc file takes time. So, use this with care.
The closest that I have come is a utility called WinTAR that can read/write tar onto a remote tape drive or read/write local files/floppies. The author says that reading/writing of local SCSI tape drives will not be available until Q4. Do you know of any free/share/inexpensive-ware that will run on a PC and will read/write tar (or cpio for that matter) tapes in a local SCSI drive?
ftp://prep.ai.mit.edu/pub/GNU/tar-1.11.8.tar.gz
As an alternative, the MKS toolkit has all the well-known UNIX utilities, but runs on MS-DOS. You can find more information at: http://www.mks.com.
If you are looking for a freeware package, your best bet is probably the System Resource Accounting package for BSD-flavored systems. It was developed at University of Rochester and presented at LISA V. The sources are available from:
ftp://ftp.cc.rochester.edu/pub/acc-src/acct-4.0
I doubt that this system will fit your needs as is, but may very well be a worthwhile starting place for building the software you need.
I suppose I could roll my own, but I'd hate to reinvent the wheel. Any help or thoughts would be appreciated.
Nevertheless, such a utility would be really useful. If any of our readers knows of a freeware implementation of this or a similar utility, please let me know. In the meantime, stay tuned for news.
About the Author
Bjorn Satdeva is the president of /sys/admin, inc., a consulting firm which specializes in large installation system administration. Bjorn is also co-founder and former president of Bay-LISA, a San Francisco Bay Area user's group for system administrators of large sites. Bjorn can be contacted at /sys/admin, inc., 2787 Moorpark Ave., San Jose, CA 95128; electronically at bjorn@sysadmin.com; or by phone at (408) 241-3111.
|