Question

I wrote a script to fill a file for some Disk capacity testing. Could you please tell me why I have an error?

#!/bin/bash 
COUNTER=0;
FILE_SIZE_BITS=8589934592;
FILE_NAME="fill_me";
while [ $COUNTER -eq 0 ]; do
    echo "Dummy text to fill the file" >> "$FILE_NAME";
    SIZE='stat -c%s fill_me';
    if [[ $SIZE -gt $FILE_SIZE_BITS ]]; then
        let COUNTER=COUNTER+1;
    fi
done

the error is:

-bash: [[: stat -c%s fill_me: division by 0 (error token is "fill_me")

Thanks

Was it helpful?

Solution

The error is here:

SIZE='stat -c%s fill_me'

Do this instead:

SIZE=$(stat -c%s fill_me)

This sets SIZE to the output of the stat -c%s fill_me command. The $(...) syntax is the Command Substitution syntax:

Command substitution allows the output of a command to replace the command name.


BTW you can do the same thing with dd[manual]:

dd if=/dev/zero of=fill_me bs=1 count=8589934592

OTHER TIPS

As amaud576875 answered, dd is the way to go.

For fun, here's another method:

str63=123456789.123456789.123456789.123456789.123456789.123456789.123
yes $str63 | head -134217728 > fill_me

You found the programming error already, but from a shell programming point of view, the overall script is quite clunky. Here is a refactored version.

#!/bin/bash 
FILE_SIZE_BITS=8589934592;
FILE_NAME="fill_me";
while true; do
    echo "Dummy text to fill the file" >> "$FILE_NAME";
    SIZE=$(stat -c%s "$FILE_NAME")  # if you have a variable, use it everywhere
    if [[ $SIZE -gt $FILE_SIZE_BITS ]]; then
        break
    fi
done

The variable name COUNT and the arithmetic you performed on it is rather misleading, as you exit as soon as COUNT is bigger than 0. Might as well use a true/false flag in that case, but here, I don't even store the status in a variable. Instead, I break out of the loop (and thus, the entire script) when the exit condition is true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top