LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Blogs > linux-related notes
User Name
Password

Notices


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.
Rate this Entry

If condition "if condition ... if condition"

Posted 08-28-2010 at 06:26 PM by the dsc
Updated 06-01-2023 at 12:47 PM by the dsc

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:

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
...
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":

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
...
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

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
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.
Posted in Uncategorized
Views 1553 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 10:18 PM.

Main Menu
Advertisement
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration