Re: What file display command does NOT default to STDIN if unspecified?
- From: Barry Margolin <barmar@xxxxxxxxxxxx>
- Date: Sun, 22 Apr 2007 14:25:58 -0400
In article <1177208996.484008.48220@xxxxxxxxxxxxxxxxxxxxxxxxxxxx>,
thecrow <hokkaidocrow@xxxxxxxxx> wrote:
On Apr 21, 12:46 pm, Ed Morton <mor...@xxxxxxxxxxxxxx> wrote:
thecrow wrote:
I'm looking for a command to display the contents of a file, which
takes the filename as an arg, and which fails if the arg isn't given
or the file doesn't exist.
The problem with cat, tail, head, grep, and so forth is that if you
supply no argument, it defaults to STDIN. This causes us trouble
from time to time in shell scripts because someone will use the idiom
myValue=`cat $VALUE_FILE` expecting that VALUE_FILE would have been
set in the environment. But if not, then of course the cat command
defaults to STDIN and sits there waiting for input.
That's because you aren't quoting your variable as you should:
$ myValue=`cat $VALUE_FILE`
<interrupt>
$ myValue=`cat "$VALUE_FILE"`
cat: : No such file or directory
Well, that's somewhat useful, but is it realistic? I consider that
defensive coding against a scenario that should be altogether
avoided. I could go edit all these scripts to add defensive quotes,
but really, who quotes everyting in a shell script? Somewhere down
the line, some ignorant maintainer could come behind me and delete
them, not being aware of their importance. If there were some
command other than cat, then that might signal more strongly the
intentions to future maintainers, more so than just using quotes.
(And I don't believe in comments unless there's no way at all to make
the code so obvious as to be self-documenting)
What expectation do you have that these sloppy coders will remember to
use this command, rather than the "cat" that they've been using for so
many years?
--
Barry Margolin, barmar@xxxxxxxxxxxx
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
.
- References:
- Prev by Date: Re: What file display command does NOT default to STDIN if unspecified?
- Next by Date: Re: rsync - unusual or corrupted warning - (.jVPNZY": No such file or directoryicon)
- Previous by thread: Re: What file display command does NOT default to STDIN if unspecified?
- Next by thread: Re: What file display command does NOT default to STDIN if unspecified?
- Index(es):
Relevant Pages
|