1751975 Members
4746 Online
108784 Solutions
New Discussion

For loop issue

 
SOLVED
Go to solution
Andres_13
Respected Contributor

For loop issue

Hi all

I´m newby in shell scripting and i have not idea about how the for loop works. I need:

touch /tmp/file 1
touch /tmp/file 2
.
.
touch /tmp/file n

Any help wiil be helpfull.

Tks and Rgds!
5 REPLIES 5
Christian Tremblay
Trusted Contributor

Re: For loop issue

for i in 1 2 3 4 5
do touch /tmp/file$i
done
Andres_13
Respected Contributor

Re: For loop issue

Thanks for your quick response Christian, but i need something more 'automatic' (something that don´t need to type the number of "i" ocurrences, because the number of files that i qill need to create is in the order of thousands).

Apreciate your help!
Peter Nikitka
Honored Contributor
Solution

Re: For loop issue

Hi,

modify the variable base and max to your needs!

typeset -i i=0 max=1000
base=/tmp/file
while [ $((i+=1)) -le max ]
do print $base$i
done | xargs touch

mfG Peter

PS: posix/ksh-Shell!
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Andres_13
Respected Contributor

Re: For loop issue

Thanks Peter that´s what i´m looking for!!!

Regards!
Dennis Handly
Acclaimed Contributor

Re: For loop issue

>but i need something more 'automatic' (something that doesn't need to type the number of "i" occurrences ...)

If you want to do something where the files already exist:
for file in $(ls file*); do

If you want to do something where the list of things is in a file:
for file in $(< filelist); do