Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

building conditions dynamically and executing in If stmts

Status
Not open for further replies.

patnim17

Programmer
Jun 19, 2005
111
US
Hi,
I am building this condition check in a loop dynamicallly into a variable say, VAR1..something this this:

for i in 1 2 3
do
VAR1 = "$VAR1"string that builds the condition
done

Now after the for loop I wnat to put this condition into the "if" construct and use it..something like

if ["${VAR1}"]; then
echo "abcd"
else
echo "bmcd"
fi

what is the correct way to do it?

Pat
 
Annihilanic's solution would work. However, I'd like to see a more concrete example - it's pretty unusual to have a problem so intractable that you need to create dynamic conditions on the fly with eval. What problem are you trying to solve (as opposed to 'how are you trying to do it')?

Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
I have weblogic managed instances for different sites we support in out application. now, the number of managed instances for a site is not constant. Some sites have 2, some have 4 some have 6 managed instances. So we have kept this informatio in a separate file, with the site code nnd number of instances.

So I loop through this file for a given site,find the number of managed instances and then build the condition I need to dynamically.

hte condition is basically checking the existemce of log files - one per instance.

If will try the if eval and see how that works...

pat
 
I'm sure it did. But I'm still a bit confused. My shell scripting is a little rusty as I normally use Perl for this kind of stuff, so excuse the pseudocode [sad]. So what you are doing is something like:
Code:
open file
while ! eof
   split line into $site and $instances
   for $i = 1 to $instances
      if (-e /$site/instance$i/log) 
         echo $site $i OK
      else
         echo $site $i has no log!
      end
   end
end
close file
The only reason you'd need to use eval is when the condition itself changes, not just the contents of the variables in the condition. Like
Code:
if (something) $test1 = "$A eq $B"
else $test1 = "($A + $B) * $Z == 42"
eval $test1
maybe.


Steve

[small]"Every program can be reduced by one instruction, and every program has at least one bug. Therefore, any program can be reduced to one instruction which doesn't work." (Object::perlDesignPatterns)[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top