Controlled For Loop | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


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

Controlled For Loop

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-19-2010
kelseyh kelseyh is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 16 December 2010, 5:36 AM EST
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Controlled For Loop

I have a for loop and i pass in number of times i want to loop as argument to script.


I have attached a snippet of my script. Problem is this does not work, if I manually replace the $NO_OF_LOOPS with a number in the script it works fine.

NO_OF_LOOPS=$1
for i in {0..$NO_OF_LOOPS}

Any help, i just want to control the no of loops via the argument i pass in.
Sponsored Links
    #2  
Old 03-19-2010
thillai_selvan's Avatar
thillai_selvan thillai_selvan is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 5 July 2013, 6:10 AM EDT
Location: Chennai
Posts: 190
Thanks: 0
Thanked 1 Time in 1 Post
How about this!

Code:
NO_OF_LOOPS=$1
for i in `seq 0 $NO_OF_LOOPS`
do
        echo $i
done

Sponsored Links
    #3  
Old 03-19-2010
murugaperumal's Avatar
murugaperumal murugaperumal is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 20 March 2010, 7:23 AM EDT
Location: Chennai
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts

Code:
 
NO_OF_LOOPS=$1
for i in `seq  0 $NO_OF_LOOPS`

    #4  
Old 03-19-2010
thillai_selvan's Avatar
thillai_selvan thillai_selvan is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 5 July 2013, 6:10 AM EDT
Location: Chennai
Posts: 190
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
Originally Posted by murugaperumal View Post
Code:
 
NO_OF_LOOPS=$1
for i in `seq  1 NO_OF_LOOPS`

This will not work.
Error:
seq: invalid floating point argument: _OF_LOOPS
Try `seq --help' for more information.

You need to precede $ infront of NO_OF_LOOPS

It should be like

Code:
for i in `seq  1 $NO_OF_LOOPS`

Sponsored Links
    #5  
Old 03-19-2010
murugaperumal's Avatar
murugaperumal murugaperumal is offline
Registered User
 
Join Date: Feb 2010
Last Activity: 20 March 2010, 7:23 AM EDT
Location: Chennai
Posts: 91
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by thillai_selvan View Post
This will not work.
Error:
seq: invalid floating point argument: _OF_LOOPS
Try `seq --help' for more information.

You need to precede $ infront of NO_OF_LOOPS

It should be like

Code:
for i in `seq  1 $NO_OF_LOOPS`


Now I corrected
Sponsored Links
    #6  
Old 03-19-2010
amitranjansahu's Avatar
amitranjansahu amitranjansahu is offline
Registered User
 
Join Date: Jan 2009
Last Activity: 6 February 2014, 9:06 AM EST
Location: Bangalore,INDIA
Posts: 507
Thanks: 4
Thanked 27 Times in 27 Posts
Or in bash


Code:
for ((  i = 1 ;  i <= $1;  i++  ))
do
	echo "amit $i"
done

Sponsored Links
    #7  
Old 03-19-2010
kelseyh kelseyh is offline
Registered User
 
Join Date: Nov 2009
Last Activity: 16 December 2010, 5:36 AM EST
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
I get seq: command not found
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Null Handling in Until loop. . .loop won't stop brandono66 Shell Programming and Scripting 4 11-24-2009 03:57 PM
Using variables created sequentially in a loop while still inside of the loop [bash] DeCoTwc Shell Programming and Scripting 2 06-23-2009 04:59 PM
Gnuplot shell script controlled animation darius2 Shell Programming and Scripting 7 01-10-2009 09:00 AM
IDE controlled / RAID card issue Suse 9.2 98_1LE Filesystems, Disks and Memory 1 01-15-2005 07:04 PM



All times are GMT -4. The time now is 07:42 PM.