A shell programing question
Hi guys... is there any way to make with the Shell a for or any other loop statement to make a loop like this: 01 02 03 ... 10 11 12.. That's because I need the zero before the number when the number is below 10. thanks bye --ed
i don't know anything about bash scripting but i do know perl you could do this and then issue system() calls: for (my $x = 1; $x <= 100; $x++) { # loop from 1 to 100 if ($x < 10) { # if $x is less than 10 unless ($x =~ m/0/g) { # if $x doesn't have a "0" in it $x = "0".$x; } } print $x; # print $x }
From: Linux - User
Date: Wed, 19 Sep 2001 16:49:33 -0600 To: suse-linux-e@suse.com Subject: [SLE] A shell programing question Hi guys...
is there any way to make with the Shell a for or any other loop statement to make a loop like this:
01 02 03 ... 10 11 12..
That's because I need the zero before the number when the number is below 10.
thanks
bye
--ed
-- To unsubscribe send e-mail to suse-linux-e-unsubscribe@suse.com For additional commands send e-mail to suse-linux-e-help@suse.com Also check the FAQ at http://www.suse.com/support/faq and the archives at http://lists.suse.com
On Wednesday 19 September 2001 15:49, Linux - User wrote:
Hi guys...
is there any way to make with the Shell a for or any other loop statement to make a loop like this:
01 02 03 .... 10 11 12..
That's because I need the zero before the number when the number is below 10.
thanks
bye
--ed
Ed, Perhaps something like this? It should give you some ideas, at least. N=1 until [ $N -eq 13 ]; do if [ $N -lt 10 ]; then echo 0$N else echo $N fi let N+=1 done Kevin -- Sleep is perhaps the only of life's great pleasures that need not be of short duration.
On Wednesday 19 September 2001 15:49, Linux - User wrote:
is there any way to make with the Shell a for or any other loop statement to make a loop like this:
01 02 03
Can I ask why you need to count like this? It reminded me of a story I read once... Somebody asked the local Unix guru if "sed" could be made to count. The two of them spent three hours coming up with a "sed" script. Turns out the asker just wanted to print line numbers on a file. He/she really needed the "nl" command, not "sed". So the reason I ask is because there may be a way to accomplish your overall goal, without making the shell count. -- Robert Wohlfarth rjwohlfar@galaxyinternet.net "Is not life more important than food, and the body more important than clothes?" -- Matthew 6:25b
01 02 03 ... 10 11 12..
That's because I need the zero before the number when the number is below 10.
Oh dear... (after looking at previous proposals) This is SOO simple: printf %02d $cnt (You may use `printf %4d $cnt` to make the decimal representation of cnt (at least) 4 characters long; printf is a builtin shell command.) Example: declare -i cnt=0 # declare integer counter <loop begin statement> string="`printf %02d $cnt`" # string= "00","01","02"..."09","10","11"... cnt=$cnt+1 # increment integer counter <loop end statement> Wolly -- Some operating systems are called "user friendly". Linux, however, is "expert friendly".
participants (5)
-
gabriel
-
Kevin Hochhalter
-
Linux - User
-
rjwohlfar@galaxyinternet.net
-
Wolly Wicyrek