comparing multiple variable using if

comparing multiple variable using if

am 26.09.2007 16:10:36 von Jobs Circular group

hi ,

i have for variable DATA,DATALOG,MISC and LOGS
if any of the variable value is non zero than i want fire a mail

can u tell any string how to do using if ..then..else..

nikunj

Re: comparing multiple variable using if

am 26.09.2007 16:21:49 von Janis Papanagnou

On 26 Sep., 16:10, Nikunj Khakhar wrote:
> hi ,
>
> i have for variable DATA,DATALOG,MISC and LOGS
> if any of the variable value is non zero than i want fire a mail
>
> can u tell any string how to do using if ..then..else..
>
> nikunj

if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
[ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
then
mail -s "subject" address@domain fi


Janis

Re: comparing multiple variable using if

am 26.09.2007 16:50:17 von Jobs Circular group

On Sep 26, 7:21 pm, Janis wrote:
> On 26 Sep., 16:10, Nikunj Khakhar wrote:
>
> > hi ,
>
> > i have for variable DATA,DATALOG,MISC and LOGS
> > if any of the variable value is non zero than i want fire a mail
>
> > can u tell any string how to do using if ..then..else..
>
> > nikunj
>
> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
> then
> mail -s "subject" address@domain > fi
>
> Janis


How can i execute the value like

if $DATA -ne 0 than it should execute the "THIS IS NIKUNJ"
if $MISC -ne 0 than it should execute the "THIS IS JANIS"
so ON...

nikunj

Re: comparing multiple variable using if

am 26.09.2007 17:13:34 von Janis Papanagnou

Nikunj Khakhar wrote:
> On Sep 26, 7:21 pm, Janis wrote:
>
>>On 26 Sep., 16:10, Nikunj Khakhar wrote:
>>
>>
>>>hi ,
>>
>>>i have for variable DATA,DATALOG,MISC and LOGS
>>>if any of the variable value is non zero than i want fire a mail
>>
>>>can u tell any string how to do using if ..then..else..
>>
>>>nikunj
>>
>> if [ "$DATA" -ne 0 ] || [ "$DATALOG" -ne 0 ] ||
>> [ "$MISC" -ne 0 ] || [ "$LOGS" -ne 0 ]
>> then
>> mail -s "subject" address@domain >> fi
>>
>>Janis
>
>
>
> How can i execute the value like
>
> if $DATA -ne 0 than it should execute the "THIS IS NIKUNJ"
> if $MISC -ne 0 than it should execute the "THIS IS JANIS"
> so ON...
>
> nikunj
>

if [ "$DATA" -ne 0 ]
then
execute the "THIS IS NIKUNJ"
fi

if [ "$MISC" -ne 0 ]
then
execute the "THIS IS JANIS"
fi

(Take care that you've written "then" correctly in the code;
besides that the code is comparable to your above requirement.)

Janis

Re: comparing multiple variable using if

am 27.09.2007 20:06:37 von Michael Tosch

Nikunj Khakhar wrote:
> hi ,
>
> i have for variable DATA,DATALOG,MISC and LOGS
> if any of the variable value is non zero than i want fire a mail
>
> can u tell any string how to do using if ..then..else..
>
> nikunj
>

If you mean non zero *length* then it is

if [ -n "$DATA" -o -n "$DATALOG" -o -n "$DATALOG" -o -n "$MISC" ]
then
mail ...
fi

or shorter

if [ -n "$DATA$DATALOG$MISC$LOGS" ]
then
mail ...
fi

--
Michael Tosch @ hp : com

Re: comparing multiple variable using if

am 27.09.2007 20:43:53 von Cyrus Kriticos

Nikunj Khakhar wrote:
>
> i have for variable DATA,DATALOG,MISC and LOGS
> if any of the variable value is non zero than i want fire a mail
>
> can u tell any string how to do using if ..then..else..

if [ $(($DATA+$DATALOG+$MISC+$LOGS)) -ne 0 ]; then
mail ...
else
...
fi

--
Best regards | "The only way to really learn scripting is to write
Cyrus | scripts." -- Advanced Bash-Scripting Guide

Re: comparing multiple variable using if

am 28.09.2007 05:18:01 von cfajohnson

On 2007-09-26, Nikunj Khakhar wrote:
> hi ,
>
> i have for variable DATA,DATALOG,MISC and LOGS
> if any of the variable value is non zero than i want fire a mail

If you are talking about numeric non-zero (and all values are
numeric or empty):

if [ $(( !!${DATA:-0} + !!${DATALOG:-0} + !!${DATALOG:-0} + !!${MISC:-0} )) -eq 4 ]
then
echo "all non-zero"
fi

If you are talking about non-empty strings:

if [ $(( !!${#DATA} + !!${#DATALOG} + !!${#DATALOG} + !!${#MISC} )) -eq 4 ]
then
echo "all non-zero"
fi


--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence