LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-04-2010, 09:19 AM   #1
SuchANewb
LQ Newbie
 
Registered: Nov 2010
Posts: 7

Rep: Reputation: 0
Bash + Recursive Method


I have made a program which reads the input number and then sums it up. Example: If you write 5 then the program will sum up -> 1+2+3+4+5=15

But apparently I need to solve this program so it will solve this mathematical problem recursively and the function summ should call itself.

Any suggestions?

Code:
#!/bin/bash
GLOBAL=$1
SUM=0
COUNTER=1

function summ {

if [ -z $GLOBAL ] ; then
	echo "Insert number!"
	exit 1
fi

while [ $SUM -le $GLOBAL ]; do
	let SUM=SUM+COUNTER
	let COUNTER=COUNTER+1
done
echo $SUM
}

summ

Last edited by SuchANewb; 11-04-2010 at 10:28 AM.
 
Old 11-04-2010, 09:51 AM   #2
alli_yas
Member
 
Registered: Apr 2010
Location: Johannesburg
Distribution: Fedora 14, RHEL 5.5, CentOS 5.5, Ubuntu 10.04
Posts: 559

Rep: Reputation: 92
Hi

This sounds like home work; but anyway:

Read this which explains the technique of bash recursion and calling a function within itself.

Also what would make the solution even better is this - which is a mathematical formula that adds numbers from 1..n
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with recursive bash script me4linux Programming 6 03-20-2007 06:46 AM
Insert recursive method in an AVL trees. HOW???? carspidey Programming 1 03-08-2006 08:13 PM
Help with a recursive chmod script in bash lowpro2k3 Programming 11 07-25-2005 08:03 PM
Recursive search in bash scripting ! zulfilee Linux - Software 3 12-12-2004 11:40 PM
modify bash script - recursive action xscousr Programming 6 09-17-2003 02:52 PM


All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration