Dumb "sh" question about "$@"

Arthur David Olson ado at elsie.UUCP
Sat Nov 17 22:12:07 AEST 1984


Apologies in advance for the dumbness of this question.
Please note that the task described below is not the one that caused my
interest in this problem in the first place; I've invented the task below
only for the purpose of explaining the problem.

Suppose I want to create a "sh" script that prints out the date and then
acts like "cat".  Let's call it "dcat" for sake of concreteness.  An initial
guess as to what it might contain would be:

	date
	cat $*

All well and good. . .except if I want to cat a file with a question mark in
its name.  If I had a directory with files named "a", "?", and "c" in it, then
while the command
	cat "?"
would list the contents of just the file named "?", the command
	dcat "?"
would list the contents of all three files.

Well, the "sh" manual page notes that
	"$@" is equivalent to "$1" "$2" ...
so a second guess as to what the "dcat" script might contain would be:

	date
	cat "$@"

Now the "?" case gets handled correctly. . .but whereas the command
	cat < /etc/passwd
dumps that old friend of a file to the standard output, the command
	dcat < /etc/passwd
dumps the date to the standard output and follows it up with the contents of
the current working directory.  This happens because if there are no arguments
to a shell script, "$@" expands to a single null argument rather than to no
arguments.

So it seems as if what I get to do is:

	date
	case $# in)
		0) 	cat ;;
		*)	cat "$@" ;;
	esac

which I'm hard-pressed to believe.

Can you tell me what I'm missing?
Please reply by mail rather than posting a followup.
Thanks for your help.
--
UNIX is an AT&T Bell Laboratories trademark.
--
	..decvax!seismo!elsie!ado			(301) 496-5688
	DEC, VAX and Elsie are Digital Equipment and Borden trademarks



More information about the Comp.unix mailing list