LinuxQuestions.org
Visit Jeremy's Blog.
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 10-14-2022, 06:43 AM   #1
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Rep: Reputation: Disabled
Continuous loop


Hi everyone , i am building here a script but i have a problem controling a variable in it , it looks that the variable always return to 1 when it loops .

A simple code :

Code:
cntdir="5"
function count () {

#This if statement also must be here
if [[ "$i" -gt "5" ]]
then
exit 0
fi
for i in $(seq "$cntdir")
do

#I need to have this next code to control the position of i
# this next line should add +1 to current i value , and it does but when it
# loops the code in count then changes it to 1 again
echo "i value is $i"
i=$((i+1))
echo "i value now is $i"
count
done
}
count
I need to control the variable "i" but every time it finishes the code and return it goes to 1 again instead the next number .
This is an eternal loop , and the problem is that i can not control the i variable by adding +1 to i .

Any ideas ?
 
Old 10-14-2022, 07:30 AM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
Did you make your `count` function recursive deliberately?
 
Old 10-14-2022, 07:32 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
You are calling your function from within the loop which means the sequence starts from 1 and therefore i=1 regardless.

If you want to control i use a while true loop instead of a for. It would help to know what you a trying to accomplish with the function.

Last edited by michaelk; 10-14-2022 at 07:34 AM.
 
1 members found this post helpful.
Old 10-14-2022, 09:49 AM   #4
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by NevemTeve View Post
Did you make your `count` function recursive deliberately?
yes , this is an example .

i variable starts at 1 with a sequence to 5 , but inside the code i add another 1 to the i var ,
witch means that 1+1 =2 , so i should be at 2 when it loops again in the "for" sentence , however i believe that this is not working because the loops "For" never reaches the "Done" command to start the next sequence witch is 2 , so no matter what i do , if the loop command does not finish by itself then i will be always 1 .

I made a different approach to the code here and already fixed my issue , i added additional functions to allow the loop work correctly , however if i post these changes here it will not make any sense because i need to post the full script witch already goes in 385 lines of code , because no one will understand unless they start to read the code from the beginning .

Best way is to remove this thread because definitively the "for" loop must run until the "done" to follow to the next sequence , if not reaches "done" instruction witch tells the code that first sequence is finished and can go to next sequence then it will never work .
 
Old 10-14-2022, 10:06 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Code:
for i in $(seq "$cntdir")
do
#I need to have this next code to control the position of i
# this next line should add +1 to current i value , and it does but when it
# loops the code in count then changes it to 1 again
echo "i value is $i"
i=$((i+1))
echo "i value now is $i"
count <-- starts function over again which resets i -->
done
Your correct the loop never reaches "done" because you keep starting the function over. Without extra context the example script does not make much sense but here is one way to "fix" it.

Code:
#!/bin/bash
cntdir="5"
i=1

function count {
#This if statement also must be here
if [[ "$i" -gt "5" ]]
then
  exit 0
fi

while true
do
  echo "i value is $i"
  i=$((i+1))
  echo "i value now is $i"
  count
done
}
count
 
1 members found this post helpful.
Old 10-14-2022, 10:20 AM   #6
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
@OP I think your code just makes no sense; if you explained what problem X is, maybe we could suggest something.
 
1 members found this post helpful.
Old 10-14-2022, 10:26 AM   #7
pedropt
Member
 
Registered: Aug 2014
Distribution: Devuan
Posts: 345

Original Poster
Rep: Reputation: Disabled
Thank you for the code , works perfectly .
 
  


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
LXer: Continuous Integration/Continuous Development with FOSS Tools LXer Syndicated Linux News 0 08-01-2019 05:41 AM
LXer: Continuous Delivery and Continuous Deployment: Keys to the DevOps Revolution LXer Syndicated Linux News 0 04-17-2019 06:21 PM
LXer: Zuul: Proven open-source continuous integration/continuous delivery LXer Syndicated Linux News 0 05-23-2018 08:31 PM
LXer: Perfecting DevOps Continuous Integration and Continuous Delivery with Kayenta LXer Syndicated Linux News 0 04-11-2018 12:42 AM

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

All times are GMT -5. The time now is 07:04 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