This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
bash [2013/05/08 17:53] k2patel [Bash Tips / Tricks] |
bash [2013/05/08 17:55] k2patel [Bash Tips / Tricks] |
||
---|---|---|---|
Line 366: | Line 366: | ||
=== Truth Table === | === Truth Table === | ||
- | In the if-then-else, exactly one of command2 or command3 will be executed. But in the && || version, command3 runs if either command1 or command2 returns false. | + | <code bash> |
+ | These two commands not the same: | ||
- | We can actually run through all four possibilities. Here's the command (I wonder whether this will get through unscathed?): | + | command1 && command2 || command3 |
+ | if command1 | ||
+ | then | ||
+ | command2 | ||
+ | else | ||
+ | command3 | ||
+ | fi | ||
+ | </code> | ||
+ | |||
+ | In the if-then-else, exactly one of command2 or command3 will be executed. But in the && || version, command3 runs if either command1 or command2 returns false. | ||
+ | |||
+ | <code bash> | ||
for c1 in true false; do for c2 in true false; do echo "$c1 && $c2 || c3"; (echo c1; $c1) && (echo c2; $c2) || echo c3; done; done | for c1 in true false; do for c2 in true false; do echo "$c1 && $c2 || c3"; (echo c1; $c1) && (echo c2; $c2) || echo c3; done; done | ||
+ | </code> | ||
And the output: | And the output: |