0

I've seen many examples with multiple conditions related to each other, but there are 2 different unrelated conditions where the second one only should happen if the first one is true. How can I get the following nicely organised and in 1 line?

funtion1
if [[ $valueX == 2 ]]; then
    funtion2
    if [[ $valueY -gt 16 ]]; then
        sleep $valueZ
    fi
fi
Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
Nixx
  • 15
  • 1
  • 7
  • 1
    Your question is tagged with both "bash" and "ash", two different shells. The `[[` syntax is specific to bash; ash doesn't support it. – Keith Thompson Aug 03 '12 at 23:25

1 Answers1

0

You can have it nicely organized, or you can have it in one line.

I'd say what you have now is already nicely organized. I don't think there's any way to reorganize it in fewer lines without making it less legible.

(If the funtion2 weren't there, you could combine the two conditions.)

If you insist on putting that entire chunk of code on line line, you can do it simply by joining the lines and adding semicolons, like this:

funtion1 ; if [[ $valueX == 2 ]]; then funtion2 ; if [[ $valueY -gt 16 ]]; then sleep $valueZ ; fi ; fi

But as I said, I don't think anyone would call this "nicely organized"; it's more difficult to read, and to maintain, than the original multi-line nicely indented version.

Note that the [[ syntax is specific to ; doesn't support it.

(Oh, and you've misspelled "function".)

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • ok, thanks, I had 20 of these after one another and figures it could get more compact.. but guess not :) – Nixx Jul 25 '12 at 21:05
  • @Nixx: You might still be able to reorganize the code if there's some consistent relationship among the conditions. – Keith Thompson Jul 25 '12 at 21:22
  • mhm, not sure, only function2 and valueX are equal for all 20. The other elements are different (although numbered in sequence) – Nixx Jul 25 '12 at 22:00
  • At the moment I'm cleaning up my code and it tuns out that It is prefered to have everything in 1 line. How do I do this? is there a "end of line" charachter that works in all situations? – Nixx Aug 01 '12 at 19:24
  • You can replace end-of-line with `;` in some cases. If you joined all your lines into one, adding semicolons *except* after the `then` lines, then it should work. But such a layout is ugly and difficult to read. *Why* is it preferred to have everything on one line? – Keith Thompson Aug 01 '12 at 21:53
  • The one line would be preferred as I attempt to hide the code which will be included in a 1 lined xml file (just not to make it stand out) inside an app-builder app i'm using :) – Nixx Aug 02 '12 at 14:33
  • @Nixx: It's possible a `case` statement could make your code neater, but if you want it all on one line then neatness isn't really the goal. It's possible that other techniques could make it more *compact* however, but we'd need to see more of the code to determine that. – Dennis Williamson Aug 26 '12 at 23:35
  • @Dennis: well, it turned out the app that would send out this one-lined code messes things up if the line was too long, so I gave up and kept it in 4 lines. And the full script is actually the original color-fade version :) – Nixx Aug 27 '12 at 08:45