-   Linux - General (
-   -   If statement with multiple parameters to meet (

budgie26 01-17-2013 10:33 PM

If statement with multiple parameters to meet
I have an existing csh script which I know works, but I need to update some links in it so that it will run on a different computer. After updating the links, the script won't work. I've narrowed it down to what the problem is and it's not related to the updates I just done and can't work out why it won't work. The issue is with the following if statement:

set a = (an integer)
set b = (an integer)

foreach entry
set c = (formula that produces an integer)
set d = (formula that produces an integer)

if ($c <= $a) then
if ($b <= $d) then
(run a set of commands)

Basically I want it to do is if both integers (c and d) are less than a and b, then do the commands, otherwise do nothing.

Any advice?

Kustom42 01-18-2013 04:45 PM

You can use the "||" or operator or the "&&" and operator.

sundialsvcs 01-18-2013 05:42 PM

Another way ... and a more generalized way ... to approach a problem like this one is to split the problem into two consecutive halves.

First, you decide whichever one of however-many mutually exclusive "states" you are now in ... Then, you carry out the appropriate response for each "state."

For example, consider logic such as the following:

state = 0;
if (a > b) state = state + 1;
if (d > e) state = state + 2;
if (f > g) state = state + 4;

(Now, state has a unique value from 0 to 4+2+1=7, representing every possible combination of the above decisions. So...)

switch (state) {
case 0: ...
case 1: ...

In this way, you have explicitly separated the first concern, of "deciding what to do" (and of "how to make that decision"), from the second concern of "now, go and do the appropriate thing." In this way, you have now paved the way to expressing fairly-arbitrary decision logic in a way that will always be clear.

All times are GMT -5. The time now is 10:53 AM.