25.03.2013 Views

Copyright Sams Teach Yourself Shell Programming in 24 Hours

Copyright Sams Teach Yourself Shell Programming in 24 Hours

Copyright Sams Teach Yourself Shell Programming in 24 Hours

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

If you want to list the contents of this tar file, you can <strong>in</strong>voke the script as follows:<br />

$ ./listtar fruits.tar<br />

This gives us the follow<strong>in</strong>g output:<br />

rwxr-xr-x 500/100 0 Nov 17 08:48 1998 fruits/<br />

rw-r--r-- 500/100 0 Nov 17 08:48 1998 fruits/apple<br />

rw-r--r-- 500/100 0 Nov 17 08:48 1998 fruits/banana<br />

rw-r--r-- 500/100 0 Nov 17 08:48 1998 fruits/mango<br />

rw-r--r-- 500/100 0 Nov 17 08:48 1998 fruits/pear<br />

rw-r--r-- 500/100 0 Nov 17 08:48 1998 fruits/peach<br />

For this example, the output that you encounter depends on the version of tar that is <strong>in</strong>stalled on your<br />

mach<strong>in</strong>e. Some versions <strong>in</strong>clude more detail <strong>in</strong> the output than are shown here.<br />

Usage Statements<br />

Another common use for $0 is <strong>in</strong> the usage statement for a script, which is a short message<br />

<strong>in</strong>form<strong>in</strong>g the user how to <strong>in</strong>voke the script properly. All scripts used by more than one user should <strong>in</strong>clude<br />

such a message.<br />

In general, the usage statement is someth<strong>in</strong>g like the follow<strong>in</strong>g:<br />

echo "Usage: $0 [options][files]"<br />

Simplify<strong>in</strong>g Script Ma<strong>in</strong>tenance<br />

Us<strong>in</strong>g $0 as I've illustrated is encountered <strong>in</strong> the <strong>in</strong>stall and the un<strong>in</strong>stall scripts of some software<br />

packages.<br />

Because these scripts share many of the same rout<strong>in</strong>es and global variables, it is desirable, for ease of<br />

ma<strong>in</strong>tenance, to merge them <strong>in</strong>to a s<strong>in</strong>gle script hav<strong>in</strong>g different behavior depend<strong>in</strong>g on the name with<br />

which it is <strong>in</strong>voked.<br />

If you are writ<strong>in</strong>g scripts that need to share ma<strong>in</strong> rout<strong>in</strong>es, consider us<strong>in</strong>g such a scheme to simplify<br />

ma<strong>in</strong>tenance.<br />

If you consider the mytar script given previously, a usage statement would be a helpful addition, <strong>in</strong> case the<br />

script was called with some name other than the two names it knows about. To implement this, change the<br />

case statement as follows:<br />

case $0 <strong>in</strong><br />

*listtar) TARGS="-tvf $1" ;;<br />

*maketar) TARGS="-cvf $1.tar $1" ;;<br />

*) echo "Usage: $0 [file|directory]"<br />

exit 0<br />

;;

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

Saved successfully!

Ooh no, something went wrong!