ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I would ask why because IMHO as posted it is easier for my simple mind to understand. For nested tests that duplicate if-then else clauses you need to add parenthesis which is what boughtonp posted but improved. Untested but here is your code.
You can convert a if-then-fi to a && { list; }
But a if-then-else-fi can be converted to a && { list; } || { list; } only if the first { list; } does not alter its initial exit status.
Note1:
there must be a semicolon or a newline before the closing }
Note2:
The echo "OK2" is always true; no true command needed here.
Note3:
The true command is not needed here because the previous statement is always true (both branches have an echo).
A if-then-else-fi is simpler.
Last edited by MadeInGermany; 11-02-2023 at 12:25 PM.
Purpose of having it that way in short: only if DIR /tmp exists, execute inner condition, otherwise skip.
^^ What NevemTeve says.
Quote:
Originally Posted by czezz
Why this is ugly or wrong logic?
"Ugly" in that you cannot easily understand the intended logic simply by looking at it, whereas the (possibly nested) IF/ELSE makes the logic explicit.
Wrong logic in that [...] && ... || alone is not equivalent to IF/ELSE.
Code:
if [...] then
First whatever is here...
else
Second whatever is here...
fi
[...] && (First whatever is here...)
|| (Second whatever is here...)
In the IF/ELSE implementation the Second whatever is here depends ONLY on the [...] test.
In the [...] && ... || implementation Second whatever is here depends on both the [...] test AND the exit status of the First whatever is here...
You can write it such that the end result is more or less equivalent for a specific case, as above, but that only makes it fragile and more obscure, or dare I say, uglier.
OK, i see the point now.
That condition will be added to the crontab and for any cost i do want to avoid adding script.
Also, i find it nicer having "no-if" statement in the oneliner
Side question - does this way of putting IF statement has its name? Does anyone know?
While this thread is "superficially interesting, in a geek-y sort of way," I would almost beg you to please "keep it as a separatefile."
You can write the file in "bash" if you want to, or(!) you can use the so-called "shebang (#!)" feature. If, for example, the first line of your file is: #!/usr/bin/perl, the remainder of your "shell" script can now be written in [Perl]. Or any interpreted language-processor of your pleasure. The shell silently forks an instance of the appropriate interpreter, and the end-user will never know. (Or, care.)
The problem with "one-liners" is: "interpreting them, either when you did not write them, or six weeks after you did."
A "double trouble" with them is – "finding the bug(s)," and maybe first figuring out which "set of chicken-scratches" actually contains the bug(s). ("Triple trouble" if you guessed wrong.) ("Quadruple trouble" if your intended repair introduces another bug(s).) All of this wasted time is avoidable. And expensive.
A "separate file" can be written in the most-clear language of your choosing, and it can include descriptive comments. This clarity and maintainability can make all the difference in the world – yet it makes no difference at all to the computer.
Last edited by sundialsvcs; 11-02-2023 at 05:50 PM.
As it was mentioned it is not really a good idea, && and || is not the same as if/then/else even if sometimes looks similar or you may force them to behave similarly.
Quote:
Originally Posted by czezz
That condition will be added to the crontab and for any cost i do want to avoid adding script.
Putting one-liners into crontab is not a good idea too, instead, it is meant to start a script which will do the real job. But otherwise [obviously] it can be done, it just hurts readability and maintainability
Quote:
Originally Posted by czezz
Also, i find it nicer having "no-if" statement in the oneliner
As long as you don't need to work with it. Again, it just hurts readability and maintainability and against the original concept.
Quote:
Originally Posted by czezz
Side question - does this way of putting IF statement has its name? Does anyone know?
You missed any kind of error handling which is most probably not important for you (or in this case), but not really manageable in a oneliner.
Additionally (as it was already mentioned) you don't need ls at all, you can use the shell itself to count the files.
Also you can simplify the nested if/then/else structures
Code:
[[ -d /tmp/ ]] || { echo "no dir"; exit 1; }
shopt -s nullglob
files=(/tmp/*.gz)
[[ ${#files[@]} -lt 19 ]] || { echo too many files; exit 1; }
echo ok
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.