LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 12-15-2005, 11:49 AM   #1
WindowBreaker
Member
 
Registered: Oct 2005
Distribution: Slackware
Posts: 228

Rep: Reputation: 40
Need help with a simple script shell script


I need a simple shell script that can autoincrement numbers, starting at one (001), thru 999.
The catch: the preceding zero's are required.

Here's what I've got:

#!/bin/sh
num=001
while [ $num -lt 1000 ];do
echo $num
num=$(($num+001))
done

This results in the following output:
001
2
3
4
5
6
7. . .etc

Is there a simple way to cause it to output, and not truncate, the leading zeros???

I'm sure it's something simple, but this is driving me nuts, so any suggestions would be appreciated.

Thanks
 
Old 12-15-2005, 12:09 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391Reputation: 2391
Hi,

Do you have to do this with a script?

If so:

Use printf instead of echo

printf "%03g\n" $num

If not: take a look at seq

Examples:

$ seq 5
1
2
3
4
5
$ seq 3 5
3
4
5
$ seq -f "%03g" 3 5
003
004
005

Hope this helps.
 
Old 12-15-2005, 12:45 PM   #3
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 59
How about something like this
Code:
#!/bin/bash

for (( i=1; i<1000; i++ )) ; do
if [ $i -lt 10 ]; then
   echo 00$i
elif [ $i -ge 10 -a $i -lt 100 ]; then
   echo 0$i
else
   echo $i
fi
done
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Simple Shell Script Help Kristijan Programming 3 06-13-2005 09:13 PM
Simple shell script mikz Linux - General 1 02-24-2005 07:18 AM
simple shell script sharpie Programming 9 06-02-2004 11:47 PM
a simple shell script Warchief Programming 1 07-31-2003 05:01 AM
Very Simple Shell Script cli_man Linux - Software 5 04-24-2003 10:14 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:31 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration