Ksh WTF usage



  • Using it for bloody ages, I just have realized it : if and test usage is counter-intuitive in ksh.

    Math : you compare numbers with <, >, <=, >=, =... Many langages are using == as comparator and = as assign operator.

    Ksh : you compare strings with <, >, = and != . You compare numbers with... -lt, -gt instead of < and > ! I would have rather used the mathematical operators for numbers, but I didn't invent Ksh.

    WTF ?



  • Probably they wanted to keep it compatible with the bourne shell.



  • <abrasive>

    So, what kind of useless niche language is this Ksh thing?

    </abrasive>



  • @dhromed said:

    <abrasive>

    So, what kind of useless niche language is this Ksh thing?

    </abrasive>

    Know shell programming? On *nix systems? Ksh is Korn Shell anyway.

    Hmmm... 'abrasive' doesn't mean 'sarcasm' or anything I hope (more like 'rough' or something?) :)

    Hmmm... You ARE ironic, right?

    Hmmm... WTF?!?!?



  • @ammoQ said:

    Probably they wanted to keep it compatible with the bourne shell.

    Precisely. Also sh, which preceded ksh, has no math operators, so they used the (math) operators from sh's string comparisons consistently in ksh, and then used the only other operators they had available for the math stuff when it was added.

    But, wouldn't we all do stuff differently if we could go back and do it again with the benefit of hindsight?

    Imagine if Paula had just added that one little 'i' - countless brillant posts might have been avoided!



  • @impslayer said:

    @dhromed said:

    <abrasive>

    So, what kind of useless niche language is this Ksh thing?

    </abrasive>

    Know shell programming? On *nix systems? Ksh is Korn Shell anyway.

    Hmmm... 'abrasive' doesn't mean 'sarcasm' or anything I hope (more like 'rough' or something?) :)

    Hmmm... You ARE ironic, right?

    Hmmm... WTF?!?!?



    Abrasive is violently derogatory. "What the fuck are you doing, bitch?!" *bitchslap*

    Never done shell programming.
    Does dir/p in DOS count?

    I used the <abrasive> tags to denote that I thought not using intuitive operators in a language is a WTF.

    But I didn't know it was shell programming, which is obviously different from coding in a text editor.


  • @dhromed said:

    Never done shell programming.
    Does dir/p in DOS count?

    I used the <abrasive> tags to denote that I thought not using intuitive operators in a language is a WTF.

    But I didn't know it was shell programming, which is obviously different from coding in a text editor.

    Actually, the Unix shell is a full-blown programming language, unlike the DOS command prompt. It does have quite a number of oddities, though.



  • you wouldn't even think of calling this a wtf if you
    had only a slight idea of what characters >, < and ! stand for in
    (virtually) every shell.



    guess what! in xslt, there's &lt; instead of <. now that's wtf!




  • @judas said:

    you wouldn't even think of calling this a wtf if you
    had only a slight idea of what characters >, < and ! stand for in
    (virtually) every shell.



    guess what! in xslt, there's &lt; instead of <. now that's wtf!




    You're probably right. I don't have that slight idea. I've never used a > in any shell command in my life.

    XSLT escapes the tag-defining chars for a pretty good reason. It's in XML itself.



  • @codeman said:

    Imagine if Paula had just added that one little 'i' - countless brillant posts might have been avoided!



    God damnit, that's her NAME damnit!



  • <, |, and > are the Traditional (and therefore "most intuitive", if you're a shell monkey) tokens for controlling input and output redirection.  This is true of all (normal) unix shells, windows cmd.exe and command.com, and DOS going back quite a ways.  Probably CP/M, but that's before my time.

    The three simple cases are
    foo > somefile:  run 'foo', redirecting standard output to the file 'somefile'.
    foo < somefile:  run 'foo', redirecting standard input to the file 'somefile' (instead of the console, normally).
    foo | bar: run foo, and run bar.  redirect foo's standard output to bar's standard input.

    There's some more complex cases for handling merging streams and redirecting streams aside from stdin and stdout.  Seems kind of arcane, but you can do some fairly useful things with it.

    That said, it's pretty weird that ksh uses < and > for string comparisons.

    (It's worth noting that the -lt, -gt, -eq, et cetera operators may not be shell intrinsics, too:

    [owen@verdandi ~]$ which [
    /usr/bin/[

    which is a link to /usr/bin/test.  Read the test(1) manpage, if you care, but the gist is that test is a standalone program which evaluates logical expressions and sets its exit code to 0 (success) if the expression is true or non-0 (failure) if the expression is untrue.  Possibly a holdover from sh; I believe bash implements [(expression)] internally rather than delegating to /usr/bin/[ but I don't know for sure.)



  • @impslayer said:

    @dhromed said:

    <abrasive>

    So, what kind of useless niche language is this Ksh thing?

    </abrasive>

    Know shell programming? On *nix systems? Ksh is Korn Shell anyway.

    Hmmm... 'abrasive' doesn't mean 'sarcasm' or anything I hope (more like 'rough' or something?) :)

    Hmmm... You ARE ironic, right?

    Hmmm... WTF?!?!?

    Iron could be rough and abrasive ;)



  • No, that's not all yet.

    sh:   if [ x"$x" \> x"$y" ]; then echo "String x > String y"; fi
    perl: if($x gt $y) { print "String x > String y\n"; }
    
    sh:   if [ $x -gt $y ]; then echo "Number x > Number y"; fi
    perl: if($x > $y) { print "Number x > Number y\n"; }
    

    Confusing, isn't it?


  • Perl's makes more sense to me; the > operator is used for the numeric test.

    The language DOES have to have different operators for comparison, because a single SV can contain both a SvIV and a SvPV [perlguts], that is, a scalar can contain a seperate numeric value from its string value.  This is occasionally useful for things like error messages.  Not having a seperate operator for strings would make that ambiguous.  Of course, an ascii compare is different from a numeric compare, anyway, and using either operator will coerce the value into it's correct compare type.

    dhromed, as someone said earlier in more technical terms, the < and > terms are actually reserved, much like in XML, as shell tokens.  Were I asked, the REAL wtf is that the shell is using reserved tokens, and then in a non-intuitive manner.  I hope that ksh at least takes those as built-ins between []s so that you don't always have to escape them.



  • No, no, the real WTF is that a traditional sh cannot do arithmetics at all. test respectively [ is an external program, and so is expr which is used for arithmetics. So the shell obviously cannot do anything about the "escaping case". The other real WTF is that [ does not work well with string comparisons, which is why one usually adds those xs to both strings:

    [code language="sh"]
    $ [ "\!" = "x" ]
    [: =: unexpected operator
    [/code]

    The more advanced shells do have a built-in syntax for arithmetics:

    [code language="sh"]
    ((x = 3))
    if ((x < 4)); then
      echo 3 is less than 4
    fi
    [/code]

    bash and zsh agree on this syntax.


Log in to reply