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-30-2008, 12:03 PM   #1
sleeper0110
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Rep: Reputation: 0
print 1 to 100 in a shell script


I need to output the number 1 through 100 in a shell script.
I can't use the c style for loop because the shell (busybox shell) doesn't allow it.

Is there an easy way to do this without typing out all 100 numbers?

I want to use something like:

for i in 1 to 100
do
echo $i
done

rather than
for i in 1 2 3 4 5 ...
do
echo $i
done

Is it possible to do ranges in for loops in regular /bin/sh scripts?

Again, I cannot use c style for loops (for ((a=1; a <=100; a++)))

Thanks
 
Old 10-30-2008, 12:15 PM   #2
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Take a look at the seq command, eg:

for var in `seq 1 5`; do echo $var; done
 
Old 10-30-2008, 12:48 PM   #3
sleeper0110
LQ Newbie
 
Registered: Oct 2008
Posts: 15

Original Poster
Rep: Reputation: 0
seq does it for me.
Thanks pixellany!
 
Old 10-30-2008, 03:51 PM   #4
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 38
If your shell supports brace expansion:

Code:
printf "%d\n" {1..100}
 
Old 11-02-2008, 04:55 AM   #5
nishamathew1980
Member
 
Registered: Oct 2008
Posts: 37

Rep: Reputation: 16
This should do the trick:

for a in `seq 100`
do
echo -n "$a "
done
 
Old 11-02-2008, 04:55 AM   #6
nishamathew1980
Member
 
Registered: Oct 2008
Posts: 37

Rep: Reputation: 16
Or, you could use this:

for a in {1..100}
do
echo -n "$a "
done

Linux Archive

Last edited by nishamathew1980; 11-09-2008 at 04:48 AM.
 
  


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
how to print an apostrophe (') in a shell script using awk? skuz_ball Programming 11 03-10-2012 08:26 AM
shell script to print ldap users cmontr Programming 3 08-19-2008 01:43 PM
How to print my process id using shell script ik_nitk Programming 4 06-07-2007 12:23 AM
Trying to write a perl script that will print shell variable ohcarol Programming 2 04-16-2007 08:02 AM
command or shell script to print line range from file minil Programming 3 12-28-2005 08:05 AM

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

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