LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-30-2009, 04:57 PM   #1
VauxhallVXR
LQ Newbie
 
Registered: May 2009
Posts: 6

Rep: Reputation: 0
New to Bash | Need help with recursion.


Hey guys, new to the forum, a whole week fresh to bash.

What I'm trying to accomplish is not necessarily efficient, but humor me.

function Recursion {

if [ $v != $p ]; do

echo RUNNING
sleep 3
else
echo NOT RUNNING
sleep 3
break
fi

}

So my logic on the if condition does work, but what I basically need is a way to recurse that function until it hits the else statement and breaks.

I've tried a while loop, it will get an infinite semi-often.

Any help is appreciated, thanks much!
 
Old 05-30-2009, 05:17 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
How about an 'until' loop?
 
Old 05-30-2009, 05:47 PM   #3
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,897

Rep: Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019Reputation: 5019
I don't think a recursive routine is the best choice for this, a normal iterative loop would probably be a much better choice, but if you absolutely must do it recursively, then something like this is how you'd go about it.

The following example assumes the idea is to keep recursing until something is NOT RUNNING ( when v = p )

Code:
function Recursion {

#  a command that changes one or both of $v or $p is required here
#  otherwise it won't work.

if [ $v != $p ] ; then
  echo RUNNING
  sleep 3
  Recursion
else
  echo NOT RUNNING
fi

return 0

}
 
  


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
using recursion in bash script drkstr Linux - Software 4 07-09-2006 08:48 PM
Bash Local Variable Recursion With Array jshivers Programming 0 06-16-2006 04:31 PM
Recursion in C hubabuba Programming 12 10-03-2005 07:46 AM
tar: '--no-recursion' option doesn't prevent recursion Earl Parker II Slackware 12 08-17-2004 02:49 AM
Writing bash script with recursion.. ray5_83 Programming 4 08-04-2004 05:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:48 AM.

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