recursion script problem

Hi Guys,,

I tried to create a recursive function in unix.
The following is the code.

#/bin/sh

function(){
n=$1;

if [ $n -eq "0" ]; then

out=1;
echo "inside if for 0";

else
out = `$n * function "$n-1"`;
echo "inside if for $n-1;
fi

echo out;
}

input=$1;
echo "$input";
function "$input";

But it is giving an error.This is the error "function()recurs.ksh[3]: 0403-057 Syntax error at line 3 : `(' is not expected."

Can you please help me in debugging it.

Thanks for ur help in advance,,
Regards,
Magesh.

hi magesh,

function without name ???

is that possible??

reg,
research3

sorry my bad..

#/bin/sh

function_recur (){
n=$1;

if [ $n -eq "0" ]; then

out=1;
echo "inside if for 0";

else
out = `function "$n-1"`;
echo "inside if for $n-1;
fi

echo out;
}

input=$1;
echo $input;
function_recur $input;
echo $?

recurs.ksh[3]: 0403-057 Syntax error at line 13 : `"' is not matched.

I have modified it but still having a problem

To keep the forums high quality for all users, please take the time to format your posts correctly.

First of all, use Code Tags when you post any code or data samples so others can easily read your code. You can easily do this by highlighting your code and then clicking on the # in the editing menu. (You can also type code tags

```text
 and 
```

by hand.)

Second, avoid adding color or different fonts and font size to your posts. Selective use of color to highlight a single word or phrase can be useful at times, but using color, in general, makes the forums harder to read, especially bright colors like red.

Third, be careful when you cut-and-paste, edit any odd characters and make sure all links are working property.

Thank You.

The UNIX and Linux Forums