Useless use of cat awards
2010-06-25
4623 words
22 mins read
<a href="http://partmaps.org/era/unix/award.html#cat">Here you will find some useless use of cat command in linux. Really interesting and quite informative.
Useless Use of Cat Award
If you’ve been reading <a href="news:comp.unix.shell">comp.unix.shell or any of the related groups <a href="news:comp.unix.questions">(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 <a href="http://partmaps.org/era/unix/award.html#forms">(see end) and added some comments of my own.
If you came here looking for material about abuse of feline animals, try <a href="http://www.altavista.com/cgi-bin/query?pg=aq&text=yes&q=title%3a%22kitty+porn%22+and+not+%28xxx+or+title%3a%22persian+kitty%22+or+title%3acorporation%29">this Alta Vista search instead.
Contents:
-
Useless Use of <a href="http://partmaps.org/era/unix/award.html#cat">
1
<td> <div class="text codecolorer"> cat </div> </td> </tr>
-
Useless Use of <a href="http://partmaps.org/era/unix/award.html#kill">
1
<td> <div class="text codecolorer"> kill -9 </div> </td> </tr>
-
Useless Use of <a href="http://partmaps.org/era/unix/award.html#echo">
1
<td> <div class="text codecolorer"> echo </div> </td> </tr>
-
Useless Use of <a href="http://partmaps.org/era/unix/award.html#ls">
1
<td> <div class="text codecolorer"> ls * </div> </td> </tr>
-
Useless Use of <a href="http://partmaps.org/era/unix/award.html#wc">
1
<td> <div class="text codecolorer"> wc -l </div> </td> </tr>
-
Useless Use of <a href="http://partmaps.org/era/unix/award.html#grep">
1
<td> <div class="text codecolorer"> grep | awk </div> </td> </tr>
-
Useless Use of <a href="http://partmaps.org/era/unix/award.html#backticks">Backticks
-
Useless Use of <a href="http://partmaps.org/era/unix/award.html#test">
1
<td> <div class="text codecolorer"> test </div> </td> </tr>
- <a href="http://partmaps.org/era/unix/award.html#gripes">Assorted Other Gripes
- <a href="http://partmaps.org/era/unix/award.html#readme">Related Resources
- <a href="http://partmaps.org/era/unix/award.html#forms">Reconstructions of Randal’s Form Letters
<a name="cat"> The Useless Use of Cat Award
The venerable <a href="http://www.stonehenge.com/%7Emerlyn/">Randal L. Schwartz hands out Useless Use of Cat Awards from time to time; you can see some recent examples in <a href="http://search.dejanews.com/dnquery.xp?QRY=&ST=PS&DBS=1&defaultOp=AND&maxhits=25&format=terse&showsort=date&groups=&authors=&subjects=useless+%26+use+%26+cat+%26+award&fromdate=Jan+1+1970&todate=">Deja News. (The subject line really says ”This Week’s Useless Use of Cat Award” although the postings are a lot less frequent than that nowadays). The actual award text is basically the same each time, and the ensuing discussion is usually just as uninteresting, but there are some refreshing threads there among all the flogging of this dead horse.
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 <a href="http://partmaps.org/era/unix/award.html#uucaletter">reconstruction of the Award text.)
Briefly, here’s the collected wisdom on using cat:
The purpose of cat is to concatenate (or ”catenate”) files. If it’s only one file, concatenating it with nothing at all is a waste of time, and costs you a process.
The fact that the same thread (”but but but, I think it’s cleaner / nicer / not that much of a waste / my privelege to waste processes!”) springs up virtually every time the Award is posted is also Ancient Usenet Tradition.
Of course, as Heiner points out, using
1
|
on a single file to view it from the command line is a valid use of
1
|
(but you might be better off if you get accustomed to using
1
|
for this instead).
In a recent thread on
1
|
, the following example was posted by Andreas Schwab as another Useful Use of Cat on a lone file:
{ foo; bar; cat mumble; baz } | whatever
Here, the contents of the file
1
|
are output to stdout after the output from the programs
1
|
and
1
|
, and before the output of
1
|
. All the generated output is piped to the program
1
|
. (Read up on shell programming constructs if this was news to you
1
|
Other Fun Awards
This could evolve into a good listing of ”don’t do that” shell programming idioms.
<a name="kill"> Useless Use of Kill -9
Randal also posts his <a href="http://search.dejanews.com/dnquery.xp?QRY=&ST=PS&DBS=1&defaultOp=AND&maxhits=25&format=terse&showsort=score&groups=&authors=&subjects=useless+%26+use+%26+kill&fromdate=Jan+1+1970&todate=">Useless Use of Kill -9 Award although much less frequently.
(See below for a <a href="http://partmaps.org/era/unix/award.html#uuk9letter">reconstruction of the Award text. It explains the issues clearly enough.)
<a name="echo"> Useless Use of
1
|
This is really a special case of <a href="http://partmaps.org/era/unix/award.html#backticks">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
variable=\"something here, or perhaps even the result of backticks\"
some command -options echo $variable
<div id="linkalert-box" style="position: absolute; left: 0px; top: 0px; z-index: 1000000000 ! important; visibility: hidden; opacity: 1;"><img id="linkalert-icon-1" src="chrome://linkalert/skin/none-icon.png" alt="" />
Depending a little bit on what exactly you have the variable for, this can be reduced at least to
> <pre>variable=\"something here, or perhaps even the result of backticks\"
some command -options $variable
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:
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
echo
</div>
</td>
</tr>
</table>
</div>
will \”flatten\” any whitespace in
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
$variable
</div>
</td>
</tr>
</table>
</div>
into a single space — unless you double-quote
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
$variable
</div>
</td>
</tr>
</table>
</div>
, 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 <a href=\"http://partmaps.org/era/unix/award.html#backticks\">Useless Use of Backticks</a> further down the page. There is also a parallel, slightly different example on the <a href=\"http://partmaps.org/era/unix/award-example-backticks.html#echo-backticks\">Backticks Example page</a>
### <a name=\"ls\"> Useless Use of</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
ls *
</div>
</td>
</tr>
</table>
</div>
</a></h3>
Very clever. Usually this is seen as part of a
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
for
</div>
</td>
</tr>
</table>
</div>
loop:
> <pre>for f in `ls *`; do
command \"$f\" # newbies will often forget the quotes, too
done
Of course, the
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
ls
</div>
</td>
</tr>
</table>
</div>
is not very useful. It will just waste an extra process doing absolutely nothing. The
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
*
</div>
</td>
</tr>
</table>
</div>
glob will be expanded by the shell before
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
ls
</div>
</td>
</tr>
</table>
</div>
even gets to see the file names (never mind that
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
ls
</div>
</td>
</tr>
</table>
</div>
lists all files by default anyway, so naming the files you want listed is redundant here).
Here\’s a related but slightly more benign error (because
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
echo
</div>
</td>
</tr>
</table>
</div>
is often built into the shell):
> <pre>for f in `echo *`; do
command \"$f\"
done
But of course the backticks are still useless, the glob itself already does the expansion of the file names. (See <a href=\"http://partmaps.org/era/unix/award.html#echo\">Useless Use of
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
echo
</div>
</td>
</tr>
</table>
</div>
</a> above.) What was meant here was obviously
> <pre>for f in *; do
command \"$f\"
done
Additionally, oftentimes the command in the loop doesn\’t even need to be run in a for loop, so you might be able to simplify further and say
> <pre>command *</pre>
A different issue is how to cope with a glob which expands into file names with spaces in them, but the for loop or the backticks won\’t help with that (and will even make things harder). The plain glob generates these file names just fine; <a href=\"http://partmaps.org/era/unix/award-example-ls.html\">click here for an example.</a> See also <a href=\"http://partmaps.org/era/unix/award.html#backticks\">Useless Use of Backticks</a>
Finally, as Aaron Crane points out, the result of
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
ls *
</div>
</td>
</tr>
</table>
</div>
will usually be the wrong thing if you do it in a directory with subdirectories;
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
ls
</div>
</td>
</tr>
</table>
</div>
will list the contents of those directories, not just their names.
### <a name=\"wc\"> Useless Use of</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
wc -l
</div>
</td>
</tr>
</table>
</div>
</a></h3>
This is my personal favorite. There is actually a whole class of \”Useless Use of (something) | grep (something) | (something)\” problems but this one usually manifests itself in scripts riddled by useless backticks and pretzel logic.
Anything that looks like
> <pre><var>something</var> | grep \'..*\' | wc -l</pre>
can usually be rewritten like something along the lines of
> <pre><var>something</var> | grep -c . # Notice that . is better than \'..*\'</pre>
or even (if all we want to do is check whether <var>something</var> produced any non-empty output lines)
> <pre><var>something</var> | grep . >/dev/null && ...</pre>
(or
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
grep -q
</div>
</td>
</tr>
</table>
</div>
if your grep has that).
If <var>something</var> 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:
> <pre><var>something</var> && ...</pre>
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 <a href=\"http://partmaps.org/era/unix/award-example-backticks.html\">separate page</a> and annotated a little bit. It expands on the above and also has a bit about useless use of backticks <a href=\"http://partmaps.org/era/unix/award.html#backticks\">(q.v.)</a>
Here\’s a contribution I got from Aaron Crane (thanks!):
> <div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
> <table cellspacing="0" cellpadding="0">
> <tr>
> <td class="line-numbers">
> <div>
> 1<br />
> </div>
> </td>
>
> <td>
> <div class="text codecolorer">
> grep -c
> </div>
> </td>
> </tr>
> </table>
> </div>
>
> can actually solve a large class of problems that
>
> <div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
> <table cellspacing="0" cellpadding="0">
> <tr>
> <td class="line-numbers">
> <div>
> 1<br />
> </div>
> </td>
>
> <td>
> <div class="text codecolorer">
> grep | wc -l
> </div>
> </td>
> </tr>
> </table>
> </div>
>
> can\’t. If what interests you is the count for each of a group of files, then the only way to do it with
>
> <div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
> <table cellspacing="0" cellpadding="0">
> <tr>
> <td class="line-numbers">
> <div>
> 1<br />
> </div>
> </td>
>
> <td>
> <div class="text codecolorer">
> grep | wc -l
> </div>
> </td>
> </tr>
> </table>
> </div>
>
> is to put a loop round it. So where I had this:
>
> > <pre>grep -c \"^~h\" [A-Z]*/hmm[39]/newMacros</pre>
>
> the naive solution using
>
> <div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
> <table cellspacing="0" cellpadding="0">
> <tr>
> <td class="line-numbers">
> <div>
> 1<br />
> </div>
> </td>
>
> <td>
> <div class="text codecolorer">
> wc -l
> </div>
> </td>
> </tr>
> </table>
> </div>
>
> would have been
>
> > <pre>for f in [A-Z]*/hmm[39]/newMacros; do
# or worse, for f in `ls [A-Z]*/hmm[39]/newMacros` ...
echo -n \"$f:\"
# so that we know which file\'s results we\'re looking at
grep \"^~h\" \"$f\" | wc -l
# gag me with a spoon
done > > and notice that we also had to fiddle to get the output in a convenient form.
### <a name=\"grep\"> Useless Use of grep | awk and grep | sed </a>
Here\’s another one:
> <pre>ps -l | grep -v \'[g]rep\' | awk \'{print $2}\'</pre>
<small>(Of course, this is merely an example. If you have</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
lsof
</div>
</td>
</tr>
</table>
</div>
<p>
it\’s probably a better solution to this particular problem; also the output of
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
ps
</div>
</td>
</tr>
</table>
</div>
<p>
varies wildly from system to system so you might want to print something else than
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
$2
</div>
</td>
</tr>
</table>
</div>
<p>
and use completely different options to
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
ps
</div>
</td>
</tr>
</table>
</div>
<p>
.) </small>
</p>
<p>
Remember that
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
sed
</div>
</td>
</tr>
</table>
</div>
<p>
and
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
awk
</div>
</td>
</tr>
</table>
</div>
<p>
are glorified variants of
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
grep
</div>
</td>
</tr>
</table>
</div>
<p>
. So why use grep at all?
</p>
<blockquote>
<pre>ps -l | awk \'!/[a]wk/{print $2}\'</pre>
</blockquote>
<p>
Usually you\’d like the regex to be tighter than this, especially if your login might happen to include the letters
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
grep
</div>
</td>
</tr>
</table>
</div>
<p>
or
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
awk
</div>
</td>
</tr>
</table>
</div>
<p>
…
</p>
<p>
<small></p>
<blockquote>
<p>
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
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
/etc/passwd
</div>
</td>
</tr>
</table>
</div>
<p>
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?)
</p>
</blockquote>
<p>
</small>
</p>
<h3>
<a name=\"backticks\"> Useless Use of Backticks </a>
</h3>
<p>
This is a sort of meta-award, or a corollary of several of the others. It merely consists of the general observation that shell scripts with lots of backticks in them are often possible to optimize and clean up a lot.
</p>
<p>
The <a href=\"http://partmaps.org/era/unix/award-example-backticks.html\">cluelessness example (I\’ve put it on a separate page)</a> 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.
</p>
<p>
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.
</p>
<h4>
<a name=\"arg-max\"> Dangerous Backticks </a>
</h4>
<p>
A special idiom to pay attention to, because it\’s basically always wrong, is this:
</p>
<blockquote>
<pre> for f in `cat file`; do
...
done
<p>
Apart from the classical <a href=\"http://partmaps.org/era/unix/award.html#cat\">Useless Use of Cat</a> here, the backticks are outright dangerous, unless you <strong>know</strong> the result of the backticks is going to be less than or equal to how long a command line your shell can accept. (Actually, this is a kernel limitation. <a href=\"http://partmaps.org/era/unix/arg-max.html\">The constant <tt>ARG_MAX</tt> in your <tt>limits.h</tt></a> should tell you how much your own system can take. POSIX requires <tt>ARG_MAX</tt> to be at least 4,096 bytes.)
</p>
<p>
Incidentally, this is also one of the Very Ancient Recurring Threads in <a href=\"news:comp.unix.shell\"><tt>comp.unix.shell</tt></a> so don\’t make the mistake of posting anything that resembles this.
</p>
<p>
The right way to do this is
</p>
<blockquote>
<pre> while read f; do
...
done <file
<p>
or, in cases where the command in backticks was something more complicated than a (Useless Use of) <tt>cat</tt> of a single file,
</p>
<blockquote>
<pre> command that used to be in backticks |
while read f; do … done
<p>
The <tt>ARG_MAX</tt> warning applies to other constructs than <tt>for</tt> loops too, of course. Generally, commands that look like
</p>
<blockquote>
<pre> command `other command`</pre>
</blockquote>
<p>
are usually safer with <tt>xargs</tt>
</p>
<p>
:
</p>
<blockquote>
<pre> other command | xargs command</pre>
</blockquote>
<p>
The classic example is running something on each one of a number of files listed by <tt>find</tt>. An additional problem here is that <tt>find</tt> might find files whose names contain line breaks. GNU <tt>find</tt> can even cope with that, by using the null character as the file name separator.
</p>
<blockquote>
<pre> # Note that we use -print0 instead of -print
This is a GNU find -specific option
Similarly, the -0 option to xargs is a GNU feature
find -options -more -options -print0 | xargs -r0 command
<p>
With normal
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
find
</div>
</td>
</tr>
</table>
</div>
<p>
, a user could create a file with a newline in its name, tricking you into, well, at least missing that file, and potentially something a lot more dangerous. (Imagine that you have a nightly
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
cron
</div>
</td>
</tr>
</table>
</div>
<p>
job which runs as root and which removes old files from
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
/tmp
</div>
</td>
</tr>
</table>
</div>
<p>
. Now imagine that a malicious user creates a directory in
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
/tmp
</div>
</td>
</tr>
</table>
</div>
<p>
whose name ends in newline, and in that directory creates
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
etc/
</div>
</td>
</tr>
</table>
</div>
<p>
and
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
etc/passwd
</div>
</td>
</tr>
</table>
</div>
<p>
. Then wait for it to grow old, or modify the inode\’s date stamp with
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
touch
</div>
</td>
</tr>
</table>
</div>
<p>
…)
</p>
<p>
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
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
/tmp/moo<newline>/etc/passwd
</div>
</td>
</tr>
</table>
</div>
<p>
, normal
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
find /tmp -print
</div>
</td>
</tr>
</table>
</div>
<p>
would output
</p>
<blockquote>
<pre>/tmp/moo
/etc/passwd
<p>
and
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
xargs
</div>
</td>
</tr>
</table>
</div>
<p>
would see two file names here. Changing the record separator to ASCII 0 means it\’s now valid for a file name to span multiple lines, so this becomes a non-issue.
</p>
<h3>
<a name=\"test\"> Useless Use of Test </a>
</h3>
<p>
This was suggested by Chris Thompson in <a href=\"news:comp.unix.shell\"><tt>comp.unix.shell</tt>:</a>
</p>
<blockquote>
<p>
All these UUOC postings remind me of one of my least favourite constructs in (Bourne, FTSOE) shell scripts:
</p>
<blockquote>
<pre> mangle the world
if [ $? -ne 0 ] ; then
echo \"Oh dear, mangling the world failed horribly\" \\
>&2 # shortened, stderr redirection by your humble HTMLizer
putback the world
exit 255
fi</pre>
</blockquote>
<p>
when
</p>
<blockquote>
<pre> if mangle the world ; then : ; else
echo \"Oh dear, mangling the world failed horribly\" \\
>&2 # I\'m still here
putback the world
exit 255
fi</pre>
</blockquote>
<p>
seems to me much cleaner (even when the \”then\” clause can\’t be used in a more economical way, or, as with some shells, omitted).
</p>
</blockquote>
<p>
(Something like this is also covered in the <a href=\"http://partmaps.org/era/unix/award.html#wc\"><tt>wc -l</tt> section above.)</a>
</p>
<h3>
<a name=\"gripes\"> Assorted Other Gripes </a>
</h3>
<p>
Jon LaBadie sent me the following list (thanks!):
</p>
<p>
<small> (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.) </small>
</p>
<blockquote>
<p>
Some things that bug me:
</p>
<ul>
<li>
Regular expressions used for searching (not substituting) that begin or end with \’.*\’. Actually \’anything*\’ or \’anything?\’. If you are willing to accept \”zero repetitions\” of the anything, why specify it?
</li>
<li>
Awk scripts that are basically cut unless reordering of fields is needed.
</li>
<li>
Case conversions in comp.unix.shell (ex. how do I change my file names from UC to LC?) using tr/sed/awk/??? when some shells have builtin case conversions.
</li>
<li>
Complex schemes to basically eliminate certain chars. For example DOS lines to UNIX lines. Sure read dos2unix(1), but using sed/awk/… when \”tr -d \’^M\’\” is all that is needed.
</li>
<li>
Global changes to a file using sed to create a tmp file and renaming the tmp file, when an \”ed(1)\” here document would do fine.
</li>
</ul>
</blockquote>
<p>
Frederick J. Sena comments;
</p>
<blockquote>
<p>
I <em>really</em> hate useless \”kill -9\”\’s and \”rm -fr\”\’s! After that, the next most annoying thing is people who use a useless \”chmod 777\” to make a file writable.
</p>
</blockquote>
<p>
That\’s a good observation and another example of the same pattern; having only the heavy-duty sledgehammer in your toolbox, and breaking all the smaller nails with it.
</p>
<p>
Frederick also remarks:
</p>
<blockquote>
<p>
I disagree with your awk/cut comment, as I often use awk for everything and cut for nothing because the syntax for awk is so much cleaner for one liners and I don\’t have to RTFM so much.
</p>
</blockquote>
<p>
I\’ll counter that awk is overkill, and you don\’t need to reread the cut manual after you\’ve read it once or twice; that\’s my experience. Also cut much more clearly conveys to the reader what is going on — a small awk script certainly should not take a lot of time to decode, but if you do it <em>too</em> quickly, there might be subtle points which are easy to miss. By contrast, cut doesn\’t have those subtleties, for better or for worse.
</p>
<p>
Despite the looks of this embarrassing section, I do appreciate comments and additional ideas for this page. <a href=\"http://www.iki.fi/era/feedback.html\">Send me mail</a> with your suggestions!
</p>
<h2>
<a name=\"readme\"> Related Resources </a>
</h2>
<p>
Or, Other Diatribes.
</p>
<ul>
<li>
<a href=\"http://language.perl.com/versus/csh.whynot\">Csh Programming Considered Harmful</a> by Tom Christiansen
</li>
<li>
<a href=\"http://partmaps.org/era/unix/guis.txt\">GUIs Considered Harmful</a> (ditto)
</li>
</ul>
<p>
Additionally, you should probably be aware of the following FAQs:
</p>
<ul>
<li>
<a href=\"http://www.faqs.org/faqs/unix-faq/shell/intro/\">Welcome to <div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
comp.unix.shell
</div>
</td>
</tr>
</table>
</div>
<p>
</a></li>
<li>
<a href=\"http://www.faqs.org/faqs/unix-faq/shell/shell-differences/\">Unix shell differences and how to change your shell</a>
</li>
<li>
<a href=\"http://www.faqs.org/faqs/unix-faq/faq/\">Unix Frequently Asked Questions</a>
</li></ul>
<p>
All of these are available in a nice browsable format from <a href=\"http://www.faqs.org/faqs/by-newsgroup/comp/comp.unix.shell.html\">the
</p>
<div class="codecolorer-container text solarized-light" style="overflow:auto;white-space:nowrap;width:550px;">
<table cellspacing="0" cellpadding="0">
<tr>
<td class="line-numbers">
<div>
1<br />
</div>
</td>
<td>
<div class="text codecolorer">
faqs.org
</div>
</td>
</tr>
</table>
</div>
<p>
archive</a> (which is where the above links will take you).
</p>
<p>
I have a <a href=\"http://partmaps.org/era/unix/index.html\">small collection of Unix links</a> with some more information, too. <a name=\"forms\">
</p>
<hr />
<p>
</a>
</p>
<h3>
<a name=\"uucaletter\"> Useless Use of Cat Award form letter </a>
</h3>
<p>
(Quote abomination)
</p>
<blockquote>
<pre>And of course, if you\'ve been following along for a week or two, you know
that this (BING!) is a Useless Use of Cat!
Rememeber, nearly all cases where you have:
cat file | some_command and its args ...
you can rewrite it as:
<file some_command and its args ...
and in some cases, such as this one, you can move the filename to the arglist as in:
some_command and its args ... file
Just another Useless Use of Usenet,
<p>
(.signature)
</p>
<hr />
<h3>
<a name=\"uuk9letter\"> Useless Use of Kill -9 form letter </a>
</h3>
<p>
(Quote abomination)
</p>
<blockquote>
<pre>No no no. Don\'t use kill -9.
It doesn't give the process a chance to cleanly:
-
shut down socket connections
-
clean up temp files
-
inform its children that it is going away
-
reset its terminal characteristics
and so on and so on and so on.
Generally, send 15, and wait a second or two, and if that doesn't work, send 2, and if that doesn't work, send 1. If that doesn't, REMOVE THE BINARY because the program is badly behaved!
Don't use kill -9. Don't bring out the combine harvester just to tidy up the flower pot.
Just another Useless Use of Usenet,
<p>
(.signature)
</p>
<hr />
<p>
$Id: award.prep,v 1.47 2000/12/16 09:24:38 era Exp $
</p>
<p>
<small> )-: ylwols hguoht( depoh dah I naht retteb neve ,dekrow ti yltnerappA — <a href=\"http://www.stonehenge.com/merlyn/refindex.html\">.egap \”elpoeP looC\” s\’ladnaR</a> no dedulcni ti teg ot si egap siht fo esoprup elohw eht ,yllaer oN </small>
</p>
<p>
era eriksson * <a href=\"http://www.iki.fi/era/\">home page </a>
</p>
Related Articles:
- 2010/04/30 AndreaMosiac.. The way to create a mosaic.
- 2010/06/25 MySQL tutorial and reference manual
- 2010/06/25 Getting the list of IOCTLS in the kernel.
- 2010/06/25 5-reasons-i-like-linux-and-5-why-i-dislike-it
- 2010/06/25 Linux Advertisement – So linux finally decides to do some marketing.