views:

130

answers:

4

supposed to be I have 3 filenames

file-00 file-01 file-02

I'm trying to replicate those files to file-03, file-04, file-05...file-98, file-99, file-100 based on the original files.

So in bash I do

#!/bin/bash

x=0
y=1
z=2

for i in $(seq 0 100);
  do
    cp file-00 file-$((x=x+2));
    cp file-01 file-$((y=y+3));
    cp file-02 file=$((z=z+4));
  done

but this doesn't work and it gives me the following output. which I only need a sequence of 100 numbers.

x=2
y=3
z=4
x=4
y=6
z=8
x=6
y=9
z=12
x=8
y=12
z=16
x=10
y=15
z=20
x=12
y=18
z=24
x=14
y=21
z=28
x=16
y=24
z=32
x=18
y=27
z=36
x=20
y=30
z=40
x=22
y=33
z=44
x=24
y=36
z=48
x=26
y=39
z=52
x=28
y=42
z=56
x=30
y=45
z=60
x=32
y=48
z=64
x=34
y=51
z=68
x=36
y=54
z=72
x=38
y=57
z=76
x=40
y=60
z=80
x=42
y=63
z=84
x=44
y=66
z=88
x=46
y=69
z=92
x=48
y=72
z=96
x=50
y=75
z=100
x=52
y=78
z=104
x=54
y=81
z=108
x=56
y=84
z=112
x=58
y=87
z=116
x=60
y=90
z=120
x=62
y=93
z=124
x=64
y=96
z=128
x=66
y=99
z=132
x=68
y=102
z=136
x=70
y=105
z=140
x=72
y=108
z=144
x=74
y=111
z=148
x=76
y=114
z=152
x=78
y=117
z=156
x=80
y=120
z=160
x=82
y=123
z=164
x=84
y=126
z=168
x=86
y=129
z=172
x=88
y=132
z=176
x=90
y=135
z=180
x=92
y=138
z=184
x=94
y=141
z=188
x=96
y=144
z=192
x=98
y=147
z=196
x=100
y=150
z=200
x=102
y=153
z=204
x=104
y=156
z=208
x=106
y=159
z=212
x=108
y=162
z=216
x=110
y=165
z=220
x=112
y=168
z=224
x=114
y=171
z=228
x=116
y=174
z=232
x=118
y=177
z=236
x=120
y=180
z=240
x=122
y=183
z=244
x=124
y=186
z=248
x=126
y=189
z=252
x=128
y=192
z=256
x=130
y=195
z=260
x=132
y=198
z=264
x=134
y=201
z=268
x=136
y=204
z=272
x=138
y=207
z=276
x=140
y=210
z=280
x=142
y=213
z=284
x=144
y=216
z=288
x=146
y=219
z=292
x=148
y=222
z=296
x=150
y=225
z=300
x=152
y=228
z=304
x=154
y=231
z=308
x=156
y=234
z=312
x=158
y=237
z=316
x=160
y=240
z=320
x=162
y=243
z=324
x=164
y=246
z=328
x=166
y=249
z=332
x=168
y=252
z=336
x=170
y=255
z=340
x=172
y=258
z=344
x=174
y=261
z=348
x=176
y=264
z=352
x=178
y=267
z=356
x=180
y=270
z=360
x=182
y=273
z=364
x=184
y=276
z=368
x=186
y=279
z=372
x=188
y=282
z=376
x=190
y=285
z=380
x=192
y=288
z=384
x=194
y=291
z=388
x=196
y=294
z=392
x=198
y=297
z=396
x=200
y=300
z=400
x=202
y=303
z=404
A: 

You should be adding 3 to each of x, y and z. They start different and this would keep them different. And you only need to loop about 32 times, not 101.

Pascal Cuoq
+1  A: 

Try this instead:

for i in $(seq 2 3 100);
  do
    cp file-00 file-$i;
    cp file-01 file-$(($i + 1));
    cp file-02 file=$(($i + 2));
  done

Note that the above snippet uses a different set of command line arguments to seq:

seq start increment end

Since each time through the loop you are handling 3 numbers at once, the counter can safely be incremented by 3 each time through.

Adam Batkin
You don't need to use `seq` - `for ((i=2; i<=0; i+=3))`
Dennis Williamson
As with most things, TMTOWTDI
Adam Batkin
And some are better than others.
Dennis Williamson
I don't see how either way is better than the other, just different. I was intentionally trying to keep my answer as similar to the original code as possible. I think that people learn best by small incremental changes.
Adam Batkin
A: 

A Bash solution:

for ((x=0; x<100; x++)); do
  cp "file-$(printf %02d $((x%3+1)))" "file-$(printf %03d $((x+1)))"
done

And yes, you know where it comes from ;-)

TheBonsai
You no good excuse for a bash programmer! What are you THINKING!?! Obviously you want to start with x=3, and since he starts with file-00 you don't want to add '1' to 'x%3'. Oh and to top it off you should only pad a single zero so printf '%02d'. P.S. I say this all in just, TheBonsai and I go way back =)
SiegeX
So where does it come from?
Craig Trader
/me bows his head in shame and starts crying. Clearly you are right. The code is flexible, it can easily be altered.
TheBonsai
Craig: irc.freenode.net, #bash
TheBonsai
+3  A: 

Bash 2.x/3.x

for ((x=3; x<100; x++)); do
   cp "file-0$((x%3))" "file-$(printf %02d $x)";
done

Bash 4.x

for x in {03..99}; do
   cp "file-0$((10#$x%3))" "file-$x";
done
SiegeX