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.

$0 The name of the command be<strong>in</strong>g executed. For shell scripts, this is the path with which it was<br />

<strong>in</strong>voked.<br />

$n These variables correspond to the arguments with which a script was <strong>in</strong>voked. Here n is a<br />

positive decimal number correspond<strong>in</strong>g to the position of an argument (the first argument is $1,<br />

the second argument is $2, and so on).<br />

$# The number of arguments supplied to a script.<br />

$* All the arguments are double quoted. If a script receives two arguments, $* is equivalent to $1<br />

$2.<br />

$@ All the arguments are <strong>in</strong>dividually double quoted. If a script receives two arguments, $@ is<br />

equivalent to $1 $2.<br />

$? The exit status of the last command executed.<br />

$$ The process number of the current shell. For shell scripts, this is the process ID under which<br />

they are execut<strong>in</strong>g.<br />

$! The process number of the last background command.<br />

Us<strong>in</strong>g $0<br />

Start by look<strong>in</strong>g at $0. This variable is commonly used to determ<strong>in</strong>e the behavior of scripts that can be<br />

<strong>in</strong>voked with more than one name. Consider the follow<strong>in</strong>g script:<br />

#!/b<strong>in</strong>/sh<br />

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

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

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

esac<br />

tar $TARGS<br />

You can use this script to list the contents of a tar file (t as <strong>in</strong> tape and ar as <strong>in</strong> archive, a common format for<br />

distribut<strong>in</strong>g files <strong>in</strong> UNIX) or to create a tar file based on the name with which the script is <strong>in</strong>voked. The tar<br />

file to read or create is specified as the first argument, $1.<br />

I called this script mytar and made two symbolic l<strong>in</strong>ks to it called listtar and maketar as follows:<br />

$ ln -s mytar listtar<br />

$ ln -s mytar maketar<br />

If the script is <strong>in</strong>voked with the name maketar and is given a directory or filename, a tar file is created. If<br />

you had a directory called fruits with the follow<strong>in</strong>g contents<br />

$ ls fruits<br />

apple banana mango peach pear<br />

you can <strong>in</strong>voke the script as maketar to obta<strong>in</strong> a tar file called fruit.tar conta<strong>in</strong><strong>in</strong>g this directory, by<br />

issu<strong>in</strong>g the follow<strong>in</strong>g command:<br />

$ ./maketar fruits

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

Saved successfully!

Ooh no, something went wrong!