SO I am making a program that tests the average REad/Write speed of the hard drive using the dd command and my code is as follows(bash):
a=1
b=1
numval=3
for i in `seq 1 3`;
do
TEST$i=$(dd if=/dev/zero of=speedtest bs=1M count=100 conv=fdatasync)
#I think that this is the problem line
done
RESULT=$(($TEST1 + $TEST2))
RESULT=$(($RESULT + $TEST3))
RESULT=$(($RESULT / $numval))
echo $RESULT > Result
The code above returns the following errors (in between the dd outputs): TEST1=: command not found TEST2=: command not found TEST3=: command not found
Please help (believe it or not) this is for a school project edit: I understand that my variable does not have a valid name. but Im wondering if there is a way to do this without this shit: "^$({$-%})$" REGEX? is there way to do it without that?
You have (at least) two problems.
TEST$i=...
is not valid bash syntax for a variable assignment. And if the first "word" in a command line is not a valid assignment, then it's treated as a command name. So bash
goes ahead and substitutes the value of $i
for $i
and the output of the dd
command for $(dd ...)
(see below), ending up with the successive "commands" TEST1=
, TEST2=
and TEST3=
. Those aren't known commands, so it complains.
In an assignment, the only characters you can put before the =
are letters, numbers and _
(unless it is an array assignment), which means that you cannot use parameter substitution to create a variable name. (But you could use an array.)
You seem to be assuming that the dd
command will output the amount of time it took, or something like that. It doesn't. In fact, it doesn't output anything on stdout
. It will output several lines on stderr
, but stderr
isn't captured with $(...)