1753681 Members
5779 Online
108799 Solutions
New Discussion

Re: shell script help

 
1a
Advisor

shell script help

i want to compare two values in shell script .

 

example-

ca=50

pa=45

 

if [ $ca -gt $pa ]

 

i need output greater value . how to print the vaule after the if statement.

 

 

 

15 REPLIES 15
Hakki Aydin Ucar
Honored Contributor

Re: shell script help

 if [ $ca -gt $pa ]
 then
echo $ca
fi

V. Nyga
Honored Contributor

Re: shell script help

if [ $ca -gt $pa ]
 then
echo $ca
 elif [ $pa -gt $ca ]

 then
echo $pa
 else
echo "They are equal"
fi

(Sorry Hakki) :-)

*** Say 'Thanks' with Kudos ***
Hakki Aydin Ucar
Honored Contributor

Re: shell script help

Danke ! :smileyvery-happy:

 

I missed the half part of code..

James R. Ferguson
Acclaimed Contributor

Re: shell script help

Hi:

 

Since you only want the greater value, or if they are equal, merely that value, you could use:

 

# [ ${ca} -gt ${pa} ] && echo ${ca} || echo ${pa}

Regards!

 

...JRF...

Dennis Handly
Acclaimed Contributor

Re: shell script help

If you are using a real shell you can use C style expressions:

if (( ca > pa )); then

   echo $ca

else

   echo $pa

fi

This prints the maximum of the two.

1a
Advisor

Re: shell script help

hi every one thanks for you reply

 

Dennis,

 

i user your script , it working fine . please tell how to print the greater value in one text file

1a
Advisor

Re: shell script help

Sorry dennis,

i use your script , it is working fine. please tell how to print the greater value in one text file

because i need output value through mail, so i plan to send mailx command.
Tom Maloy
Respected Contributor

Re: shell script help

Starting with Dennis' script, and noting that nothing will print if the values are equal, this might work:

 

if (( ca > pa )); then

   echo $ca

else

   echo $pa

fi   >  max.txt

 

mailx  -s  "max values"  mail_address_here  <  max.txt

Carpe diem!
Dennis Handly
Acclaimed Contributor

Re: shell script help

And if you don't plan to send much text to your message:

if (( ca > pa )); then

   echo $ca

else

   echo $pa

fi  | mailx  -s  "max values"  mail_address_here

 

>noting that nothing will print if the values are equal:

 

The above fragment always prints a value.