LinuxQuestions.org
Review your favorite Linux distribution.
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 08-28-2010 at 06:28 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.
Posted in Uncategorized
Views 669 Comments 0
« Prev     Main     Next »
Total Comments 0

Comments

 

  



All times are GMT -5. The time now is 03:36 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration