The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Increment date in 'for' loop? SunnyK Shell Programming and Scripting 4 10-30-2007 02:12 AM
Infinite Loop in Autosys while running a shell script, Manual run is fine sharmagaurav_2k Shell Programming and Scripting 2 09-04-2007 05:20 AM
pick the bug the server enters an infinite loop arjunjag High Level Programming 3 07-18-2007 10:53 PM
ls command in infinite Loop umakant SUN Solaris 3 07-16-2007 10:25 PM
high priority thread contains an infinite loop rvan High Level Programming 0 02-14-2007 05:30 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-24-2007
Registered User
 

Join Date: Dec 2007
Posts: 4
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;
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 12-24-2007
Registered User
 

Join Date: Jan 2007
Posts: 2,965
Quote:
Originally Posted by mrityunjay22 View Post
while [ $i -le 5]
A space between the 5 and ] might make a difference.
Reply With Quote
  #3 (permalink)  
Old 12-25-2007
Registered User
 

Join Date: Oct 2007
Location: USA
Posts: 462
Quote:
Originally Posted by mrityunjay22 View Post
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
Reply With Quote
  #4 (permalink)  
Old 12-25-2007
Registered User
 

Join Date: Dec 2007
Posts: 4
after doing all the above steps its not worki9ng can anyone suggest another workable way of incrementing the variable i
Reply With Quote
  #5 (permalink)  
Old 12-25-2007
Registered User
 

Join Date: May 2007
Posts: 49
Smile Try it

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

#!/bin/sh

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

done
Reply With Quote
  #6 (permalink)  
Old 12-25-2007
Registered User
 

Join Date: Dec 2007
Posts: 4
Quote:
Originally Posted by rinku View Post
Please try to do it. I think it will increament the value of i.

#!/bin/sh

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

done
thanks its working
Reply With Quote
  #7 (permalink)  
Old 12-25-2007
Registered User
 

Join Date: Jun 2007
Posts: 338
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
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 03:57 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0