1827294 Members
1722 Online
109717 Solutions
New Discussion

Re: for in shell

 
oiram
Regular Advisor

for in shell

Hello:

A very easy question, how can I make a for loop with a range of numbers. I mean:

for i in [0-10000]
do
...........
done
Thanks.
9 REPLIES 9
Jean-Louis Phelix
Honored Contributor

Re: for in shell

hi,

rather use while or until :

I=0
while [ $i -lt 10000 ]
do
...
I=$(($I + 1))
done

Regards.
It works for me (© Bill McNAMARA ...)
Stefan Farrelly
Honored Contributor

Re: for in shell

let x=0
while [ $x -lt 10000 ]
do

let x=$x+1
done
Im from Palmerston North, New Zealand, but somehow ended up in London...
F. X. de Montgolfier
Valued Contributor

Re: for in shell

Hi,

This answer is for ksh.
I don't know by heart how to use for without using a function, but you can do it with a while loop:


#! /usr/bin/ksh
let i=1
while [[i -le 1000 ]]
do
let i=${i}+1
echo ${i}
done

Cheers,

Fran??ois-Xavier

John Palmer
Honored Contributor

Re: for in shell

Using shell integer variables, the syntax is...

let I=0
while (( I <= 10000 ));
do
...
let I=I+1
done

Regards,
John
Ravi_8
Honored Contributor

Re: for in shell

Hi

i=0
while($i -le 10000)
do
...
i=($i + 1)
done
never give up
KapilRaj
Honored Contributor

Re: for in shell

if also can be used

A=0
if [ $A -gt 100000 ]
then
exit 0
else
.....
.....
.....
A=`echo " $A + 1 " |bc`
fi

kaps
Nothing is impossible
H.Merijn Brand (procura
Honored Contributor

Re: for in shell

If you're running a (t)csh, you can use repeat

% repeat 10000 echo help

In the (k)sh you can fake this with a function

a5:/u/usr/merijn 102 > ksh
$ repeat () {
> let i=$1
> shift
> while [ $i -gt 0 ]; do
> $@
> let i=`expr $i - 1`
> done
> }
$ repeat 3 echo help
help
help
help
$
Exit 1
a5:/u/usr/merijn 103 >
Enjoy, Have FUN! H.Merijn
Paul Sperry
Honored Contributor

Re: for in shell

i=0
while [ $i != 10000 ]
do
let i=i+1

done
Ralph Grothe
Honored Contributor

Re: for in shell

Just to increase the noise,
here another Bash feature:

$ echo $0
-bash
$ for ((i=0; i<10; i++));do printf "%3u Bash lets you do C-like loops\n" $i;don
e
0 Bash lets you do C-like loops
1 Bash lets you do C-like loops
2 Bash lets you do C-like loops
3 Bash lets you do C-like loops
4 Bash lets you do C-like loops
5 Bash lets you do C-like loops
6 Bash lets you do C-like loops
7 Bash lets you do C-like loops
8 Bash lets you do C-like loops
9 Bash lets you do C-like loops
Madness, thy name is system administration