14.06.2014 Views

Unix Power Tools

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

28.16<br />

As an example, here’s a series of commands that puts a listing of the current<br />

directory into a temporary file, emails the listing, then overwrites the previous<br />

version of the file:<br />

$ ll > $tf-1; mail -s backup joe < $tf-1; mv $tf-1 listing<br />

I can repeat that same command later by using a history substitution (30.8)<br />

like !ll.<br />

• It’s useful with sleep (25.9) to run a command after a delay. The next example<br />

shows a series of commands in a C shell alias that you might use to print<br />

a warning and give the user a chance to abort before the last command (exit,<br />

which ends the current shell) is executed. Be sure to read the important note<br />

after this example:<br />

alias bye 'echo "Type CTRL-c to abort logout"; sleep 10; exit'<br />

Note that, in C-type shells and older Bourne-type shells, pressing your interrupt<br />

key (24.10)—like CTRL-c—will stop execution of all jobs on the current<br />

command line. The alias above works in shells like that. But in some shells,<br />

like bash2, interrupting a command in a string of commands separated by<br />

semicolons will affect only that single command. So I couldn’t rewrite the<br />

alias above for bash2 because, if I pressed CTRL-c while the sleep command<br />

was executing, that would simply abort sleep—and proceed to run exit,<br />

which would log me out immediately!<br />

• If you’re running a series of commands that take some time to complete,<br />

you can type all the commands at once and leave them to run unattended.<br />

For example, I have little shell scripts named nup and ndown (24.22) (which<br />

run /sbin/ifup and /sbin/ifdown, respectively) to start and disable the network.<br />

On a system with a dialup modem and a long file transfer to perform,<br />

it’s nice to be able to type a series of commands that bring up the network,<br />

do a couple of file transfers, then bring down the network. I can type this<br />

string, go about my business somewhere else, and come back later:<br />

$ nup;ptbk;getmail;ndown<br />

After nup returns, the network is up (the modem has connected). So the<br />

shell runs ptbk (38.9) to make a backup of my work on this book. Next,<br />

getmail gets my email (it basically runs fetchmail). When getmail finishes,<br />

ndown hangs up the modem. This can take several minutes from start to finish,<br />

but the shell manages it all while I do something else. (If I didn’t have a<br />

windowing system with multiple xterms, I could have put that string of commands<br />

into a subshell (43.7) in the background (23.2).) This is one place that a<br />

GUI interface for network control really loses to command-line utilities and<br />

the shell.<br />

Two related operators, && and || (35.14), work like a semicolon, but they only<br />

execute the next command if the previous one succeeded or failed, respectively.<br />

—JP<br />

564 Part VI: Scripting<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2009 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!