Operating System - HP-UX
1747988 Members
4660 Online
108756 Solutions
New Discussion юеВ

Re: Incremental by 1 in the for loop

 
SOLVED
Go to solution
wojtek75
Frequent Advisor

Incremental by 1 in the for loop

He,

it is easy for 3 elements:
for i in 1 2 3; do echo $i; done

How I can make it for 100 elements without listing all the numbers? Thanks in advance.
16 REPLIES 16
James R. Ferguson
Acclaimed Contributor
Solution

Re: Incremental by 1 in the for loop

Hi:

#!/usr/bin/sh
typeset -i N=1
while (( N <= 100 ))
do
echo ${N}
N=$N+1
done

Regards!

...JRF...
wojtek75
Frequent Advisor

Re: Incremental by 1 in the for loop

Thanks, any ideas with 'for' loop?
James R. Ferguson
Acclaimed Contributor

Re: Incremental by 1 in the for loop

Hi (again):

> Thanks, any ideas with 'for' loop?

No, not with the shell.

Regards!

...JRF...
Mel Burslan
Honored Contributor

Re: Incremental by 1 in the for loop

Together with agreeing JRF's "not with `for`" view, I am under the impression that you are trying to do something with a vast number of files or similar. If this is the case, for directive can work for you, if you can come up with a regex (regular expression), covering all of these files. For example:

for file in `ls -1 /home/mydir/data/IN*`
do
echo ${file}
cp ${file} /home/mydir/archive
done

if you are dealing with sequential numbering and do not want to write numbers one-by-one manually, you have no other choice than using the while construct, as 'for' directive does not work the same way in UNIX shells as it does on high level languages as BASIC.

Hope this helps
________________________________
UNIX because I majored in cryptology...
Steven Schweda
Honored Contributor

Re: Incremental by 1 in the for loop

> Thanks, any ideas with 'for' loop?

No matter how much you love your hammer,
sometimes a screwdriver is a better tool for
the job.


Perhaps you could "echo ${N}" into a
variable, and use that result in a "for"
statement. (But that wouldn't make it the
right thing to do.)
James R. Ferguson
Acclaimed Contributor

Re: Incremental by 1 in the for loop

Hi (again):

Steven makes a good point. If you want a better "hammer" you need to look elsewhere.

The 'sh-posix' manpages make it clear that a 'for' loop is the interation over a list unlike C's 'for' loop or even Perl's variations. In Perl, for example, I could write:

# perl -le 'for ($n=1;$n<=100;$n++) {print $n}'

...using the C-syntax for 'for'. However, I wouldn't do that when I could do:

# perl -le 'print for 1..100'

It's all about TIMTOWDI --- screwdrivers and hammers; but if all you have is a hammer, you have to use it as intended :-)

Regards!

...JRF...
Dennis Handly
Acclaimed Contributor

Re: Increment by 1 in the for loop

>JRF: N=$N+1

A typo, it should be: (( N = N + 1 )) or (( N += 1 ))

 

(Actually that initial assignment does work but you can't have spaces.)

Arturo Galbiati
Esteemed Contributor

Re: Incremental by 1 in the for loop

Hi,
to generate a range of
numbers for further use within a command pipe or
shell script. This can be done with some simple
sh-code:

------------------------ CUT HERE ------------------

#! /bin/sh

# range - Generate of numbers.

lo=$1
hi=$2

while [ $lo -le $hi ]
do
echo -n $lo " "
lo=`expr $lo + 1`
done

------------------------ CUT HERE ------------------

It can now be used a way like:

for i in $(range 1 100); do ; done


HTH,
Art
Frank de Vries
Respected Contributor

Re: Incremental by 1 in the for loop

To my mind it can be done.

You generate the numbers in a file with
a while loop (see above), but output >$TMPFILE

and then in your forloop you do

for i in $(cat $TIMPFILE)
do
typeset -i count
print $count
count=count+1
done
Look before you leap