Just annotations of little "how to's", so I know I can find how to do something I've already done when I need to do it again, in case I don't remember anymore, which is not unlikely. Hopefully they can be useful to others, but I can't guarantee that it will work, or that it won't even make things worse.
If condition "if condition ... if condition"
Tags bash, check, conditions
If you want to have a script that would do something if either one of two consequences is true, a simple way to do that would be:
You check if either one is true, when "||" works as "or", then go on checking each one independently again.
But alternatively you can concatenate "checks" within "checks":
I don't know if it would work in every circumstance, but it worked on a simple script I've made just to test that. It's possibly not absurdly superior for that use specifically, but perhaps the principle can be a key for an easier solution in a more complex scenario. Or maybe not. There it is, anyway.
___________________________
10 years later or so, I think I'd be doing it more along the lines of
Seems better to read/understand and no worse in any way than both of the previous versions, particularly better to read than the last one, which is ugly as hell.
Nowadays I also pretty much only use "[["
I'm not even sure the "[" standalone program still exits, maybe it's even a synonymous function within Bash itself.
Code:
if [ condition 1 ] || [ condition 2 ] ; then if [ condition 1 ] ; then consequences for condition 1 fi if [ condition 2 ] ; then consequences for condition 2 fi consequences of either one, but happens only once, not twice fi ...
But alternatively you can concatenate "checks" within "checks":
Code:
if [ ` if [ condition 1 ] ; then consequences for condition 1 fi if [ condition 2 ] ; then consequences for condition 2 fi ` ] ; then consequences of either one, but happens only once, not twice fi ...
___________________________
10 years later or so, I think I'd be doing it more along the lines of
Code:
if [[ condition 1 ]] ; then consequences for 1 ; fi if [[ condition 2 ]] ; then consequences for 2 ; fi if [[ condition 1 || condition 2 ]] ; then consequences for either, to happen only once ; fi
Nowadays I also pretty much only use "[["
I'm not even sure the "[" standalone program still exits, maybe it's even a synonymous function within Bash itself.
Total Comments 0