compair multiple variable need more help..
compair multiple variable need more help..
am 28.09.2007 14:55:34 von Jobs Circular group
hello when i try to compair follwoing variable by "OR" (||) it only
compair first variable , i use ksh
can any body help ?
if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
[ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
Re: compair multiple variable need more help..
am 28.09.2007 15:02:52 von Tiago Peczenyj
On Sep 28, 9:55 am, Nikunj Khakhar wrote:
> hello when i try to compair follwoing variable by "OR" (||) it only
> compair first variable , i use ksh
>
> can any body help ?
>
> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
My 2 and 1/2 cents
$ A=1
$ B=2
$ C=3
$ D=0
$ if [[ $A -ne 0 || $B -ne 0 || $C -ne 0 || $D -ne 0 ]]
> then echo 'ok'
> else echo 'nok'
> fi
ok
Re: compair multiple variable need more help..
am 28.09.2007 16:06:07 von Ed Morton
Nikunj Khakhar wrote:
> hello when i try to compair follwoing variable by "OR" (||) it only
> compair first variable
It'd be good if you showed us what values you've tried that make you say
that and post the smallest executable script that can reproduce the
problem so we can verify and solve it.
> , i use ksh
>
> can any body help ?
>
> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
>
This won't solve your problem (if you really have one), but instead of
all those negative tests, use positives to make it clearer what you're
doing. I.e. instead of:
if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
[ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
then
echo "got a value"
else
echo "got all zeros"
fi
this would be clearer:
if [ "$DATA" -eq 0 ] && [ "$DATALOG" -eq 0 ] &&
[ "$MISC" -eq 0 ] && [ "$LOGS" -eq 0 ]
then
echo "got all zeros"
else
echo "got a value"
fi
Any time you find yourself using a "not" (e.g. "!" or "-ne"), really
stop and think about what you're doing as negatives are in general
harder to understand than positives, and single-negatives open the door
to future double-negatives which get much harder to understand.
Regards,
Ed.
Re: compair multiple variable need more help..
am 28.09.2007 16:26:53 von Maxwell Lol
Nikunj Khakhar writes:
> hello when i try to compair follwoing variable by "OR" (||) it only
> compair first variable , i use ksh
>
> can any body help ?
>
> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
You seem to be having problems with this.
If DATA is not zero, then the || condition is never checked.
> DATA=1
> [ "$DATA" -ne 0 ] || echo yes && echo no
no
I know of several ways to check multiple variables:
if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] || [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
if [ "$DATA" -ne 0 -o "$DATALOG" -ne 0 -o "$MISC" -ne 0 -o "$LOGS" -ne 0 ]
if [[ "$DATA" -ne 0 || "$DATALOG" -ne 0 || "$MISC" -ne 0 || "$LOGS" -ne 0 ]]
If you are unsure of the results, and if these forms are different,
you can easily test them using something like:
-----------
#!/bin/sh
for DATA in 0 1
do
for DATALOG in 0 1
do
for MISC in 0 1
do
for LOGS in 0 1
do
printf "%d %d %d %d " $DATA $DATALOG $MISC $LOGS
if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] || [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
then
printf "%s " yes
else
printf "%s " no
fi
if [ "$DATA" -ne 0 -o "$DATALOG" -ne 0 -o "$MISC" -ne 0 -o "$LOGS" -ne 0 ]
then
printf "%s " yes
else
printf "%s " no
fi
if [[ "$DATA" -ne 0 || "$DATALOG" -ne 0 || "$MISC" -ne 0 || "$LOGS" -ne 0 ]]
then
printf "%s " yes
else
printf "%s " no
fi
echo ""
done
done
done
done
------------------
This outputs
0 0 0 0 no no no
0 0 0 1 yes yes yes
0 0 1 0 yes yes yes
0 0 1 1 yes yes yes
0 1 0 0 yes yes yes
0 1 0 1 yes yes yes
0 1 1 0 yes yes yes
0 1 1 1 yes yes yes
1 0 0 0 yes yes yes
1 0 0 1 yes yes yes
1 0 1 0 yes yes yes
1 0 1 1 yes yes yes
1 1 0 0 yes yes yes
1 1 0 1 yes yes yes
1 1 1 0 yes yes yes
1 1 1 1 yes yes yes
which tells me that these three versions are logically equivalent
At least - when using bash.
Let's check on your original problem:
>i have for variable DATA,DATALOG,MISC and LOGS
>if any of the variable value is non zero than i want fire a mail
So all three versions seem to do what you want to do.
Re: compair multiple variable need more help..
am 28.09.2007 16:45:07 von Maxwell Lol
Maxwell Lol writes:
> You seem to be having problems with this.
Let me follow up.
As we understand it, DATA can be a zero or non-zero integer. If you
are looking to distinquish between a zero-length string vs. a string
of 1 or more characters, there's a different test operator.
Re: compair multiple variable need more help..
am 28.09.2007 22:56:45 von Michael Tosch
Ed Morton wrote:
> Nikunj Khakhar wrote:
>
>> hello when i try to compair follwoing variable by "OR" (||) it only
>> compair first variable
>
> It'd be good if you showed us what values you've tried that make you say
> that and post the smallest executable script that can reproduce the
> problem so we can verify and solve it.
>
>> , i use ksh
>>
>> can any body help ?
>>
>> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
>> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
>>
>
> This won't solve your problem (if you really have one), but instead of
> all those negative tests, use positives to make it clearer what you're
> doing. I.e. instead of:
>
> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
> then
> echo "got a value"
> else
> echo "got all zeros"
> fi
>
> this would be clearer:
>
> if [ "$DATA" -eq 0 ] && [ "$DATALOG" -eq 0 ] &&
> [ "$MISC" -eq 0 ] && [ "$LOGS" -eq 0 ]
> then
> echo "got all zeros"
> else
> echo "got a value"
> fi
>
> Any time you find yourself using a "not" (e.g. "!" or "-ne"), really
> stop and think about what you're doing as negatives are in general
> harder to understand than positives, and single-negatives open the door
> to future double-negatives which get much harder to understand.
>
> Regards,
>
> Ed.
be positive!
sounds indeed better than
don't be negative!
--
Michael Tosch @ hp : com
Re: compair multiple variable need more help..
am 28.09.2007 23:00:40 von Ed Morton
Michael Tosch wrote:
> Ed Morton wrote:
>
>> Nikunj Khakhar wrote:
>>
>>> hello when i try to compair follwoing variable by "OR" (||) it only
>>> compair first variable
>>
>>
>> It'd be good if you showed us what values you've tried that make you
>> say that and post the smallest executable script that can reproduce
>> the problem so we can verify and solve it.
>>
>>> , i use ksh
>>>
>>> can any body help ?
>>>
>>> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
>>> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
>>>
>>
>> This won't solve your problem (if you really have one), but instead of
>> all those negative tests, use positives to make it clearer what you're
>> doing. I.e. instead of:
>>
>> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
>> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
>> then
>> echo "got a value"
>> else
>> echo "got all zeros"
>> fi
>>
>> this would be clearer:
>>
>> if [ "$DATA" -eq 0 ] && [ "$DATALOG" -eq 0 ] &&
>> [ "$MISC" -eq 0 ] && [ "$LOGS" -eq 0 ]
>> then
>> echo "got all zeros"
>> else
>> echo "got a value"
>> fi
>>
>> Any time you find yourself using a "not" (e.g. "!" or "-ne"), really
>> stop and think about what you're doing as negatives are in general
>> harder to understand than positives, and single-negatives open the
>> door to future double-negatives which get much harder to understand.
>>
>> Regards,
>>
>> Ed.
>
>
>
> be positive!
> sounds indeed better than
> don't be negative!
>
>
and way better than
don't not be positive!
Re: compair multiple variable need more help..
am 29.09.2007 09:48:13 von Jobs Circular group
On Sep 28, 4:00 pm, Ed Morton wrote:
> Michael Tosch wrote:
> > Ed Morton wrote:
>
> >> Nikunj Khakhar wrote:
>
> >>> hello when i try to compair follwoing variable by "OR" (||) it only
> >>> compair first variable
>
> >> It'd be good if you showed us what values you've tried that make you
> >> say that and post the smallest executable script that can reproduce
> >> the problem so we can verify and solve it.
>
> >>> , i use ksh
>
> >>> can any body help ?
>
> >>> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
> >>> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
>
> >> This won't solve your problem (if you really have one), but instead of
> >> all those negative tests, use positives to make it clearer what you're
> >> doing. I.e. instead of:
>
> >> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
> >> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
> >> then
> >> echo "got a value"
> >> else
> >> echo "got all zeros"
> >> fi
>
> >> this would be clearer:
>
> >> if [ "$DATA" -eq 0 ] && [ "$DATALOG" -eq 0 ] &&
> >> [ "$MISC" -eq 0 ] && [ "$LOGS" -eq 0 ]
> >> then
> >> echo "got all zeros"
> >> else
> >> echo "got a value"
> >> fi
>
> >> Any time you find yourself using a "not" (e.g. "!" or "-ne"), really
> >> stop and think about what you're doing as negatives are in general
> >> harder to understand than positives, and single-negatives open the
> >> door to future double-negatives which get much harder to understand.
>
> >> Regards,
>
> >> Ed.
>
> > be positive!
> > sounds indeed better than
> > don't be negative!
>
> and way better than
> don't not be positive!- Hide quoted text -
>
> - Show quoted text -
hello,
i done with what i wanted , i used below command
if [ \( $DATA -eq 0 \) -a \( $NEW -eq 0 \) -a \( $MISC -eq 0 \) -a \
( $LOG -eq 0 \) ]
FYI.....
Re: compair multiple variable need more help..
am 29.09.2007 15:00:30 von Michael Tosch
Nikunj Khakhar wrote:
> On Sep 28, 4:00 pm, Ed Morton wrote:
>> Michael Tosch wrote:
>>> Ed Morton wrote:
>>>> Nikunj Khakhar wrote:
>>>>> hello when i try to compair follwoing variable by "OR" (||) it only
>>>>> compair first variable
>>>> It'd be good if you showed us what values you've tried that make you
>>>> say that and post the smallest executable script that can reproduce
>>>> the problem so we can verify and solve it.
>>>>> , i use ksh
>>>>> can any body help ?
>>>>> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
>>>>> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
>>>> This won't solve your problem (if you really have one), but instead of
>>>> all those negative tests, use positives to make it clearer what you're
>>>> doing. I.e. instead of:
>>>> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
>>>> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
>>>> then
>>>> echo "got a value"
>>>> else
>>>> echo "got all zeros"
>>>> fi
>>>> this would be clearer:
>>>> if [ "$DATA" -eq 0 ] && [ "$DATALOG" -eq 0 ] &&
>>>> [ "$MISC" -eq 0 ] && [ "$LOGS" -eq 0 ]
>>>> then
>>>> echo "got all zeros"
>>>> else
>>>> echo "got a value"
>>>> fi
>>>> Any time you find yourself using a "not" (e.g. "!" or "-ne"), really
>>>> stop and think about what you're doing as negatives are in general
>>>> harder to understand than positives, and single-negatives open the
>>>> door to future double-negatives which get much harder to understand.
>>>> Regards,
>>>> Ed.
>>> be positive!
>>> sounds indeed better than
>>> don't be negative!
>> and way better than
>> don't not be positive!- Hide quoted text -
>>
>> - Show quoted text -
>
>
> hello,
> i done with what i wanted , i used below command
> if [ \( $DATA -eq 0 \) -a \( $NEW -eq 0 \) -a \( $MISC -eq 0 \) -a \
> ( $LOG -eq 0 \) ]
> FYI.....
>
Well done, you used -a within the [] operator!
You can even omit the \( \) brackets.
--
Michael Tosch @ hp : com
Re: compair multiple variable need more help..
am 02.10.2007 09:10:47 von Jobs Circular group
On Sep 29, 8:00 am, Michael Tosch
wrote:
> Nikunj Khakhar wrote:
> > On Sep 28, 4:00 pm, Ed Morton wrote:
> >> Michael Tosch wrote:
> >>> Ed Morton wrote:
> >>>> Nikunj Khakhar wrote:
> >>>>> hello when i try to compair follwoing variable by "OR" (||) it only
> >>>>> compair first variable
> >>>> It'd be good if you showed us what values you've tried that make you
> >>>> say that and post the smallest executable script that can reproduce
> >>>> the problem so we can verify and solve it.
> >>>>> , i use ksh
> >>>>> can any body help ?
> >>>>> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
> >>>>> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
> >>>> This won't solve your problem (if you really have one), but instead of
> >>>> all those negative tests, use positives to make it clearer what you're
> >>>> doing. I.e. instead of:
> >>>> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
> >>>> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
> >>>> then
> >>>> echo "got a value"
> >>>> else
> >>>> echo "got all zeros"
> >>>> fi
> >>>> this would be clearer:
> >>>> if [ "$DATA" -eq 0 ] && [ "$DATALOG" -eq 0 ] &&
> >>>> [ "$MISC" -eq 0 ] && [ "$LOGS" -eq 0 ]
> >>>> then
> >>>> echo "got all zeros"
> >>>> else
> >>>> echo "got a value"
> >>>> fi
> >>>> Any time you find yourself using a "not" (e.g. "!" or "-ne"), really
> >>>> stop and think about what you're doing as negatives are in general
> >>>> harder to understand than positives, and single-negatives open the
> >>>> door to future double-negatives which get much harder to understand.
> >>>> Regards,
> >>>> Ed.
> >>> be positive!
> >>> sounds indeed better than
> >>> don't be negative!
> >> and way better than
> >> don't not be positive!- Hide quoted text -
>
> >> - Show quoted text -
>
> > hello,
> > i done with what i wanted , i used below command
> > if [ \( $DATA -eq 0 \) -a \( $NEW -eq 0 \) -a \( $MISC -eq 0 \) -a \
> > ( $LOG -eq 0 \) ]
> > FYI.....
>
> Well done, you used -a within the [] operator!
> You can even omit the \( \) brackets.
>
> --
> Michael Tosch @ hp : com- Hide quoted text -
>
> - Show quoted text -
hello michael,
-a i used for the AND operation and \ i used for the recognize the
value other wise it would check only first value and skip the second
value ...thanks
Nikunj khakhar @ patni.. com