Gentoo Archives: gentoo-amd64

From: Duncan <1i5t5.duncan@×××.net>
To: gentoo-amd64@l.g.o
Subject: [gentoo-amd64] Re: How to use &&
Date: Sun, 04 Jan 2009 23:07:13
Message-Id: pan.2009.01.04.23.07.00@cox.net
In Reply to: Re: [gentoo-amd64] How to use && by Richard Freeman
1 Richard Freeman <rich0@g.o> posted 4960DC68.1060604@g.o,
2 excerpted below, on Sun, 04 Jan 2009 10:57:28 -0500:
3
4 > Using && in this way is technically a bit of a hack - the && operator
5 > was intended to evaluate a boolean expression and not as a replacement
6 > for "if". That doesn't stop 99.9999999% of shell scripters from using
7 > it, however.
8
9 Talking about which... for single-clause "if" versions I use &&/|| almost
10 exclusively, these days, with {compound statements} wrapped and indented
11 for clarity as necessary, to the point that reading shell code with a
12 single-clause "if" has me wondering where the "else" is.
13
14 Now, every time I come to a multi-clause if/then/else or even a compound
15 if (if/then/elseif or if/then/elseif/then/elseif...), I wonder what the
16 equivalent logic should be there. However, it's apparently convoluted
17 enough I'm not the only one not using it very often, so I don't believe
18 I've come across a proper example to copy. Every time I try to map the
19 logic of what it should look like, my mind gets tangled up. Thus my
20 question is simple enough, are there accepted ways of using &&/|| in
21 place of compound if statements, that once I see, I'll probably go "Duh!"?
22
23 Some day I really ought to go thru a formal advanced shell scripting
24 tutorial. I learned practical shell scripting based on the intro in
25 "Running Linux", the Bash appendix in "Linux in a Nutshell" (which I
26 still keep within reach when I'm on a sysadminning binge), and tracing
27 the logic of and then splitting into several smaller scripts the big
28 Mandrake rcsysinit script... back about Mandrake 8.1 time. It's amazing
29 how effective that was at actually learning the /practical/ side of
30 things, to the point I've traced and debugged and proposed for Roy
31 several OpenRC patches since I run the ~arch and sometimes hard-masked
32 versions (tried the git version for awhile, before deciding that was /
33 too/ raw for me), but I'm still missing some of the more advanced
34 tricks. I already have some links from Steve Long on [] vs [[]] I need
35 to go thru at some point. If someone could provide a nice link or two
36 here covering the above if/then/else &&/|| shortcuts question, I'd
37 probably actually go thru it before the []/[[]] stuff, since the &&/||
38 stuff bugs me every time I need an if conditional with both then and else
39 clauses.
40
41 Or maybe the reason I keep getting tangled up trying to follow the logic
42 is that it's not possible, short-circuit evaluation doesn't work that way?
43
44 --
45 Duncan - List replies preferred. No HTML msgs.
46 "Every nonfree program has a lord, a master --
47 and if you use the program, he is your master." Richard Stallman