LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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, 08: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 09:28 AM.
 
Old 11-04-2010, 08: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



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 05:46 AM
Insert recursive method in an AVL trees. HOW???? carspidey Programming 1 03-08-2006 07:13 PM
Help with a recursive chmod script in bash lowpro2k3 Programming 11 07-25-2005 07:03 PM
Recursive search in bash scripting ! zulfilee Linux - Software 3 12-12-2004 10:40 PM
modify bash script - recursive action xscousr Programming 6 09-17-2003 01:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:59 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
Open Source Consulting | Domain Registration