Live data from Hacker News

Test your square brackets

fluca1978.github.io

41–50 of 68 posts

Re: Test your square brackets

#42
Whoa!

/bin/[ as a binary looking for it's own "]" closing bracket!

What a nasty syntax hack!

I wonder what the motivation was for doing this rather than just implementing test expression support directly in the shell?

I just tried and bash also accepts some other odd filenames in place of "[", so you can do:

ln -s /bin/test "-"

ln -s /bin/test "$"

And use either of these in place of "[" (assuming they are on your path). Of course they still expect the closing "]" since that requirement comes from /bin/test.

It's an interesting way to extend bash in a confusing way! You could write a "$" utility that did something else entirely, perhaps looking for a "closing $" too, then write something like:

if $ args $;

then

fi

Re: Test your square brackets

#43
post #40
post #21

Reminds me of my rather memorable introduction to special characters invoking functions, seeing this dastardly little quip in the email signature of someone in a mailing list (circa '95 or so). :(){:|:&};: My curiosity piqued, I pasted it into the shell on my terminal in a pure example of FAFO. The poor little Sparc 5 I was using ground to a halt over the course of about ten seconds. The reboot was as hard as the les…

> The reboot was as hard as the lesson. I'm confused. What happened on reboot?

A hard reboot is where the power goes all the way off, a soft reboot is where it doesn't. A fork bomb makes it very hard / impossible to trigger a soft reboot, forcing you to do a hard reboot.

As an extra sting, a hard reboot can be damaging if the software and hardware is not correctly handling power interruption, which was much more likely in the 90's.

Re: Test your square brackets

#44
post #29

Earlier quoted context omitted.

yeah, if [ is a command in /bin/ what is [[ ?

A builtin part of the shell, just like `if` and `then` and `fi`. Not all shells have `[[`, the one that doesn't which people unknowingly run into most is probably `dash` as used by Alpine Linux. POSIX doesn't require `[[` in a shell, BASH & Zsh support it but others often don't.

right so "[" is a file in /bin, "[[" is implicitly part of the shell, so that's as bad as I thought

Re: Test your square brackets

#45
post #21

Reminds me of my rather memorable introduction to special characters invoking functions, seeing this dastardly little quip in the email signature of someone in a mailing list (circa '95 or so). :(){:|:&};: My curiosity piqued, I pasted it into the shell on my terminal in a pure example of FAFO. The poor little Sparc 5 I was using ground to a halt over the course of about ten seconds. The reboot was as hard as the les…

I prefer

    u(){u|u&};u
AKA, fork u.

Re: Test your square brackets

#46
Here is the part of the test.c source from V7 Unix:

   main(argc, argv)
   char *argv[];
   {
   
    ac = argc; av = argv; ap = 1;
    if(EQ(argv[0],"[")) {
     if(!EQ(argv[--ac],"]"))
      synbad("] missing","");
    }
    argv[ac] = 0;
    if (ac
So, if the professor was missing the "[" command, they were missing a link. More likely, they had a weird orthodoxy about using "test" instead of "[" because reasons. One good reason to use "test" instead of "[" in a Bourne shell control statement is if you are running a list of commands between "if" and "then", and the test is the last part of the list.

Another good place to use "test" is if you are not in a control statement and you are just setting $?.

Edit: EQ() is defined as:

   #define EQ(a,b) ((tmp=a)==0?0:(strcmp(tmp,b)==0))
   char *tmp;

Re: Test your square brackets

#47
The [[…]] built-in test version was introduced in Peter Korn's korn shell, ksh88 IIRC. As where various other modernized notations like array variables, process substitution or

    $(command)
as a much more readable version of the backquotes version.

https://docs.oracle.com/cd/E36784_01/html/E36870/ksh88-1.htm...

While learning the Bourne shell as acstudent, I was rapidly lured by csh and then tcsh, but Tom Christiansen's pamphlet https://everything2.com/title/csh+programming+considered+har...

and (or?) the appearance of Paul Falstad's Z-Shell saved me ;-0

Re: Test your square brackets

#48
post #27

To me the salient part is that he had an exam on shell scripts?!

I did my degree in 2012-2015; and we had a mandatory first year course on *nix, which IIRC included a test on shell scripting.

Its not really theoretical or applied CS, but it is a core skill for meaningfully doing applied CS.

Re: Test your square brackets

#49
post #33

The ultimately sad part was the professor in a Sun OS machine. In a corner with no where to go, giving demerits because his bash was older than he realized. Reminds me of my college professor that claimed you don’t have to close HTML tags (some you absolutely do) and I proved that you do. Not all of them, but most of them. (Netscape Navigator Days)

Why do you think the prof even used bash? I highly doubt his ancient SunOS machine had a GNU toolchain. `test` and `[` are both POSIX, but if there was no `/bin/[` I doubt the shell in question (original Bourne? Some proprietary Sun shell? Who knows) had it built in either.

Bourne /bin/sh I’m certain of it. If it was Sun OS, it was sh. Was just stating that he was sitting on old while trying to teach new and handing out -1’s for leveraging the new.

Terrible…

Re: Test your square brackets

#50

Whoa! /bin/[ as a binary looking for it's own "]" closing bracket! What a nasty syntax hack! I wonder what the motivation was for doing this rather than just implementing test expression support directly in the shell? I just tried and bash also accepts some other odd filenames in place of "[", so you can do: ln -s /bin/test "-" ln -s /bin/test "$" And use either of these in place of "[" (assuming they are on your pat…

I also love argv[--argc]. Evil genius.

And it's fully legit (from the draft C standard, 5.1.2.2.1, § 2 "Program startup"):

> The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination.

https://www.open-std.org/jtc1/sc22/WG14/www/docs/n1256.pdf [pdf]

Post reply on HN