comparision script (if STATEMENT)

comparision script (if STATEMENT)

am 26.09.2007 13:49:56 von Jobs Circular group

have one more query


I have three data
DATA,MISC and LOGS


by if...else i can compair the single value like


if [$DATA = 0]
else
do it
then
don do
fi


How can i use 3 variable using single if statmen which can compair if
value is zero or not ...


with regards,
nikunj

Re: comparision script (if STATEMENT)

am 26.09.2007 18:04:55 von Bill Marcum

On Wed, 26 Sep 2007 04:49:56 -0700, Nikunj Khakhar
wrote:
>
>
> have one more query
>
>
> I have three data
> DATA,MISC and LOGS
>
>
> by if...else i can compair the single value like
>
>
> if [$DATA = 0]
> else
> do it
> then
> don do
> fi
>
>
> How can i use 3 variable using single if statmen which can compair if
> value is zero or not ...
>
>
if [ "$var1" -eq 0 ] && [ "$var2" -eq 0 ] && [ "$var3" -eq 0 ]
&& = and
|| = or



--
Culture is the habit of being pleased with the best and knowing why.

Re: comparision script (if STATEMENT)

am 26.09.2007 19:06:23 von Lew Pitcher

On Sep 26, 12:04 pm, Bill Marcum wrote:
> On Wed, 26 Sep 2007 04:49:56 -0700, Nikunj Khakhar
>
> wrote:
>
> > have one more query
>
> > I have three data
> > DATA,MISC and LOGS
>
> > by if...else i can compair the single value like
>
> > if [$DATA = 0]
> > else
> > do it
> > then
> > don do
> > fi
>
> > How can i use 3 variable using single if statmen which can compair if
> > value is zero or not ...
>
> if [ "$var1" -eq 0 ] && [ "$var2" -eq 0 ] && [ "$var3" -eq 0 ]
> && = and
> || = or

if [ "$var1" - eq 0 -a "$var2" -eq 0 -a "$var3" -eq 0 ]

-a = and
-o = or
see the test(1) manpage ("man 1 test")

;-)

--
Lew

Re: comparision script (if STATEMENT)

am 26.09.2007 19:57:15 von Lew Pitcher

On Sep 26, 1:06 pm, Lew Pitcher wrote:
> On Sep 26, 12:04 pm, Bill Marcum wrote:
>
>
>
> > On Wed, 26 Sep 2007 04:49:56 -0700, Nikunj Khakhar
>
> > wrote:
>
> > > have one more query
>
> > > I have three data
> > > DATA,MISC and LOGS
>
> > > by if...else i can compair the single value like
>
> > > if [$DATA = 0]
> > > else
> > > do it
> > > then
> > > don do
> > > fi
>
> > > How can i use 3 variable using single if statmen which can compair if
> > > value is zero or not ...
>
> > if [ "$var1" -eq 0 ] && [ "$var2" -eq 0 ] && [ "$var3" -eq 0 ]
> > && = and
> > || = or
>
> if [ "$var1" - eq 0 -a "$var2" -eq 0 -a "$var3" -eq 0 ]

Or, even trickier...

if [ "$var1$var2$var3$var4" = "0000" ]

or
case $var1$var2$var3$var4 in
0000)
echo all vars were zero
;;
*)
echo at least one var was non-zero
;;
esac

There are a thousand ways to skin a cat

Re: comparision script (if STATEMENT)

am 28.09.2007 14:54:38 von Geoff Clare

Lew Pitcher wrote:

>> > if [ "$var1" -eq 0 ] && [ "$var2" -eq 0 ] && [ "$var3" -eq 0 ]
>>
>> if [ "$var1" - eq 0 -a "$var2" -eq 0 -a "$var3" -eq 0 ]

The version using && is preferred because:

+ the -a and -o operators are part of the XSI option in POSIX/SUS (i.e. only
UNIX systems are required to support them, other POSIX systems need not)

+ in general the -a and -o operators are less robust than && and || when
variables have special values such as "!"; although I'll grant that in
this specific case their use is probably reasonably safe

+ the -a and -o operators are going to be marked obsolescent in the 2008
revision of POSIX/SUS, and removed in the one after that.

> Or, even trickier...
>
> if [ "$var1$var2$var3$var4" = "0000" ]
>
> or
> case $var1$var2$var3$var4 in
> 0000)
> echo all vars were zero
> ;;
> *)
> echo at least one var was non-zero
> ;;
> esac

These will produce the wrong output if one or more variables are
empty and the others contain four 0's between them.

--
Geoff Clare

Re: comparision script (if STATEMENT)

am 01.10.2007 19:52:14 von heiner.marxen

In article ,
Geoff Clare writes:
> Lew Pitcher wrote:
>> case $var1$var2$var3$var4 in
>> 0000)
>> echo all vars were zero
>> ;;
>> *)
>> echo at least one var was non-zero
>> ;;
>> esac
>
> These will produce the wrong output if one or more variables are
> empty and the others contain four 0's between them.

Yes, a common error. But we can correct that error:

case $var1/$var2/$var3/$var4 in 0/0/0/0) ...

If it matches, the 3 slashes in the pattern must match exactly
those three slashes between the parameters.
Now we know which "0" comes from which parameter.
--
Heiner Marxen http://www.drb.insel.de/~heiner/