0

output the name of the file with more lines, and how many more it has (one space between the name and number). If there is a tie, the output should be "foobar 0". eg: foo has 1 more line: "foo 1"; bar has 3 more lines: "bar 3", same # lines: "foobar 0". NO IF statements.

why is this not working: [[ $r -gt 0 ]] && { echo foo $r; } || [[ $r -lt 0 ]] && { echo bar $r; } || { echo foobar $r; } $r is the difference between the 2 files

2 Answers 2

3

Note that your cases are disjoint, so you don't actually need the ||.

[[ $r -gt 0 ]] && echo foo $r;     # positive 
[[ $r -lt 0 ]] && echo bar $r;     # negative
[[ $r -eq 0 ]] && echo foobar $r;  # zero

why is this not working

&& and || evaluate from left to right, there's no difference in the precedence between them, which makes it hard to do if .. then .. else style logic with them. You need to add grouping manually with braces or parenthesis. (Or just use the if statement, it's there for a reason.)

For example, this always prints nonnegative, regardless of if the negative prints:

[ $x -ge 0 ]] || echo negative && echo nonnegative

If you want the difference as a positive value, you'll need to negate it in the middle branch, e.g.:

[[ $r -lt 0 ]] && echo bar $(( -r ));
1

With round brackets, it works like described: ([[ $r -gt 0 ]] && { echo foo $r; }) || ([[ $r -lt 0 ]] && { echo bar $r; }) || { echo foobar $r; }

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .