0

I am trying to write a script that will generate a random number of garbage files of random length. Ideally 1-50 files of length 100Kb-100Mb. I have written this script but does only generate 2 files. I would also like the filename to be the string "RanDoM" followed by a random number.

What am i doing wrong?

#!/bin/bash

ntimes=$(( ( RANDOM % 50 )  + 1 ))

for i in {1.. $ntimes}
do
   dd if=/dev/urandom of=$RANDOM bs=1K count=$(((RANDOM % 10000) + 1))
done
ECII
  • 10,297
  • 18
  • 80
  • 121

1 Answers1

1

The construct {1..$var} does not work because brace expansion happens before variable expansion. From the bash man page:

   The order of expansions is: brace expansion; tilde expansion, parameter
   and  variable expansion, arithmetic expansion, and command substitution
   (done in a left-to-right fashion); word splitting; and pathname  expan-
   sion.

You can get the functionality you want using an external tool like seq:

for i in $(seq 1 $ntimes); do
  dd ...
done
ghoti
  • 45,319
  • 8
  • 65
  • 104