3

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

1
  • 1
    It's not necessary to use $(()) 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 a sh script. Try putting this as your shebang (the first line of the script): #!/bin/bash or run it with bash -x instead of sh -x. Commented Dec 20, 2010 at 0:14

2 Answers 2

1

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.)

3
  • of course... was so busy getting annooyed at the rand not working at first, that i lost track of using my own arguments. sorry for wasting your time. Thank you very much!
    – user331398
    Commented Dec 19, 2010 at 22:24
  • @user331398: So now you remember why you check for a correct number of arguments in your script? :D Commented Dec 19, 2010 at 22:38
  • IF=${2:?"No input specified"} checks the 2nd arg; if it's there, the the 1st one is there too.
    – frayser
    Commented Dec 20, 2010 at 2:33
0

The rand script you are using requires a single numeric parameter. The resulting number will be between 0 and N-1. I am suspecting you're not getting a number because you have not provided this parameter.

Another possibility would be if the script could not be executed. Are you certain it is the correct directory and that the file is executable? Perhaps try using the full path while testing just to be sure.

Note: I think you wanted to use count=1 on DD, since it sounds like you wanted a single block copied. If you don't, it will continue to write blocks until the end of the input file is reached.

2
  • The script I see at the SHELLdorado web site doesn't require the parameter; it pays attention if one is given, and otherwise uses 2^64 as the default maximum value. Commented Dec 19, 2010 at 21:32
  • I am using count, cut it out from my initial post. The script is working fine as is, provides me with a large nice random number. Even if I do 'rand N' for some N it doesnt matter. Same error as before from my nested script.
    – user331398
    Commented Dec 19, 2010 at 21:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.