Visit Our UNIX and Linux User Community


recursion script problem


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting recursion script problem
# 1  
Old 07-21-2009
recursion script problem

Hi Guys,,

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

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.

Last edited by vgersh99; 07-21-2009 at 09:13 AM.. Reason: code tags, PLEASE!
# 2  
Old 07-21-2009
hi magesh,

function without name ???

is that possible??

reg,
research3
# 3  
Old 07-21-2009
sorry my bad..
Code:
#/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

Last edited by vgersh99; 07-21-2009 at 09:13 AM.. Reason: code tages PLEASE!
# 4  
Old 07-21-2009
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 [code] and [/code] 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

Previous Thread | Next Thread
Test Your Knowledge in Computers #146
Difficulty: Easy
In 1983, Richard Stallman announced the GNU (short for "GNU's Not Unix") project, to create a proprietary, commercial software Unix-like system.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Recursion in a bash script

So the problem I am having is recursion with in bash. Specifically the program ideally takes either the current or provided directory, lists out everything in that directory, if it finds another directory the script should call itself and go into that directory. I managed to get it to dive into the... (6 Replies)
Discussion started by: mistsong1
6 Replies

2. Shell Programming and Scripting

script recursion

Can someone please explain me why the following script calls it self recursively: #!/bin/bash echo Called $0 while this not: #!/bin/bash echo Called $($0) Thanks (6 Replies)
Discussion started by: superpointer
6 Replies

3. Programming

C Recursion (explain)

Hi, Question: how come the output is like that? Can explain to me abit. I am learning C. Thanks! #include <stdio.h> #include <string.h> void printit(char line_of_char, int index); int main() { char line_of_char; int index = -1; strcpy(line_of_char, "This is a string."); ... (5 Replies)
Discussion started by: seede
5 Replies

4. Shell Programming and Scripting

Shell script to generate Fibonacci series using recursion

I am facing problem with Shell script to generate Fibonacci series using recursion i.e. recursive function. Here is my script: #!/bin/sh fibo() { no=$1 if ; then return 0 elif ; then return 1 else a1=`expr $no - 1` fibo $a1 ... (10 Replies)
Discussion started by: Tapas Bose
10 Replies

5. UNIX for Dummies Questions & Answers

Function Recursion Shift Problem

Hi, I'm trying to create a script that will display the contents of the users directories, but i'm confused about how to incorporate the shift properly. The problem I'm getting with my script is that it goes throught the first couple of directories but then returns an error as it loses the... (10 Replies)
Discussion started by: nuvpal
10 Replies

6. Programming

Recursion

I want to halt a tail recursive function after certain validation. I want to come out of entire recursion without unwinding phase. How can i achieve that . The coding is done in C language. (5 Replies)
Discussion started by: joshighanshyam
5 Replies

7. Shell Programming and Scripting

Help Help Help in recursion

Hello every body. I am trying to find the factorial using the following code. But it is giving the syntax error. I tried very much but in vain. Thanks in advance for helping me factorial() { if then y=`expr $1 - 1` x=$(( $1 \* factorial $y ))... (6 Replies)
Discussion started by: murtaza
6 Replies

8. Shell Programming and Scripting

Problem with recursion in subdirectories

Hello ! I need some help with my simple bash script. This script removes all files ( with name given in $1 ) in current dir and subdirectories . The problem is with first loop in the script ( for file in * ; do ) . When I run the sript in my home directory this script display sometimes( ... (5 Replies)
Discussion started by: scotty_123
5 Replies

9. Shell Programming and Scripting

recursion too deep

I am running a korn shell script which has a recursive function. The script ran for 117 iterations and ended up with the following error "recursion too deep". what should be done to avert this? Thanks in advance Swamy p.s. I am on UNIX MPRAS V4 (3 Replies)
Discussion started by: swamy455
3 Replies

10. Shell Programming and Scripting

recursion

I'm using the UNIX csh and i wish to use recursion to nav my way up (or down as it is) a given folder. My little test script is called "r" and takes a folder as argv (or $1) #!/bin/tcsh -f set allFiles = `ls -A $argv` cd $argv while ($#allFiles) if (-d... (1 Reply)
Discussion started by: gsjf
1 Replies

Featured Tech Videos