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
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
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
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.
I have a code as the following
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
Please help..
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:
The output produced by this script is:
Hope this helps,
Don
This User Gave Thanks to Don Cragun For This Post:
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)