16.05.2015 Views

Working with the Unix OS

Working with the Unix OS

Working with the Unix OS

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Unix</strong> Shells<br />

Shell Program Variables<br />

Several special variable names are available <strong>with</strong>in shell programs.<br />

$0 Program name<br />

$1 $2 ...$9 Program arguments<br />

$# Number of arguments<br />

$* Same as "$1 $2 ...$9"<br />

$@<br />

Like $* except "$@" will put double quotes around each argument.<br />

$@" --> "$1" "$2"..."$n"<br />

$? Exit status of <strong>the</strong> last command.<br />

$$ Shell process number. Uniquely identifies <strong>the</strong> process. Often used for temporary<br />

file names which reside in <strong>the</strong> /tmp directory which is shared by everyone.<br />

e.g. /tmp/lsout$$<br />

Inputs and Outputs to/from a Process<br />

Inputs<br />

Files<br />

Stdin<br />

Program name<br />

Arguments<br />

Exported environment<br />

variables<br />

Outputs<br />

Files<br />

Stdout/Stderr<br />

Exit status<br />

<strong>Unix</strong> Shell Examples<br />

! ZLESS<br />

$ vi zless<br />

-------Enter using vi editor-------<br />

#!/bin/sh<br />

# @(#)zless - browse compressed files<br />

zcat $* | less<br />

-----------------------------------<br />

$ chmod ug+x zless # make shell script executable<br />

$ zless ass1.c.Z<br />

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />

SOLARIS: zless is not provided<br />

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />

! MKSHAR<br />

#!/bin/sh<br />

# @(#)mkshar - creates shell archives from list of files<br />

# shell archives use <strong>the</strong> "here document" facility to<br />

# store <strong>the</strong> contents of one or more files.<br />

# shell archives can be executed by shell to create<br />

# <strong>the</strong> original archived files. They are typically<br />

# used to send files by electronic mail or news.<br />

for file<br />

do<br />

echo "cat > $file

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

Saved successfully!

Ooh no, something went wrong!