I'm trying to write a simple shell script in ubuntu to dd to a random block number but for some reason I can't manage to achieve this simple task. I tried this recipe http://www.shelldorado.com/newsletter/issues/2002-3-Aug.html by calling their rand script from mine (cut out irrelevant parts)
DEV=$1
DD=dd
IF=$2
DEV_BLOCKS=4182000
BLK_SIZE=4096
# actual test
GB=$((1024*1024*1024))
for (( i = 0 ; i <= $(($GB * 2)); i++ ))
do
#echo "$i times"
offset=`./rand`
# offset=$(($offset%$DEV_BLOCKS))
$DD if=$IF of=$DEV bs=$BLK_SIZE seek=$offset count=1
done
but I always end up having my $offset variable containing a string and not the actual invokation
$ ./rand
5732148894262698848
$ ./random
dd: opening `': No such file or directory
$ sh -x random infile outfile 2>&1 | tee log.file
+ DEV=infile
+ DD=dd
+ IF=outfile
+ DEV_BLOCKS=4182000
+ BLK_SIZE=4096
+ GB=1073741824
random: 14: Syntax error: Bad for loop variable
A direct call to the rand script yields a perfectly good random number printed to the console. Can someone please help and point me as to what i'm doing wrong?
I apologize if this was asked before, I did not find a relevant post. Thank you
$(())
inside(())
. This works:for (( i = 0 ; i <= GB * 2; i++ ))
Your error message "Bad for loop variable" is because you're trying to use Bash syntax in ash
script. Try putting this as your shebang (the first line of the script):#!/bin/bash
or run it withbash -x
instead ofsh -x
.