The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Problem with recursion in subdirectories scotty_123 Shell Programming and Scripting 5 03-11-2007 05:51 AM
A Question On Recursion In Ksh marlonus999 Shell Programming and Scripting 1 01-11-2007 07:09 AM
allow recursion on dns server? xnightcrawl UNIX for Advanced & Expert Users 1 03-29-2006 10:36 AM
recursion too deep swamy455 Shell Programming and Scripting 3 07-18-2005 03:18 PM
recursion gsjf Shell Programming and Scripting 1 08-26-2002 12:22 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-28-2007
murtaza murtaza is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 31
factorial using recursive function?

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 [ $1 -gt 1 ]
then
y=`expr $1 - 1`
x=$(( $1 \* factorial $y ))
return $x
else
return 1
fi
}
echo -n "Enter number = "; read n
factorial $n
  #2 (permalink)  
Old 03-29-2007
murtaza murtaza is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 31
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 [ $1 -gt 1 ]
then
y=`expr $1 - 1`
x=$(( $1 \* factorial $y ))
return $x
else
return 1
fi
}
echo -n "Enter number = "; read n
factorial $n
  #3 (permalink)  
Old 03-29-2007
Raghuram.P Raghuram.P is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 44
HI,
I have made a small change in your code and it is working fine for me...try it...
factorial()
{
if [ $1 -gt 1 ]
then
y=`expr $1 - 1`
factorial $y
#x=$(( $1 \* factorial $y )) -- I have commented this
x=$(( $1 * $? ))
return $x
else
return 1
fi
}
echo -n "Enter number = "; read n
factorial $n
echo $?

Thanks
Raghuram
  #4 (permalink)  
Old 03-29-2007
murtaza murtaza is offline
Registered User
  
 

Join Date: Mar 2007
Posts: 31
Thank u very much for the reply. Can u tell me the concept of $?. Thanks once again for great help because I was doing effort for last one day.
  #5 (permalink)  
Old 03-29-2007
Raghuram.P Raghuram.P is offline
Registered User
  
 

Join Date: Feb 2007
Posts: 44
HI,
$? tells the return value of the last called function.
In that case it was the function call factorial().

Thanks
Raghuram
  #6 (permalink)  
Old 03-29-2007
justsam
Guest
  
 

Posts: n/a
Bits: 0 [Banking]
No support for Recusrsive functions in Shell scripting

Hello,

As far as i know, recursive functions are not supported in Shell Scripting.
Anyways, the following script will help you to get the factorial of a number.

#! /bin/sh

echo "Enter a number: "
read num

i=2
res=1

if [ $num -ge 2 ]
then
while [ $i -le $num ]
do
res=`expr $res \* $i`
i=`expr $i + 1`
done
fi

echo "Factorial of $num = $res"
  #7 (permalink)  
Old 03-29-2007
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
Please review our rules and note:
(4) Do not 'bump up' questions if they are not answered promptly. No duplicate or cross-posting and do not report a post or send a private message where your goal is to get an answer more quickly.

I have merged your two different threads. Please do not do this again.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 09:20 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0