Returning multiple values in Shell

Hi

I have a code as the following

#!/usr/bin/ksh
set -x
row()
{
a=$1
b=$2
c=$(($a + $b))
d=$(($a * $b))
echo $a $b
}
e=`row 2 3`
set $e
echo "The value of c is $c"
echo "The value of d is $d"

My requirement is I need to pass two arguments to a function and return two values calculated from that function..

When i tried to execute the same
i got the output as

# ./testg.sh
+ + row 2 3
e=2 3
+ set 2 3
+ echo The value of c is
The value of c is
+ echo The value of d is
The value of d is

Please help.. :eek:

Have you tried it as below?

 
function row
{
a=$1
b=$2
c=$(($a + $b))
d=$(($a * $b))
echo $a $b
}
row 2 3
echo "The value of c is $c"
echo "The value of d is $d"

I think i was not clear on my specification
I have two shell scripts.
The two output values returned by one shell script should be passed as arguments to another shell script
And I have used functions that returns one value which can be captured by using

s=`echo $?`

where in $? the returned value is stored
But if in case $? has two values how to return them is my problem

One way is I can use the intermediate file divert the outputs and grep for the required values and them into another script..

Is there any other direct way that returns multiple values from a shell function

Its still confusing.. Can you post your exact script please?

You will have to print the numbers and capture them with backticks or the like, since $? cannot return multiple values.

$? should not be used to return data in any case, it's for error codes.

Hi Priya,
I think everyone is missing the point of your problem. Although they are correct in pointing out that the exit status of a function call is not the appropriate way to return multiple values computed by a shell function. When you later said that you had two shell scripts, what you wanted became more confusing because you didn't show us two shell scripts; you showed us one shell script that included a shell function.

When you call the function row, a, b, c, and d are set in the current shell execution environment. When you call that function in a command substitution (i.e., `row 2 3` or $(row 2 3) ), you create a new shell execution environment and when you get back to the original shell execution environment (after assigning a value to the variable e , those settings have been lost. If you remove the echo from your function and call the function without using command substitution, I think you'll get what you want.

I modified your function to return non-zero (failure) if either of the operands you pass to it is zero and to return exit status 0 (success) if both operands are non-zero to demonstrate how the exit status can be used directly in an if statement or by calling the function and then using $? in the next statement to determine whether the function succeeded or failed. Try the following as a replacement for your script:

#!/usr/bin/ksh
row() {
        if [ "$1" -eq 0 ] || [ "$2" -eq 0 ]
        then    return 1
        fi
        a=$1
        b=$2
        c=$(($a + $b))
        d=$(($a * $b))
        # If we get to here, the exit status will be the exit status of the last
        # command above.  As long as the assignment to d succeeded, the exit
        # status of this function will be zero.  You could also explicitly put a
        # return 0 here, but that should not be needed in this case.
}
if row 11 0
then    echo 'row 11 0 succeeded'
        echo "The value of a is $a"
        echo "The value of b is $b"
        echo "The value of c is $c"
        echo "The value of d is $d"
else    echo 'row 11 0 failed'
fi
row 4 5
if [ $? -eq 0 ]
then    echo 'row 4 5 succeeded'
        echo "The value of a is $a"
        echo "The value of b is $b"
        echo "The value of c is $c"
        echo "The value of d is $d"
else    echo 'row 4 5 failed'
fi

The output produced by this script is:

row 11 0 failed
row 4 5 succeeded
The value of a is 4
The value of b is 5
The value of c is 9
The value of d is 20

Hope this helps,
Don

1 Like