I need help with this script

Forum rules
Before you post please read this

I need help with this script

Postby wei2912 on Mon Mar 19, 2012 8:02 am

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."
Last edited by wei2912 on Wed Mar 21, 2012 8:00 pm, edited 1 time in total.
wei2912
Level 2
Level 2
 
Posts: 79
Joined: Thu Jun 02, 2011 1:00 am

Linux Mint is funded by ads and donations.
 

Re: I need help with this script

Postby xenopeek on Mon Mar 19, 2012 8:29 am

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.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14996
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: I need help with this script

Postby wei2912 on Wed Mar 21, 2012 7:59 pm

Vincent Vermeulen 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.
wei2912
Level 2
Level 2
 
Posts: 79
Joined: Thu Jun 02, 2011 1:00 am

Re: I need help with this script

Postby xenopeek on Thu Mar 22, 2012 4:26 am

Okay, so does your script now work :)
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14996
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: I need help with this script

Postby wei2912 on Fri Mar 23, 2012 7:10 am

Vincent Vermeulen 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.
wei2912
Level 2
Level 2
 
Posts: 79
Joined: Thu Jun 02, 2011 1:00 am

Re: I need help with this script

Postby xenopeek on Fri Mar 23, 2012 7:38 am

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.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14996
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: I need help with this script

Postby wei2912 on Sun Apr 01, 2012 6:02 am

Vincent Vermeulen 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.
wei2912
Level 2
Level 2
 
Posts: 79
Joined: Thu Jun 02, 2011 1:00 am

Re: I need help with this script

Postby xenopeek on Mon Apr 02, 2012 12:49 pm

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 :wink:) inside your loop, to see if it gets updated correctly with each iteration of the loop.
User avatar
xenopeek
Level 21
Level 21
 
Posts: 14996
Joined: Wed Jul 06, 2011 3:58 am
Location: The Netherlands

Re: I need help with this script

Postby wei2912 on Tue Apr 17, 2012 10:37 am

Vincent Vermeulen 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 :wink:) inside your loop, to see if it gets updated correctly with each iteration of the loop.


Lol, thanks for pointing that out. :D

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.
wei2912
Level 2
Level 2
 
Posts: 79
Joined: Thu Jun 02, 2011 1:00 am

Linux Mint is funded by ads and donations.
 

Return to Scripts & Bash

Who is online

Users browsing this forum: No registered users and 4 guests