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 />

2. UNIX SHELLS<br />

Back Quote Substitution<br />

One way of viewing <strong>the</strong> output from a command is as a big long string of characters. <strong>Unix</strong> shell provides a way to<br />

treat stdout from a command as a string which can be substituted into ano<strong>the</strong>r command.<br />

Example:<br />

The -l option of grep lists <strong>the</strong> names of files that contain a pattern.<br />

grep -l 'bug' *.c: outputs names of any *.c files containing <strong>the</strong> pattern 'bug'.<br />

Say <strong>the</strong> output is:<br />

file0.c<br />

file3.c<br />

Now using back-quotes '...' we can take this output from "grep" and treat it as a string substituted into <strong>the</strong><br />

arguments for <strong>the</strong> command "vi".<br />

vi 'grep -l 'bug' *.c': edits all *.c files containing <strong>the</strong> pattern 'bug'<br />

vi file0.c file3.c<br />

Example:<br />

The "tr" command can translate one or more characters into a different set of characters.<br />

PATH=/usr/local/bin:/usr/ucb:/bin:/usr/bin<br />

The following outputs <strong>the</strong> string $PATH <strong>with</strong> colons translated into spaces:<br />

echo $PATH | tr : ' ': outputs /usr/local/bin /usr/ucb /bin /usr/bin<br />

We can use "ls" to display all of <strong>the</strong> system commands in <strong>the</strong> $PATH directories:<br />

ls ' echo $PATH | tr : ' ' '<br />

Shell Here Documents<br />

When writing shell programs you sometimes want some constant data (e.g. test data) as standard input for a<br />

program. This can be done using "

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

Saved successfully!

Ooh no, something went wrong!