Page 1 of 1
I need help with this script
Posted: Mon Mar 19, 2012 8:02 am
by wei2912
I have this script which purpose is to find out the average of the writing speed of the chosen harddrive (or the root partition).
Also, i can't remove the last 3 characters or the first 55 characters of the output, which is "Timing buffered disk reads: 394 MB in 3.02 seconds = "and "/MB".
The end result should be the number. (for example 256.3)
Code: Select all
#!/bin/bash
echo "Welcome to this script!"
echo ""
echo "Firstly, you need to check which hard drive is your main hard drive."
echo "The script will now print out the /etc/fstab file, please take a look at the comments and find the partition that represents the root partition."
cat /etc/fstab
echo ""
echo "Please type in the partition."
echo "Eg. /dev/sda1"
read partition
echo ""
echo "Please type the number of tests you want to do on the hard drive."
read tests
clear
echo "Doing $tests tests on $partition."
echo "Press enter to continue."
read enter
clear
average="0"
for (( test=1; test<=$tests; test++ ))
do
echo "Test No. $test"
sudo hdparm -t $partition | tee log.tmp
#Gets the previous sentence
previous=grep -B1 pattern log.tmp
average=$(({previous:55} + $average)) #55 characters before the actual speed indication, adds the actual speed indication to the averga variable
echo ""
done
average=$(($average/$tests))
echo "Your average is $average."
Re: I need help with this script
Posted: Mon Mar 19, 2012 8:29 am
by xenopeek
Whitespace is not allowed surrounding the = character in assignments. So it should be newvalue=something.
To perform calculations, you have to put them in newvalue=$(( do your math here )) constructs.
Re: I need help with this script
Posted: Wed Mar 21, 2012 7:59 pm
by wei2912
xenopeek wrote:Whitespace is not allowed surrounding the = character in assignments. So it should be newvalue=something.
To perform calculations, you have to put them in newvalue=$(( do your math here )) constructs.
Thanks, that was useful.
Re: I need help with this script
Posted: Thu Mar 22, 2012 4:26 am
by xenopeek
Okay, so does your script now work
Re: I need help with this script
Posted: Fri Mar 23, 2012 7:10 am
by wei2912
xenopeek wrote:Okay, so does your script now work
Not yet... There's a bit more to go.
I need to find out how to do these:
1. Find out the previous line the user or a script prints.
2. Find out how to trim a line.
I mantain a to-do on top of this post.
Also, thanks for your previous help.
Re: I need help with this script
Posted: Fri Mar 23, 2012 7:38 am
by xenopeek
Okay, so that is the bit inside the for loop needing some work. How about this;
Code: Select all
#Gets the previous sentence
previous=$(grep "^ Timing buffered disk reads:" log.tmp | sed 's/^.* = //' | sed 's/ .*$//')
That command finds a line like:
Code: Select all
Timing buffered disk reads: 574 MB in 3.01 seconds = 190.88 MB/sec
in the log.tmp file, and then first removes everything before the = sign (include it and the space afterwards) and then removes everything after the final space (including it). What remains is 190.88. You can use the variable $previous now to do further calculations immediately.
Re: I need help with this script
Posted: Sun Apr 01, 2012 6:02 am
by wei2912
xenopeek wrote:Okay, so that is the bit inside the for loop needing some work. How about this;
Code: Select all
#Gets the previous sentence
previous=$(grep "^ Timing buffered disk reads:" log.tmp | sed 's/^.* = //' | sed 's/ .*$//')
That command finds a line like:
Code: Select all
Timing buffered disk reads: 574 MB in 3.01 seconds = 190.88 MB/sec
in the log.tmp file, and then first removes everything before the = sign (include it and the space afterwards) and then removes everything after the final space (including it). What remains is 190.88. You can use the variable $previous now to do further calculations immediately.
Average calculations seem to not work at all.
Re: I need help with this script
Posted: Mon Apr 02, 2012 12:49 pm
by xenopeek
Might be that summation can't handle the fractional part? Test that by printing the value of $average (and shouldn't that be $sum instead
) inside your loop, to see if it gets updated correctly with each iteration of the loop.
Re: I need help with this script
Posted: Tue Apr 17, 2012 10:37 am
by wei2912
xenopeek wrote:Might be that summation can't handle the fractional part? Test that by printing the value of $average (and shouldn't that be $sum instead
) inside your loop, to see if it gets updated correctly with each iteration of the loop.
Lol, thanks for pointing that out.
Here's my current code:
Code: Select all
#!/bin/bash
echo "Welcome to this script!"
echo ""
echo "Firstly, you need to check which hard drive is your main hard drive."
echo "The script will now print out the /etc/fstab file, please take a look at the comments and find the partition that represents the root partition."
echo "Press enter to continue."
echo ""
read enter
clear
echo ""
cat /etc/fstab
echo ""
echo "Please type in the partition."
echo "Eg. /dev/sda1"
read partition
echo ""
echo "Please type the number of tests you want to do on the hard drive."
read tests
clear
echo "Doing $tests tests on $partition."
echo "Press enter to continue."
read enter
clear
sum=0
number=0
average=0
for (( test=1; test<=$tests; test++ ))
do
echo "Test No. $test"
sudo hdparm -t $partition
#Gets the number from the previous sentence and adds it to average
number=$(grep "^ Timing buffered disk reads:" log.tmp | sed 's/^.* = //' | sed 's/ .*$//')
sum=$(($sum+$number))
echo "$sum"
echo ""
done
echo ""
average=$(($sum/$tests))
echo "Your average is $average MB/sec."
echo "Press enter to quit."
read enter
The loop terminated due to this error:
Code: Select all
./testHarddriveSpeed: line 35: 0+: syntax error: operand expected (error token is "+")
BTW testHarddriveSpeed is the name of the script.