Returning multiple values in Shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Returning multiple values in Shell
# 1  
Old 04-29-2013
Returning multiple values in Shell

Hi

I have a code as the following
Code:
#!/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
Code:
# ./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.. Smilie
# 2  
Old 04-29-2013
Have you tried it as below?

Code:
 
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"

# 3  
Old 04-29-2013
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
Code:
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
# 4  
Old 04-29-2013
Its still confusing.. Can you post your exact script please?
# 5  
Old 04-29-2013
Quote:
Originally Posted by Priya Amaresh
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
Code:
s=`echo $?`

where in $? the returned value is stored
But if in case $? has two values how to return them is my problem
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.
# 6  
Old 04-29-2013
Quote:
Originally Posted by Priya Amaresh
Hi

I have a code as the following
Code:
#!/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
Code:
# ./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.. Smilie
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:
Code:
#!/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:
Code:
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
This User Gave Thanks to Don Cragun For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl subroutine returning different values in HPUX

HI , I am running a program on hpux in perl. I am encountering a strange issue where when i print a variable in the sub which is returning it , it prints a different value but when i call it and store value in a variable it gives a different o/p. the sub is sub CheckConfigFilePattern ... (4 Replies)
Discussion started by: Jcpratap
4 Replies

2. Shell Programming and Scripting

Returning and capturing multiple return values from a function

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... (2 Replies)
Discussion started by: Priya Amaresh
2 Replies

3. Shell Programming and Scripting

Output breaking when returning multiple values

I've been trying to write a command-line function to grab a website's MX records and their ip addresses. The code below works with domains that only have one MX record: function kmx { mx=`host -t MX $1 | awk '{ print $7 }'`; ip=`host $mx | sed '/IPv6/d;/handled/d' | awk '{ print $4 }'`; ... (8 Replies)
Discussion started by: Azrael
8 Replies

4. Shell Programming and Scripting

error in shell script while returning values-- urgent issue plz help.

Hi, I have initailized a varaible EBID as typeset Long EBID=0 i am calculating value of EBID using certian formula as below: (( CURR_EBID= ($BANDINDEX << 27) | ($CURR_FREQ << 16) | ($CURR_CELLID << 4) | $CURR_SECTOR_VALUE )) return $CURR_EBID The output is as below: + (( CURR_EBID=... (6 Replies)
Discussion started by: kasanur
6 Replies

5. UNIX for Dummies Questions & Answers

For loop returning more values

Hi All, Thanks all of you for the help you provide to me. Well, I have one more problem, where I am trying to pull file system information in the loop and display the filesystem percentege. I am using following code to achive this, nut it's giving the weired output. My file system is ... (1 Reply)
Discussion started by: alok.behria
1 Replies

6. Programming

returning multiple values from a function in C

hi how can I return multiple values from a C function. I tried the following: #include <stdio.h> void foo(int id, char *first_name, char *last_name) { /* this is just an example to illustrate my problem... real code makes use of the "id" parameter. */ first_name = (char... (8 Replies)
Discussion started by: Andrewkl
8 Replies

7. Shell Programming and Scripting

awk/nawk returning decimal values?

Hi Running a specific nawk statement over a 17m lines files returns the following: /bin/nawk: not enough args in ..... input record number 1,25955e+06, file test.1 source line number 1 I'd like to report the line number (in bold above) in decimal not floating so that i can spot it out. ... (1 Reply)
Discussion started by: moutaye
1 Replies

8. Solaris

awk/nawk returning decimal values?

Hi Running a specific nawk statement over a 17m lines files returns the following: /bin/nawk: not enough args in ..... input record number 1,25955e+06, file test.1 source line number 1 I'd like to report the line number (in bold above) in decimal not floating so that i can spot it out. ... (1 Reply)
Discussion started by: moutaye
1 Replies

9. Shell Programming and Scripting

Returning values from child to parent shell

I need to send the status from child shell failure to parent shell. I would like to know how could we accomplish this. My parent.sh is as below: #!/bin/ksh set -x echo "I am in parent shell now..." child.sh ret_stat=$? echo "rest_stat=$ret_stat" echo "I am below parent shell end..." ... (4 Replies)
Discussion started by: acheepi
4 Replies

10. Shell Programming and Scripting

Returning Values (shell Script)

I have an application on Informix 4GL, and I am invoking the shell script from the program, but I need to know if the shell script work fine or not, in order to continue the process. I know that we can use $? to get the final status but this is on unix command. How can I return a value from the... (3 Replies)
Discussion started by: jennifer01
3 Replies
Login or Register to Ask a Question