开发者

store nested script return value in shell script

开发者 https://www.devze.com 2023-01-31 21:19 出处:网络
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'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 in开发者_如何学Pythonvokation

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消