Re: What file display command does NOT default to STDIN if unspecified?



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)

.



Relevant Pages