I am using ksh. I want to know how can we make any function to return string or double value.
I dont want to use the global variables.
You cannot return a string from a ksh function. This is one way to do what you want.
#!/bin/ksh
myfunc()
{
echo "$1 is the first parameter"
}
result=$(myfunc "one" "two" "three")
Thanks..
but it works for simple programs where there is only one echo. it wont work if i want to echo some other string..
For eg :
myfunc()
{
echo "Inside myfunc()"
# Do all the calc....
echo "$1 is the first parameter"
}
result=$(myfunc "one" "two" "three")
And do you have any idea on how to return double values...
Thanks in adv..
Next time use CODE tags when posting code, data or logs to enhance readability and to preserve formatting like indention etc., ty.
Do you mean like this?
myfunc()
{
# Reverse the parameters
echo "$3 $2 $1"
}
result=$(myfunc "one" "two" "three")
#
echo "Result is: ${result}"
Result is: three two one
Lets c a simple eg:
function1()
{
echo "Hi there"
str="This is the string"
echo $str
}
now if I do ::
result=$(function1)
echo "Result is: ${result}"
It shows::
Result is: Hi there
This is the string
I need the output ::
Result is: This is the string
One more question..
What the procedure u all use to return a double value from a function.
Thanks
Why you have echo "Hi there" if you don't need it ?
If you like debug echo then print it to the stderr
echo "Hi there" >&2
If you like to make function which return value, don't put anything else to the stdout.
Double ?
echo ...
or
printf
...
to the stdout
are method always. Commandline stdout to the variable. Function works like commands if we look calling line.