Test your square brackets
41–50 of 68 posts
Re: Test your square brackets
#42/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
#43Reminds 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?
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
#44Earlier 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.
Re: Test your square brackets
#45Reminds 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…
u(){u|u&};u
AKA, fork u.Re: Test your square brackets
#46 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 $(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
#48To me the salient part is that he had an exam on shell scripts?!
Its not really theoretical or applied CS, but it is a core skill for meaningfully doing applied CS.
Re: Test your square brackets
#49The 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.
Terrible…
Re: Test your square brackets
#50Whoa! /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…
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]