2

So here is my code for Unix shell scripting

#! /bin/ksh

echo "Enter your first number:"    
read first

echo "Enter your second number:"    
read second

echo "Enter your third number:"    
read third

SUM=`expr $first + $second + $third`    
AVERAGE="$SUM/3"

echo "The average is $AVERAGE"

Basically I want to find the average of the 3 values that a user enters. The result I get once I enter all of the values, for example all values add up to 12; is "The average is 12/3".

slm
  • 369,824
den
  • 31

3 Answers3

5

slm's answer here hasn't taken into account that you asked about the Korn shell, not about the Bourne Again shell. The (93) Korn shell has no built-in expr command, so when using expr in Korn shell scripts you are using an external expr command. This is not a problem per se. After all, it's how one did things with the Bourne shell, which also had no expr built-in command. But as M. Kohen points out, one might prefer to use shell built-ins when a shell has them. And the Korn shell has.

M. Kohen points to the arithmetic substitution available in the Korn shell. It's important to remember that this is a substitution, so you must do something with the substituted result if you don't want to just run it as a command. The more complete form of M. Kohen's answer (fixing the operator precedence error along the way) is thus:

AVERAGE="$(( (first + second + third) / 3))"

But there are two other ways of doing this in the Korn shell. The Korn shell has a built-in command named let that does arithmetic evaluation on each of its arguments:

let "AVERAGE = (first + second + third) / 3"

Every argument to the command is a single expression, so whitespace needs to be quoted as here.

It also has a piece of syntax that is described in one ksh clone's manual as "syntactic sugar for let" with the expression turned into a single argument to that command:

(( AVERAGE = (first + second + third) / 3 ))

Further reading

JdeBP
  • 68,745
  • Thanks for the correction, I've added the tag and amended my A so that it's clear that it pertains to Bash and not Korn. If you see something egregiously wrong like this feel free to do a suggested edit on the Q to get it retagged and to leave the answered a comment alerting them to this. I only found this by accident. – slm Oct 06 '14 at 00:29
3

You can use the built-in arithmetic support in the shell that is used by a double parenthesis preceded by a dollar sign:

$(( (first + second + third) / 3))

This is more efficient than expr since it does not require a separate process (and hence fork, exec, pipe management and return).

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Didi Kohen
  • 1,841
2

Your question originally did not have the Korn Shell tag so I've since added it. If you were using Bash the following would apply.


The command expr can perform a variety of mathematical calculations. Simply make use of it to perform the averaging as well.

$ AVG=$(expr $SUM / 3)

Also you'll typically want to make use of this form of sub commands, $(...cmd...), instead of backticks. So change your SUM= line to this instead:

SUM=$(expr $first + $second + $third)

If you're unsure what a particular command can do for you be sure to check out the man pages. The command expr has all the operations that it's able to carry out, man expr.

References

slm
  • 369,824