[comp.unix.shell] Question about 'test'

PPH93@DMSWWU1A.BITNET (Thomas Heller) (11/23/90)

In article <1990Nov21.191638.19469@athena.mit.edu>, jik@athena.mit.edu (Jonathan
I. Kamens) says:
>
>In sh, with either a test built-in named "[" or a link in your filesystem
                                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^
>called "[" pointing to the test program:
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
But what will the (bourne-) shell say about...
>
>        if [ -r filename ]; then
                          ^
  ... ?

Thomas Heller

ekrell@ulysses.att.com (Eduardo Krell) (11/23/90)

In article <90326.175421PPH93@DMSWWU1A.BITNET> PPH93@DMSWWU1A.BITNET (Thomas Heller) writes:

>But what will the (bourne-) shell say about...
>>
>>        if [ -r filename ]; then
>                          ^

Why should the shell do anything about it? The "]" is an argument to
"[" (which is a link to test). The shell just passes that token as an
argument when it exec's test.
    
Eduardo Krell                   AT&T Bell Laboratories, Murray Hill, NJ

UUCP: {att,decvax,ucbvax}!ulysses!ekrell  Internet: ekrell@ulysses.att.com

rob@b15.INGR.COM (Rob Lemley) (11/29/90)

In <14048@ulysses.att.com> ekrell@ulysses.att.com (Eduardo Krell) writes:

>Why should the shell do anything about it? The "]" is an argument to
>"[" (which is a link to test). The shell just passes that token as an
>argument when it exec's test.

Also, the test command looks at argv[0] to see if it was called "test"
or "[".  If "[" then test verifies that the closing "]" argument is there.
If the closing "]" is not there, test exits with a status of 1 (false).

Rob

guy@auspex.auspex.com (Guy Harris) (11/30/90)

>But what will the (bourne-) shell say about...
>>
>>        if [ -r filename ]; then

It'll say "OK, this 'if' is going to run the command named '[', passing
it '-r' as its first argument, 'filename' as its second argument, and
']' as its third argument."

"test"/"[" will then say "well, I was invoked as '[' rather than 'test';
that means I should check to make sure my last argument is ']', and
complain otherwise."

qpliu@phoenix.princeton.edu (q.p.liu) (11/30/90)

In article <4586@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes:
 >>But what will the (bourne-) shell say about...
 >>>
 >>>        If [ -r filename ]; then

 >"test"/"[" will then say "well, I was invoked as '[' rather than 'test';
 >that means I should check to make sure my last argument is ']', and
 >complain otherwise."

trying /bin/[ shows that it only checks if argv[0] == "[".

qpliu@phoenix.princeton.edu (q.p.liu) (12/01/90)

In article <1779@seti.inria.fr>
deschamp@minos.inria.fr (Philippe Deschamp) writes:
>In article <QPLIU.90Nov29205347@phoenix.princeton.edu>,
>qpliu@phoenix.princeton.edu (q.p.liu) writes:
>|> trying /bin/[ shows that it only checks if argv[0] == "[".
>   *Never* generalize :-). On my workstation (Sun 3/60 under SunOS 4.0.3):

(Ok, was on sunos 4.1, 4.0.3 (sun4) and ctss (cray xmp, cray 2) running sh)

(btw, the string comparision is as in awk, not as in C)

>What I do not understand is this:
>	$ /bin/[
>	$
>
>Why no complaint in the last case ?

In this case, argv[0] != "[", hence no check for ].

jik@athena.mit.edu (Jonathan I. Kamens) (12/03/90)

In article <QPLIU.90Nov29205347@phoenix.princeton.edu>, qpliu@phoenix.princeton.edu (q.p.liu) writes:
|> In article <4586@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes:
|>  >"test"/"[" will then say "well, I was invoked as '[' rather than 'test';
|>  >that means I should check to make sure my last argument is ']', and
|>  >complain otherwise."
|> trying /bin/[ shows that it only checks if argv[0] == "[".

  Then either you did something wrong, or you're using a buggy version of
"test."

	$ [ frep
	test: ] missing
	$ echo $?
	255
	$ [ frep ]
	$ echo $?
	0

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

jik@athena.mit.edu (Jonathan I. Kamens) (12/03/90)

In article <1990Dec2.210141.13170@athena.mit.edu>, I write:
|> In article <QPLIU.90Nov29205347@phoenix.princeton.edu>, qpliu@phoenix.princeton.edu (q.p.liu) writes:
|> |> trying /bin/[ shows that it only checks if argv[0] == "[".
|> 
|>   Then either you did something wrong, or you're using a buggy version of
|> "test."

  I corrected q.p.liu because I misunderstood what he was saying, and just
realized this, so I'd like to apologize.  He was saying, "`Test' only checks
if the final `]' is present if it is invoked with argv[0] exactly equal to
`['."  I thought he was saying, "`Test' only checks if its argv[0] is exactly
equal to `['; it does not also check if the final ']' is present."

  Liu is right, of course.  Whether or not this behavior of "test" (i.e. the
distinguishing between "[" and "/bin/[") is correct is debatable, but I
believe that it is.

-- 
Jonathan Kamens			              USnail:
MIT Project Athena				11 Ashford Terrace
jik@Athena.MIT.EDU				Allston, MA  02134
Office: 617-253-8085			      Home: 617-782-0710

martin@mwtech.UUCP (Martin Weitzel) (12/03/90)

In article <1779@seti.inria.fr> deschamp@seti.inria.fr writes:
:
:In article <QPLIU.90Nov29205347@phoenix.princeton.edu>,
:qpliu@phoenix.princeton.edu (q.p.liu) writes:
:
:|> trying /bin/[ shows that it only checks if argv[0] == "[".
:
:   *Never* generalize :-). On my workstation (Sun 3/60 under SunOS 4.0.3):
:	$ test
:	$ [
:	test: ] missing
:
:What I do not understand is this:
:	$ '['
:	test: ] missing
:	$ PATH=/bin '['
:	test: ] missing
:	$ /bin/[
:	$
:
:Why no complaint in the last case ?

This shows exactly what original poster tried to say (as I understood).
To make it a bit clearer, the source-code of "test" must look like

	if (!strcmp(argv[0], "[")) {
		if (strcmp(argv[--argc], "]") {
			/* complain about missing "]" and exit */
			.....
		}
	}
	/* parse arguments; run tests as required */
	.....

In your last experiment argv[0] is "/bin/[" and not simply "[", hence no
comlaint.
-- 
Martin Weitzel, email: martin@mwtech.UUCP, voice: 49-(0)6151-6 56 83

dik@cwi.nl (Dik T. Winter) (12/04/90)

In article <987@mwtech.UUCP> martin@mwtech.UUCP (Martin Weitzel) writes:
 > In article <1779@seti.inria.fr> deschamp@seti.inria.fr writes:
 > :What I do not understand is this:
 > :	$ '['
 > :	test: ] missing
 > :	$ PATH=/bin '['
 > :	test: ] missing
 > :	$ /bin/[
 > :	$
 > 
 > This shows exactly what original poster tried to say (as I understood).
 > To make it a bit clearer, the source-code of "test" must look like
 > 	if (!strcmp(argv[0], "[")) {
 > 		if (strcmp(argv[--argc], "]") {
 > 			/* complain about missing "]" and exit */
 > 			.....
 > 		}
 > 	}
 > 	/* parse arguments; run tests as required */
 > 	.....
 > 
 > In your last experiment argv[0] is "/bin/[" and not simply "[", hence no
 > comlaint.

Exactly, just try:
	[ -f foo ]
and
	/bin/[ -f foo ]
--
dik t. winter, cwi, amsterdam, nederland
dik@cwi.nl