Grabbing Status In A Script

Posted Thursday, 11 January 2007, 11:48 am • Updated Friday, 26 January 2007, 12:58 pm

Continuing my earlier theme of Things Unixly, here’s another mildly novel technique that I don’t think many folks are too familiar with.

I’ve written a lot of shell scripts over the years. Back in 1999, I needed to write a script to back up my servers to a DLT jukebox. Sure—there’s commercial apps out there that’ll take care of all the nitty-gritty, but I’m the kind of guy who prefers to get the nitty-gritty under my nails. So I did it myself.

The details of the script are really immaterial. I had a small program that could issue control commands to the jukebox, and the script would step through each of the servers I needed to back up, and step through the DLT tapes along with it.

One of the first problems I ran across was, ‘how can I tell whether the backup was successful’—which went hand in hand with ‘if the backup wasn’t successful, I still need to have the jukebox move to the next tape’. But beyond that, I needed to know whether the tape actually—really and truly—had been moved into the slot it was supposed to move into after a backup was complete. If the control software had failed to move a just-written-to tape out of the recorder, I definitely did not want it to just continue merrily on its way, overwriting the backup it just made!

So how do you check the status of a command that’s been issued within a shell script? With the little-known

$?

Looks kind of silly, all alone on a line like that, doesn’t it? It’ll make more sense if I reproduce a small chunk of the script:

movejb.bin /dev/jb0 240 ${TAPE} > /dev/null 2>&1
STAT=$?
  if [ ${STAT} -ne 0 ] ;then
   echo "${ABORT}\
   \n\nUnload tape ${TAPE} failed exitcode ${STAT}"\
   | mail -t ${PAGER}
   exit 1
  fi

‘movejb.bin’ is the jukebox controller program. Redirecting standard out and standard error is, uh, a standard practice so that your script doesn’t spill out text on the console while it’s running. But what we care about is the line immediately after that—

STAT=$?

That line ‘grabs’ the status output of the command that just ran, and sets it within the variable ‘STAT’. We can then test the variable to see what that output code actually was. This works for almost any Unix command you throw at it. You can do the same with grep, to test whether it did or did not find the string you were looking for in its output.

The rest of the script should be reasonably self-explanatory to any Unix jock. In this portion of the script, if the status indicated that the tape had not been moved out of the recorder and back into its space in the jukebox, we knew a pretty critical error had occurred—the jukebox possibly locked up—so it was best to completely exit the backup script, and alert the poor sap on pager duty, usually me!

Beware though, there is no genuine standard for exit codes. As well, not all manpages even list what the expected exit codes might be after the command is run. Always check the manpage, but if you can’t, write a standalone snippet and test the output yourself—easily enough done for example with

STAT=$?
echo $STAT

Make sense? I hope so. If not, please let me know—it’s easy for me to gloss over critical parts of things like this, since it’s all practically hardwired in my brain.

Bad Form, Old Boy

Posted Tuesday, 09 January 2007, 10:39 pm

So, it seems I buggered-up the RSS feeds here yesterday, while playing with the Feedburner Feed Replacement plugin. Which means my massive audience of one or two missed out on the delicious ‘Apple’ post of earlier this evening. Heavens to Betsy! So, in truly bad form, I’m posting a throwaway entry here, to get the pipes unclogged, so to speak.

 

Apple Releases iPhone – A Grateful Nation Scratches Its Brow, er, Screen

Posted Tuesday, 09 January 2007, 8:25 pm • Updated Friday, 26 January 2007, 12:59 pm

To much fanfare, Apple Computer today released the iPhone. To be sure, it’s a sexy, innovative device—that’s what Apple does. But a quick look at the device and I, for one, can see that its boldest feature will also be its biggest shortcoming. That being? The touch-screen. Or as I like to call it, "That great, big, constantly smudged magnet for the business end of car keys, pens, eyeglass frames, pocket change, and in general anything sharper than your Aunt Tillie’s wit".

Certainly, those who fork over what promises to be a fatted-calf or two to own one of these little delights is going to be very, very careful with it. So, deprecate the potential scratching/gouging of the screen, since owners will keep likely it in a velvet-lined titanium case. Still—it’s a touch-screen. You operate the device by touching the screen, sliding your finger around on screen.

Just as a for-example, here’s a photo of the screen of my Treo 650, after using my finger to operate the touchscreen for a while (which should properly be operated with the included stylus):

You get the idea…

As Gomer Would Say…

Posted Monday, 08 January 2007, 9:42 pm • Updated Friday, 26 January 2007, 1:00 pm | 1 comment

"Surprise, surprise, surprise!"

http://www.house.gov/house/floor/thisweek.htm

(Update—the link above is static, so the content regularly changes. I just checked, and they have already modified the page to obscure the fact that they were not in session monday. Yesterday, it specifically stated that no session was scheduled for Monday, January 8th. Now it simply skips listing monday. Here’s a still image of the current page, since by next week it’ll be completely gone: http://klaatu.anastrophe.com/housesched.jpg )

The pledge that congress would  meet with that ever-so tough ‘five days a week’ schedule, broken after the first week! Reports are that the day was scheduled off due to some college football game that’s of great interest to the members of congress—ah yes, there it is, the Florida Gators vs the Ohio State Buckeyes, played this evening.

Gosh, it sure would be handy if we all could just schedule the day off whenever there’s an "important" event due to take place. I think I’ll officially schedule tomorrow off for myself so that I can pick some daisies.

Politicians. Ya gotta love ‘em. Or do you?

Nasty Habits

Posted Sunday, 07 January 2007, 7:50 pm • Updated Friday, 26 January 2007, 1:01 pm | 1 comment

Confession time. I have a nasty habit. It’s filthy, it’s hard to control, I’m guilty of indulging it at just about any time, and I’m really, really, trying to get this monkey off my back.

It’s called "lazy caps".

I’ve been doing it for years. It started innocently enough as merely a bit of rebelling against The Man. I mean, what’s the point? The word "Point" and the word "point" mean exactly the same thing. There’s no difference whatsoever. So if the meaning never changes, why do I have to capitalize point just because it’s the first word of a sentence? Only Fools need Rules, preach it!

The problem is…well, the problem is it makes one look poorly educated and…lazy. Thus, ‘lazy caps’.

Admittedly, I am indeed lazy. No question about it. But I always excused it as being a pragmatic response to expediency. That extra throw of the little finger just to capitalize a letter—it’s wasted effort dang it! And I’m a busy, busy guy! I can’t be troubled with these little nuisances. I have information to share, time is of the essence!

Yeah, right.

Why an entire post about it? To alert my faithful readership that you may note that almost every post here has been – or will be – updated to correct as much of the lazy caps as I can find. So as you’re eagerly absorbed in merriment reading through my vast archives, don’t be alarmed that I’ve gamed the posts to add savory bits for your further titillation. No sir. I’m merely correcting ‘i’ to ‘I’ a few hundred thousand times.

 

 

Made with WordPress and the Semiologic CMS | Design by Antonella Pavese