• Bas (unregistered)

    I am much more interested in the story behind this, just like with every sign, every check has a failure that led to its creation.

  • beef (unregistered)

    I also like the bad grammar, and tying it all in to the laws of physics.

  • LCrawford (unregistered)

    Perhaps some distros don't include the 'true' command?

  • El Dorko (unregistered)

    Maybe this is a test where one can prove to be sane by calling this out, and removing it? No one has yet to pass...

  • I'm not a robot (unregistered)

    Could be an attempt to protect against being run on systems that don't use zero for success? Not to actually work on those systems, just to prevent it from trying to run and producing weird results. man 3 exit says:

    The use of EXIT_SUCCESS and EXIT_FAILURE is slightly more portable (to non-UNIX environments) than the use of 0 and some nonzero value like 1 or -1. In particular, VMS uses a different convention.

    Now, I don't know if bash runs on VMS, and if it does, it might well translate exit statuses to the Unix convention, but the author of the code might not have thought of that.

    (It also makes the "exit 1" rather suspect, but again, the author might not have thought it through entirely.)

  • beef (unregistered) in reply to LCrawford

    Just tested this on Cygwin, apparently it's not installed by default, and ${?} gives 127

  • RLB (unregistered) in reply to I'm not a robot

    That's in reference to a return from a C main(). And it's only half correct. Of course (of course!) the bit that's correct is the bit that doesn't apply here: it's true that EXIT_FAILURE is more portable than a semi-random non-zero value, but a return value of zero must indicate success, VMS be hanged.

  • Simon (unregistered) in reply to I'm not a robot

    Could be an attempt to protect against being run on systems that don't use zero for success?

    Yep, that'd be my bet... I'd guess that the script author encountered one of those systems by chance, and started writing ultra-defensive code to detect it (probably copy-pasted into all future scripts).

  • Anon (unregistered)

    If true returns 1 wouldn't then the -ne in the if also possibly return 0 if they are not equal. This means that it would pass sanity check

  • LXE (unregistered)

    The reference to the fundamental laws of nature reads like an exaggeration, but there are a few more variables here:

    • the digital representation of the integral number 0 (Indian/Arabic decimal digits, no decoration, no leading zeroes, no dot);
    • the character code of the digit 0;
    • the octet representation of the character code of the digit 0.

    I disagree with calling those conventions "laws of physics" on the ground of free will, but maybe the contributor is a hardcore Laplacian determinist.

  • Brian Boorman (google)

    I'd like to see these "microcontrollers" that Remy claims are running bash. Microntroller != embedded Microprocessor.

    Remy says: the bash scripts are for deploying code to microcontrollers

  • I'm not a robot (unregistered) in reply to Brian Boorman

    I'd like to see where the article claims that bash is running on the microcontroller itself.

  • Barry Margolin (google)

    Since the shebang uses /usr/bin/env bash, it finds whatever version of bash is in the user's $PATH. Maybe it's trying to detect a bogus version of bash in the user's search path (maybe he's made the mistake of having "." in his path). Although reporting that as a failure of the laws of nature seems to be a bit of an exxageration.

  • (nodebb)

    This is an OLD convention of testing that RAM images aren't corrupt. And "OLD" means from the 70s. I also have seen this used to prevent running on the "wrong" UNIX because on some, yes and true return 1 but no and false return 0.

    Y'know.... cuz using "uname" is too much trouble.

  • Paul Neumann (unregistered) in reply to Bananafish

    You must be one of those browser sniffing over feature detection aficionados.

  • Anonymous') OR 1=1; DROP TABLE wtf; -- (unregistered)
    $ alias true=false; true; echo $?
    1
    

    Checkmate.

  • (nodebb) in reply to Anonymous') OR 1=1; DROP TABLE wtf; --

    Nope. https://www.screencast.com/t/krP2LlnKn

    I get a return of zero.

    Addendum 2018-06-18 15:28: EDIT: I think this is because bash parsed the entire line before executing the alias statement. Subsequent invocations of "true" returned 1. Only this first one returned zero.

  • Somebody Somewhere (unregistered) in reply to Brian Boorman

    For all those who claim that bash can't be run on a microcontroller, I hereby present Linux running on an 8-bit ATmega1284p: http://dmitry.gr/?r=05.Projects&proj=07.%20Linux%20on%208bit

  • siciac (unregistered) in reply to Ross_Presser

    Just use a function: true() { return 42; }; true; echo $?

  • Anonymous') OR 1=1; DROP TABLE wtf; -- (unregistered) in reply to Ross_Presser

    Derp. Aliases also apparently don't work from inside shell scripts. So going with a function like siciac suggested is the way to go.

  • eric bloedow (unregistered)

    ifor some reason, this old story came to mind: https://thedailywtf.com/articles/a-shining-perl

  • (nodebb)

    TIL about the program "true". I didn't know about it before. I use IBM JCL, and it has a program that returns true as well. But in keeping with IBM sensibility, it is named: IEFBR14

    Put that in your script and smoke it.

  • (nodebb)

    Re: IBM JCL...

    The program IEFBR14 in the beginning only 'returned' since the callers address was left in register 14 when the program started. The register that contained the return code (R0?) was whatever was there when the routine was called. In later versions, the register that had the return code was set to zero, but that was a later addition.

    Most of the time this routine was used because what you wanted was the after effects of the JCL, like delete the "dataset" (AKA file), or allocate it on secondary storage (blocking factors, etc.).

    Please remember, this was all invented in the 60's, which for many of us was a while ago (ancient history?).

  • Brian Boorman (google) in reply to Brian Boorman

    @Remy: Why wouldn't you have your CM build the product using microcontrollers that have been pre-programmed with your released code? Most distributors provide this value-add service. Much more efficient than loading code at product test cycle.

  • (nodebb)

    Now, maybe BASH isn't BASH anymore. Maybe true has been patched to fail. Maybe, maybe, maybe, but honestly, I'm wondering whose sanity is actually being checked in the sanity_check?

    Chesterton's Fence (as evidenced by a couple of cromulent suggestions already made.)

    I'm more interested as to why this wasn't put into a file of its own (with other things that are used frequently) and merely sourced in every script.

  • (nodebb) in reply to Paul Neumann

    You must be one of those browser sniffing over feature detection aficionados.

    Nah. I'm just old.

  • (nodebb) in reply to PJH

    Chesterton's Fence (as evidenced by a couple of cromulent suggestions already made.)

    I'm more interested as to why this wasn't put into a file of its own (with other things that are used frequently) and merely sourced in every script.

    Much more fun to have boiler plate templates as the start of all scripts. This way, when the template changes, ALL scripts must be rewritten over the new template. Also, in places that don't do revision control, you can sorta-kinda-maybe tell when a script was written based on the particular boiler plate at the top.

    The script in the article, for example, was written some time between 1970 and yesterday.

  • siciac (unregistered) in reply to Anonymous') OR 1=1; DROP TABLE wtf; --

    There are so many gotchas with aliases, the only reason to use them is when you need to quickly rebind a command to mess with someone.

  • (nodebb) in reply to Bananafish

    Even older that that, m'lud. This, in fact, goes back to the Roman Empire. Which, as we know, did not have zero to indicate successful termination of their C programs (which in turn was one of the main reasons of its downfall).

  • Duke of New York (unregistered)

    if you're not running Bash on a microcontroller ... you should be!

  • Some 'true' fun (unregistered)

    http://trillian.mit.edu/~jc/humor/ATT_Copyright_true.html

  • death the kid (unregistered)

    i think it'd be safer to use, that's what i'd use: echo "try this" > /dev/null; echo $?

    as remembering "true = 0 or 1" , and in what cases, is too much energy consuming for me... Universe is just concerning about energy these days..

  • (nodebb)

    By default, Bash exits with status '0', indicating success. Thus the simplest implementation of 'true' is a do-nothing bash script, with just the hashbang line.

    TRWTF is copious amount of copyright notices and change logs (version: 1.6) in some such implementations, for example:

    bash-2.05$ cat /bin/true
    #!/usr/bin/sh
    #       Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
    #         All Rights Reserved
    
    #       THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
    #       The copyright notice above does not evidence any
    #       actual or intended publication of such source code.
    
    #ident  "@(#)true.sh    1.6     93/01/11 SMI"   /* SVr4.0 1.4   */
    
  • Duke of New York (unregistered)

    true() { :; } false() { ! :; } file_not_found() { grep <&2-; }

  • o11c (unregistered)

    This is nothing compared to the start of literally ever autoconf-generated ./configure script

  • Rocky (unregistered)

    Nobody remarked on the hardcoded ESC sequences? Wtf is wrong with people. ;)

    tput is your friend, that way it doesn't really matter (in most cases) what weird terminal emulation people are using.

  • Duke of New York (unregistered)

    if true doesn't work, what makes you think that tput will?

  • Richard O'Keefe (unregistered) in reply to LCrawford

    I once used a Unix distribution where true(1) had originally been implemented as a completely empty file. Then a reseller had added their own copyright to it, without "#" comment markers. This meant that true(1) always failed.

  • tlhonmey (unregistered)

    Reminds me of a story I stumbled across once about the company Unix folks having monthly programming competitions in Bash to see who could write the most useful thing. One of the C programmers was always interested in reviewing the results, but didn't participate other than that for a couple of years.

    Then one month he made a submission: A functional C compiler written in Bash. He won that month and nobody was able to top it so the competition kind of faded out after that.

Leave a comment on “The Sanity Check”

Log In or post as a guest

Replying to comment #:

« Return to Article