Bash. What?? Why?

Need help with C, C++, perl, python, etc?

Bash. What?? Why?

Postby Lurker » 2007-03-23 00:05

Reading about bash at tldp.org
On testing constructs

let "1<2" returns 0 (as "1<2" expands to "1")
(( 0 && 1 )) returns 1 (as "0 && 1" expands to "0")

I understand the first parts, returns 0(true) or 1(false)

But WHY do they expand to "1" but is true or expand to "0" but is false??

I finally Registered. :)
Lurker
 
Posts: 209
Joined: 2007-03-22 23:29
Location: Portland, Oregon

Postby Grifter » 2007-03-23 00:31

putting double stuff { for example [[ stuff ]] or (( stuff )) } around other stuff makes it report that
Eagles may soar, but weasels don't get sucked into jet engines...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby Lurker » 2007-03-23 01:21

That can't be it as:

Let a="1<2"
echo $a
1

No stuff in double stuff.
If I hadn't read it and tried it I would have thought a to be 0 because 1 is less then 2.

Just seems weird to me.
Makes my brain hurt.
Lurker
 
Posts: 209
Joined: 2007-03-22 23:29
Location: Portland, Oregon

Postby Grifter » 2007-03-23 01:36

I was only talking about double parantheses and double square brackets
Eagles may soar, but weasels don't get sucked into jet engines...
Grifter
 
Posts: 1572
Joined: 2006-05-04 07:53
Location: Svea Rike

Postby Lurker » 2007-03-23 03:36

Further reading in an example.

Exit status opposite from [ ... ] construct!

Probably, take way more math then I could understand(which isn't much), to explain why the value is always opposite the true or false state.

or

Since convention says 0=True and 1=False screw the math.

No more thinking. Going to watch the boob tube.
Lurker
 
Posts: 209
Joined: 2007-03-22 23:29
Location: Portland, Oregon

Postby lacek » 2007-03-26 16:57

Actually, shell convention says 0 is true and anything else is false.
This makes sense in a way that in shell you are always checking the exit code of the previously executed program. Since programs can indicate a number of errors (as why the program failed) by setting their exit code to something, these exit codes are considered as errors. So if the program indicated no error (exited with 0), it will treated as true since the previous program was successful.
Since [ is a program (it's a link to 'test', try: man [), its exit code is treated as any other program's exit code. So if the test succeeded, it will set its' exit code to 0. It it failed, the exit code will vary depending on the cause of the failure, but will always be other than 0, thus will be treated as false.
lacek
Moderator Team Member
 
Posts: 769
Joined: 2004-03-11 18:49
Location: Budapest, Hungary

Postby Lurker » 2007-03-27 08:48

Good. No math.
State opposite value cause it's easier.
Lurker
 
Posts: 209
Joined: 2007-03-22 23:29
Location: Portland, Oregon


Return to Programming

Who is online

Users browsing this forum: No registered users and 8 guests

fashionable