Strange Brouhaha

Thursday, October 21, 2004

Please explain this

Now that is dead, there's nothing tying this site to my office. So I'm gonna bitch about work for a second. Will this make some people unhappy? Undoubtedly. Do I care? No.

In many programming languages, tests for equality are done with a double equal, as in this little snippet, which could be C or C++ or Java or what have you:

if (x=="foo") {
. . . // do stuff here

In the Bourne shell scripting language, string comparisons are done with a single equal sign, as in

if [ "$x" = "foo" ]
. . . # do stuff here

If you use a single equal in the first example, you've made an assignment: the variable x now has the value "foo," regardless of whether it did before. The statement x = "foo" will therefore always evaluate to true, the code in that "if" block will always run, and you've created a bug. It won't stop your program from compiling and running, but it will stop it from working correctly.

If you use a double equal in that second example, your script will not run. It will not run on Linux. It will not run on Solaris. It will not run on AIX. It will not run at all. Your script will stop, dead. It will stop every time. I can't think of a circumstance under which it will not stop. I could be wrong, I'll admit it. But I don't think I am.

Now, I'm not a great programmer. In fact, I'm no more a programmer than I am a Biblical scholar. But I do know some stuff, and one thing I know is that before you release code to QA to be checked, you RUN THE CODE. Don't you? Shoot, I'm so paranoid about my own code that it takes me forever to write a few hundred lines because I have to build each unit and test it on its own. Am I nuts, here? Wouldn't you run that thousand-line script at least once before you release it? ONCE? After all, the =/== mistake is easy to make when you work with a dozen different languages all the time, each with its own syntactical pecularities, no two quite alike. I can totally understand WHY the mistake gets made.

What I can't understand is why it's in the release. Why it's in the release SEVEN TIMES. These guys aren't dumb, I know they're not, but why in God's name wouldn't they have RUN the script and SEEN that it didn't work? It is indeed my job to check for these things, but...aren't we supposed to get things that work just a little bit?


  • Just in case somebody ever reads this and decides I'm stupid (and I won't argue with you about that), it seems to actually depend on which shell you're using and on what platform.

    I stand by what I said about it not working. In situ, the double-equal comparison definitely did not work. It did not work on any of my test machines. I was using ksh on all of those machines, because the programmers were very rigid on that point: "Nobody serious uses bash." Well, whatever. It didn't work.

    However, revisiting this issue today on my Macintosh, using a darwin build of bash 2.05b.0(1)-release, the double-equal comparison statement works the same as a single-equal comparison in shell scripts. Perhaps bash allows this, I don't know. (In OS X, it looks like /bin/sh is a hard link to /bin/bash.) So there actually are circumstances under which the double-equal comparison will work.

    By Blogger Robert, at 10:16 PM  

  • Oh, here's the test code.

    if [ $FOO == "bar" ]
    echo "Equal"
    echo "Not Equal"

    It works correctly when run (i.e. returning "Equal"). If you change the value of the declaration on line 1 to FOO="baz" and rerun, you'll get "Not Equal," as expected. If you change the double-equal to a single equal and then rerun both tests, you'll get the same results.

    By Blogger Robert, at 10:20 PM  

Post a Comment

<< Home