1827474 Members
2043 Online
109965 Solutions
New Discussion

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.

James R. Ferguson
Acclaimed Contributor

Re: shell script help

Hi:

 

No matter which script you like ( and we'll use Dennis's since you reference his ), consider learning a Unix philosophy that says do-one-thing-and-do-it-well.

 

The whole script could be:

 

# cat mymax

#/usr/bin/sh

typeset ca=$1

typeset pa=$2

if (( ca > pa )); then

   echo $ca

else

   echo $pa

fi

 

...which then could be run to output to your terminal:

 

# ./mymax 100 99

100

 

...or to a file:

 

# ./mymax 100 99 > myoutput

 

...or to email:

 

# ./mymax 100 99 | mailx -s "Max Value" mymailaddress

 

The point is to allow the user to determine where he/she wants the output to appear rather than hard-coding that assumption into your script.

 

Regards!

 

...JRF...

1a
Advisor

Re: shell script help

Hi dennis,

 

if the values are equal , how to print the value.

 

 

1a
Advisor

Re: shell script help

hi james R.

thanks for your reply. i want to learn shell script .please send any pdf document or ppt
Dennis Handly
Acclaimed Contributor

Re: shell script help

>if the values are equal, how to print the value.

 

Is this a new question?

Otherwise my script fragment will print the maximum of the two and always prints, even if equal.

If you only want to print if they are equal:

if (( ca == pa )); then

   echo $ca

fi

 

James R. Ferguson
Acclaimed Contributor

Re: shell script help


@1a wrote:

thanks for your reply. i want to learn shell script .please send any pdf document or ppt

I used to recommend the "HP-UX Shell User's Guide" which was a nice overview of the various shells.  With the advent of the "improved" (NOT!) HP websites, this doesn't appear to be available anymore.

 

Regardless, there are any number of good books on shell scripting.  Find one for beginners and begin.  I urge you to confine your initial learning to the Korn ('ksh') or Bash ('bash') shell rather than the dysfunctional C shell.  In HP-UX, the default shell is simply called 'sh' and is a Korn variant.  Bash can also be installed on HP-UX though it is usually the standard for most Linux distributions.

 

Part of learning to write code (in any language) is *writing* and *reading*.  A very good collection of shell scripts is available here:

 

http://www.shelldorado.com/

 

Regards!

 

...JRF...

 

Stephan.
Honored Contributor

Re: shell script help

I only have a pretty old copy of the shell user's guide but it's still a pretty good one.

 

Find it attached.

 

hth,

Stephan