Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Bash. What?? Why?

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
Lurker
Posts: 209
Joined: 2007-03-22 23:29
Location: Portland, Oregon

Bash. What?? Why?

#1 Post by Lurker »

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. :)

Grifter
Posts: 1554
Joined: 2006-05-04 07:53
Location: Svea Rike

#2 Post by Grifter »

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...

Lurker
Posts: 209
Joined: 2007-03-22 23:29
Location: Portland, Oregon

#3 Post by Lurker »

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.

Grifter
Posts: 1554
Joined: 2006-05-04 07:53
Location: Svea Rike

#4 Post by Grifter »

I was only talking about double parantheses and double square brackets
Eagles may soar, but weasels don't get sucked into jet engines...

Lurker
Posts: 209
Joined: 2007-03-22 23:29
Location: Portland, Oregon

#5 Post by Lurker »

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.

lacek
Posts: 764
Joined: 2004-03-11 18:49
Location: Budapest, Hungary
Contact:

#6 Post by lacek »

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.

Lurker
Posts: 209
Joined: 2007-03-22 23:29
Location: Portland, Oregon

#7 Post by Lurker »

Good. No math.
State opposite value cause it's easier.

Post Reply