Returning and capturing multiple return values from a function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Returning and capturing multiple return values from a function
# 1  
Old 04-09-2013
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
Code:
#!/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..

Last edited by vbe; 04-09-2013 at 06:43 AM.. Reason: code tags
# 2  
Old 04-09-2013
Hi
in ksh you can do in this way
Code:
#!/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:
Code:
#!/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

# 3  
Old 04-09-2013
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
Code:
#!/usr/bin/ksh 
calc() 
{ 
total=$(($1+$2)) 
echo "$total" 
diff=$(($2-$1)) 
echo "$diff" 
}
calc 5 7

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

[bash] wanted: function with a clean way for multiple return values

Hi, I have a small part of a project which is done as a bash script. bash was selected as an portability issue that works out of the box. In this script I have an exec shell-function, a wrapper around arbitrary commands. I want to have STDOUT, as an addon STDERR and the EXIT-CODE of a specified... (5 Replies)
Discussion started by: stomp
5 Replies

2. Shell Programming and Scripting

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

Capturing multiple values from user input

Hello, I need to capture multiple values from user input and do not know how to do it. I'm writing a script to delete old files, but want to give the option to keep some by asking the user. This is what my output looks like... Old files to be deleted... 1 file1 2 file2 Then this bit of... (3 Replies)
Discussion started by: jrymer
3 Replies

5. Shell Programming and Scripting

Return multiple values using for loop in perl

I am using a for loop to copy files from say DIR1 and DIR2 to DIR3.I have to check whether files are copied from DIR1 and DIR2 and print the respective message. @path=("$DIR1","$DIR2"); foreach (@path) { $rc=system("cp $_/*xml $DIR3"); if ($rc == 0) { print "Files were copied... (1 Reply)
Discussion started by: liyakathali
1 Replies

6. Shell Programming and Scripting

Need Multiple Return Values

Hi, I need to retrun multiple values function errorFileCreation { echo "Before" return -1 "Siva"; echo "Aftyer" } echo ${?} - This can be used to getting first value. how can i get second one. Advance Thanks... Shiv (3 Replies)
Discussion started by: rsivasan
3 Replies

7. Shell Programming and Scripting

Can $? return multiple values?

Hi, I have a script which does something like the below: execute_some_script.sh $arg1 $arg2 `exec-some-cmd` if then; do something else do something else fi However, during some cases, there is an error saying: line xxx: [: too many arguments at the line number which has... (5 Replies)
Discussion started by: laloo
5 Replies

8. Shell Programming and Scripting

how to capture oracle function returning 2 values in unix

i have an oracle function which returns two values, one is the error message if the function encounters anything and another one which returns a number i need to capture both and pass it on to unix shell script how to do it (2 Replies)
Discussion started by: trichyselva
2 Replies

9. Programming

Function Returning Value w/o return stmt

I am working on a C/Unix application from last 2 years which communicates with other systems using proprietary format of my client. We have a function written in C which returns integer, which is response from other system to the request message initiated by my system. This return value is then... (1 Reply)
Discussion started by: dpmore
1 Replies

10. 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
Login or Register to Ask a Question