Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Return Code 3

Status
Not open for further replies.

vuakhobo

Technical User
Apr 22, 2004
41
US
In Perl I can do statement below.

$ValueA=Sum(3,4);
print "Value:$Value";

Is there away that I can do just like statement above in shell. I know return and $? in shell but is there an easy way to do it?
 
If you use a ksh-like shell then in the man page pay attention to the let special command and the $((expr)) substitution.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
In ksh...
Code:
((VALUEA=3+4))
print "Value:${VALUEA}"
Hope this helps.
 
Hi

I think the question was not how to evaluate 3+4, but how to pass the result to the calling expression, if the evaluation was done by a function.

In shell scripts the return value is an exit status, not the result. The result is usually printed to standard output, then catched by the calling expression.
Code:
Value=[red]`[/red]Sum 3 4[red]`[/red]
[gray]# or[/gray]
Value=[red]$([/red]Sum 3 4[red])[/red]

echo "Value:$Value";
This is called Command Substitution in shell man pages, for example [tt]bash[/tt].

Feherke.
 
Thank you for fast respond PHV and Feherke

Feherke I've tried your solution but it didn't work.
This is my example code with $?

#!/usr/bin/ksh
sum ( ) {
total=`expr $1 + $2`
return $total
}

sum 3 4
ValueA=$?
echo "ValueA:$ValueA"

instead of using ValueA=$? to get total from the function
Is there anyother way to get total from function like in perl asign a value when call the function
 
#!/usr/bin/ksh
sum(){
expr $1 + $2
}
ValueA=$(sum 3 4)
echo "ValueA:$ValueA"

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ181-2886
 
Wonderful. Thank PHV .. it works !!!! yeah
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top