Boston Linux & UNIX was originally founded in 1994 as part of The Boston Computer Society. We meet on the third Wednesday of each month at the Massachusetts Institute of Technology, in Building E51.

BLU Discuss list archive


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Discuss] through the looking glass



On Sat, 2 Jun 2018 12:42:00 -0400
dan moylan <jdm at moylan.us> wrote:

> richard pieri writes:
> On 6/1/2018 7:32 PM, dan moylan wrote:
> >> also b2 writes out empty files abc and xyz.
> >>
> >> i've surely missed something fundamental, or did i just
> >> step into an alternate universe?  
> 
> > What you missed isn't so obvious.  
> 
> > "[" is a synonym for /bin/test with the caveat that a closing "]" is
> > required. What's happening is that "[ $st1 > $st2 ]" becomes:  
> 
> >   /bin/test abc > xyz  
> 
> > "/bin/test abc" returns true, and you get an empty file because test
> > generates no output, only return codes 0 (true) or 1 (false).  
> 
> > The portable fix is to rewrite your logic because the test command
> > only allows for "=" and "!=" in string comparisons.  
> 
> > The bash-specific fix is to change your single brackets to double
> > brackets: "[[ $st1 > $st2 ]]". "[[" is a bash built-in and it
> > bypasses all of the above problems. But it's not portable.  
> 
> thanks for your lucid explanation.

And just for a laugh, check out the following. It returns "<" or ">"
as expected (but it's undefined if they're equal), and nowhere does it
use any kind of "greater than" or "less than" logic:

if test "`echo "$1""\n""$2" | sort | head -n1`" = "$1"; then echo "<";else echo ">"; fi 

LOL

SteveT

Steve Litt 
June 2018 featured book: Twenty Eight Tales of Troubleshooting
http://www.troubleshooters.com/28





BLU is a member of BostonUserGroups
BLU is a member of BostonUserGroups
We also thank MIT for the use of their facilities.

Valid HTML 4.01! Valid CSS!



Boston Linux & Unix / webmaster@blu.org