the given code goes in infinite loop and does not increment variable i


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting the given code goes in infinite loop and does not increment variable i
# 1  
Old 12-25-2007
Question the given code goes in infinite loop and does not increment variable i

code is as

#!/bin/sh

i=1;
while [ $i -le 5]
do
welcome $i times;

i='expr $i+1';


done
exit 0;
# 2  
Old 12-25-2007
Quote:
Originally Posted by mrityunjay22
while [ $i -le 5]
A space between the 5 and ] might make a difference.
# 3  
Old 12-25-2007
Quote:
Originally Posted by mrityunjay22
code is as

#!/bin/sh

i=1;
while [ $i -le 5]
do
welcome $i times;

i='expr $i+1';


done
exit 0;
The while test expression should have proper spacing as well as the line that increments i. Don't need the terminating semicolons after every command. Is welcome as in "welcome $i times" a functon in your script and it would be nice to printout the value of i while executing the loop...

Code:
#!/bin/sh

i=1
while [ $i -le 5 ]
do
   welcome $i times;
   echo "value of i is...$i"
   i='expr $i + 1'
done
exit 0

# 4  
Old 12-26-2007
after doing all the above steps its not worki9ng can anyone suggest another workable way of incrementing the variable i
# 5  
Old 12-26-2007
Bug Try it

Please try to do it. I think it will increament the value of i.Smilie

#!/bin/sh

i=0
while [ $i -le 5 ]
do
echo "value of i is...$i"
i=`expr $i + 1`

done
# 6  
Old 12-26-2007
Quote:
Originally Posted by rinku
Please try to do it. I think it will increament the value of i.Smilie

#!/bin/sh

i=0
while [ $i -le 5 ]
do
echo "value of i is...$i"
i=`expr $i + 1`

done
thanks its working
# 7  
Old 12-26-2007
wrong format

HI,

There are two typos for you i think.
First,
5 ] not 5]
Second,
` not '

Code:
i=1;
while [ $i -le 5 ] 
do
echo "welcome $i times"
i=`expr $i + 1`
done

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Infinite "while" loop subshell loses current date variable

I have a simple script to log network connectivity to a set of systems. However, as expected the date appended to the log never changes because the new variable is lost when the loop starts again. Can someone clue me in on how to get around this issue? #!/bin/bash LOG=/tmp/netlog... (3 Replies)
Discussion started by: woodson2
3 Replies

2. Shell Programming and Scripting

[Solved] How to increment and add variable length numbers to a variable in a loop?

Hi All, I have a file which has hundred of records with fixed number of fields. In each record there is set of 8 characters which represent the duration of that activity. I want to sum up the duration present in all the records for a report. The problem is the duration changes per record so I... (5 Replies)
Discussion started by: danish0909
5 Replies

3. Shell Programming and Scripting

How to stop infinite loop

Im unable to stop the below infinite loop (bash script). Can someone tell me why this isnt responding to signals eg: ctrl+c (SIGINT) or ctrl+z c=0 test_loop() { c=$(($c+1)) echo "count value is : $c " sleep 1 test_loop } Im using: SunOS 5.10 PS: If run this as... (13 Replies)
Discussion started by: Arun_Linux
13 Replies

4. Shell Programming and Scripting

My for loop decides to become an infinite loop?

Hi, I was debating if I should put this in the dummies or scripts section, I apologize in advance if I chose poorly. Fairly new to Unix and BASH scripting but I thought I made it fairly well given my limited understanding. However, the output indicates that it's looping and I'm ending up with a... (5 Replies)
Discussion started by: gotreef
5 Replies

5. Homework & Coursework Questions

Help with infinite loop problem

1. The problem statement, all variables and given/known data: My problem is an infinite loop when i press any other key other then Y or y in the while loop. what i want it to do is return to the normal script outside of it if pressing N or n or keep asking the same question if its any other... (4 Replies)
Discussion started by: Ren_kun
4 Replies

6. Shell Programming and Scripting

Call a infinite loop

Hi All, I need to run an infinite loop. requirement below: function1 --> creates a file file1 function2 ---> need to call if the file creates i am running these both function via a script --> script.sh i need to run the function1 first and if the file file1 creates then need to run the... (3 Replies)
Discussion started by: satyaranjon
3 Replies

7. Shell Programming and Scripting

Infinite while loop

what is the difference between while:,while true and while false? (6 Replies)
Discussion started by: proactiveaditya
6 Replies

8. Shell Programming and Scripting

infinite while do loop problem

hi all, this is how my scrip looks like #!/bin/sh bindir='/opt/apps/script/bin' datadir='/opt/apps/script/data' dir='/opt/apps/script' while : ; do ls -1rt /opt/apps/script/data/check.txt*|tail -1 > /dev/null 2>&1 if ;then chmod +rwx $bindir/dummy2.sh ... (8 Replies)
Discussion started by: tententen
8 Replies

9. Programming

Is my code in an Infinite Loop?

Production C code compiled without the dash-g option is running, and seems to be in an infinite loop. Is there a way to tell? Is there a diagnostic tool that will report what objects or what lines of code or even what functions are being executed? Or is my best option to kill it with a dump? ... (5 Replies)
Discussion started by: marcus121
5 Replies

10. Shell Programming and Scripting

Infinite loop not looping

Hi guys, I'm having a problem getting my infinite loop to loop. It simply reads in the users choice form the menu, executes the corresponding case statement and quits instead of looping back to the main menu again. I have a feeling it might be something with my if then statements within the case... (2 Replies)
Discussion started by: hootdocta5
2 Replies
Login or Register to Ask a Question