I need help with this script

About writing shell scripts and making the most of your shell
Forum rules
Topics in this forum are automatically closed 6 months after creation.
Locked
wei2912

I need help with this script

Post 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."
Last edited by LockBot on Wed Dec 28, 2022 7:16 am, edited 2 times in total.
Reason: Topic automatically closed 6 months after creation. New replies are no longer allowed.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: I need help with this script

Post 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.
Image
wei2912

Re: I need help with this script

Post 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.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: I need help with this script

Post by xenopeek »

Okay, so does your script now work :)
Image
wei2912

Re: I need help with this script

Post 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.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: I need help with this script

Post 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.
Image
wei2912

Re: I need help with this script

Post 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.
User avatar
xenopeek
Level 25
Level 25
Posts: 29507
Joined: Wed Jul 06, 2011 3:58 am

Re: I need help with this script

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

Re: I need help with this script

Post 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 :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.
Locked

Return to “Scripts & Bash”