Hi
I am pretty confused in returning and capturing multiple values
i have defined a function which should return values "total, difference"
i have used as
#!/usr/bin/ksh
calc()
{
total=$1+$2
echo "$total"
diff=$2-$1
echo "$diff"
}
I have invoked this function as
calc 5 8
Now i need the value of $total and $diff after the function is being invoked..
Please help..
Hi
in ksh you can do in this way
#!/bin/ksh
calc()
{
A=$1
B=$2
total=$(( A + B ))
diff=$(( A - B ))
echo "$total $diff"
}
RES=$( calc 5 8 )
TOT=$( echo $RES | cut -f 1 )
DIF=$( echo $RES | cut -f 2 )
echo $TOT
echo $DIF
But with bash is shorter:
#!/bin/bash
calc()
{
A=$1
B=$2
total=$(( A + B ))
diff=$(( A - B ))
echo "$total $diff"
}
read TOT DIF < <(calc 5 8)
echo $TOT
echo $DIF
Just $1+$2 will not do the arithmetic operation in UNIX. You need to have as highlighted below and i believe you include the call to the function at the end of the function calc as
#!/usr/bin/ksh
calc()
{
total=$(($1+$2))
echo "$total"
diff=$(($2-$1))
echo "$diff"
}
calc 5 7