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
The error message is from dd complaining that it can't find the file to open, which might appear as an empty string.
Given that you've indicated in a comment that this is not exactly the code that is failing, we cannot help you - the problem is not where you think it is, which is why looking for the problem in rand is not going to help.
Most likely, you have some variant of your command line such as:
dd if=$filel of=$file2 bs=4096 seek=$offset
and you actually have a typo such as l for 1 in the command line. For example:
$ dd if= of=/dev/null bs=23 count=2
dd: opening `': No such file or directory
$
You get the same message if the output file is missing. You should immediately be debugging with:
sh -x yourscript
You could also, of course, add a diagnostic print such as:
echo rand=$offset 1>&2
(or, since you've probably never been afflicted with a shell that had bugs in the notation '>&2', you can actually drop the 1 which I put there reflexively because of bad experience in years past on machines running a DOS/Windows shell emulator.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With