If you've been reading
comp.unix.shell
or any of the related groups
(comp.unix.questions
inter alia)
for any amount of time, this should be a familiar topic.
I made this web page on the topic primarily so I'd have a simpler URL
than one of those ghastly Deja News searches to hand to people. I've
tried to reconstruct Randal's standard form letter from looking at his
postings
(see end)
and added some comments of my own.
If you came here looking for material about abuse of feline animals, try
this Alta Vista search
instead.
The oldest article Deja News finds is from 1995, but it's actually a
followup to an earlier article. By Internet standards, this is thus
an Ancient Tradition.
Exercise: Try to find statistically significant differences between
the followups from 1995 and the ones being posted today.
(See below for a
reconstruction of the Award text.)
Briefly, here's the collected wisdom on using cat:
Of course, as Heiner points out, using cat on a single
file to view it from the command line is a valid use of cat
(but you might be better off if you get accustomed to using less
for this instead).
In a recent thread on comp.unix.shell, the following
example was posted by Andreas Schwab as another Useful Use of Cat
on a lone file:
(See below for a
reconstruction of the Award text.
It explains the issues clearly enough.)
This is really a special case of
Useless Use of Backticks
but it deserves its own section because it's something you see
fairly frequently.
The canonical form of this is something like
Depending a little bit on what exactly you have the variable for,
this can be reduced at least to
and there is often no real reason to even think of using echo in backticks
when the simpler construct will do.
(There is a twist: echo will "flatten" any whitespace in
$variable into a single space -- unless you double-quote
$variable, of course --, and sometimes you can legitimately
use echo in backticks for this side effect. But that's rarely necessary or
useful, and so most often, this is just a misguided use of echo.)
There is another example in the next section, and a longer rant about
Useless Use of Backticks
further down the page. There is also a parallel, slightly different
example on the
Backticks Example page
Very clever. Usually this is seen as part of a for loop:
Here's a related but slightly more benign error
(because echo is often built into the shell):
Finally, as Aaron Crane points out, the result of ls *
will usually be the wrong thing if you do it in a directory with
subdirectories; ls will list the contents of those
directories, not just their names.
If something is reasonably coded, it might even already
be setting its exit code to tell you whether it succeeded in doing
what you asked it to do; in that case, all you have to check is
the exit code:
I used to have a really wretched example of clueless code
(which I had written up completely on my own, to protect the innocent)
which I've moved to a
separate page
and annotated a little bit. It expands on the above and also has a
bit about useless use of backticks
(q.v.)
Here's a contribution I got from Aaron Crane (thanks!):
(Of course, this is merely an example. If you have lsof
it's probably a better solution to this particular problem; also
the output of ps varies wildly from system to system
so you might want to print something else than $2 and
use completely different options to ps.)
Remember that sed and awk are
glorified variants of grep.
So why use grep at all?
True Story from Real Life: an older version of the GNATS system would
think my real name was "System Operator" because it just went looking
for the first occurrence of the letters e-r-a in the
/etc/passwd file. (Well, actually, it thought my name was
"System Era". It took me a while to figure out how it arrived at this
somewhat whimsical conclusion. Incidentally, you also have to wonder
why the author thought my real name was worth knowing, and if this is
the right way to get that information. The end goal was to produce a
template for an e-mail message -- perhaps my MUA would already know
my real name, and even be able to produce nice e-mail headers for GNATS?)
The
cluelessness example (I've put it on a separate page)
contains a lot of badly chosen backticks and a somewhat longer
discussion of what exactly they can accomplish, and some ideas for
how to do it differently.
Obviously, backticks are a valid construction, and you can put them
to good use in many shell scripts. I have simply noticed that newbie
scripters often generously treat backticks as the hammer for all those
nails they see.
Incidentally, this is also one of the Very Ancient Recurring Threads
in
comp.unix.shell
so don't make the mistake of posting anything that resembles this.
In case something is still not obvious, the only disallowed file name
characters under Unix are the null character and slash. If a file is
called something like /tmp/moo/etc/passwd,
normal find /tmp -print would output
(I'll integrate it better with this page as soon as I have the time;
I've been keeping it in my inbox for an embarrassing amount of time
so I thought I'd better at least move it here where people can see it.)
Despite the looks of this embarrassing section, I do appreciate
comments and additional ideas for this page.
Send me mail
with your suggestions!
I have a
small collection of Unix links
with some more information, too.
)-: ylwols hguoht( depoh dah I naht retteb neve ,dekrow ti yltnerappA --
.egap "elpoeP looC" s'ladnaR
no dedulcni ti teg ot si egap siht fo esoprup elohw eht ,yllaer oN
Link preview
Yahoo Search - Web Search
The search engine that helps you find exactly what you're looking for. Find the most relevant information, video, images, and answers from all across the Web. search.yahoo.com · altavista.com
Comments