Hello! Before you "bash" me with
- Not another post of this kind
Please read on and you will understand my problem...
I am using the below to extract a sum of the diskIO on a Solaris server.
Even though I am not piping to while, the final
returns zero.
The same code with Korn shell works perfectly but for some reason the execution is too slow and causes problems to the use I need it for. So I definately need to use Bash or something equally fast...
The execution is slow because you're running dozens of externals in backticks all the time. You should use externals to process big batches of data -- not individual lines. Externals are fast enough on bulk data, but overkill for anything less, like makng 10 phone calls to say 10 words. Most of your script could probably be done with one execution of awk, if we knew the details.
If you show the input you get from sar, and the output you want from the script, we can find a much more efficient way to do so, probably in nearly pure awk. It can do much more than '{ print $1 }'
I don't see anything obviously wrong with your script, though there's many places in it where things are unquoted and quoting may be important.
The script is not slow for its content.
The "sar -d 30 4" statement will take 2 minutes (sample interval of thirty seconds times the four iterations). On my test the script took 2 mins 3 secs.
The process to fish out the Average lines can be improved such that we only read $TEMP1 once.
If performance was really important, there is no need for the workfile $TEMP1 and (as Corona688 notes) the maths can be done in awk.
The other issue with your script is possibly the design. Field 5 in "sar" is the Number of Read+Write data transfers PER SECOND. Over the sample period of just 2 minutes you need some decent level of disc activity to get this figure above zero.
Finally the question I should have asked first:
Please explain the statement "execution is too slow and causes problems to the use I need it for".
(You are aware that unix systems can be configured to accumulate "sar" statistics automatically all day every day)?
Actually I altered the script to just use one AWK statement as "Corona688" suggested. It now looks like this (also corrected a bug):
I noticed afterwards that the sar command was causing most of the delay but then again I am under the impression that bash was a bit faster.
To answer "methyl"'s question, I will use this script along with net-SNMP extend (or exec if you will) functionality. The idea is to graph the accumulation of read/writes on servers.
So... Can someone propose a logical interval? The servers that will be queried I assure are very very busy
Currently I have it set as
this also helps so that my snmpwalks do not time out.
For the sake of learning.. Can someone answer how come the variable scope of the variable "CONT" is not global...?
Cope sample1: test.sh
i=0
echo " Outside loop i = $i "
while
do
i=$(( $i + 1))
echo "Inside loop i = $i "
done
echo " Out of loop i is : $i "
When run output :
Outside loop i = 0
Inside loop i = 1
Inside loop i = 2
Inside loop i = 3
Inside loop i = 4
Inside loop i = 5
Inside... (8 Replies)
I have to admit that i have not used Perl at all and this is a singular occasion where i have to patch an existing Perl script. I dearly hope i do not have to do it again for the next 15 years and therefore try to avoid having to learn the programming language in earnest.
The OS is AIX 7.1, the... (2 Replies)
Hi,
I'm a Delphi developer new to linux, new to this forums and new to BASH programming and got a new task in my work: maintaining an existing set of BASH scripts. First thing I want to do is making the code more reliable as in my opinion it's really bad written. So here's the quest:
I'm... (6 Replies)
Hi All
Is there is any way to maintain the scope of the variable in unix
Example
x=1
j=1
while
do
..
....
....
while
do
..
..
x=x+1
done
#inner most while loop ends here
done
#outer loop ends here (8 Replies)
Hi,
I use Korn Shell. Searched Forum and modified the way the file is input to the while loop, but still the variable does not seem to be retaining the final count.
while read name
do
Tmp=`echo $name | awk '{print $9 }'`
Count=`cat $Tmp | wc -l`... (6 Replies)
Hi All,
I've been researching this problem and I am pretty sure that the issue is related to the while loop and the piping. There are plenty of other threads about this issue that recommend removing the pipe and using redirection. However, I haven't been able to get it working using the ssh and... (1 Reply)
Hi,
I want to know about the variable scope in shell script.
How can we use the script argument inside the function?
fn () {
echo $1 ## I want this argument should be the main script argument and not the funtion argument.
}
also are there any local,global types in shell script?
if... (3 Replies)
Hi All,
I am new to unix shell scripting,
in the below script "num" is an input file which contains a series of numbers example :
2
3
5
8
I want to add the above all numbers and want the result finally outside the while loop. it prints the value zero instead of the actual expected... (13 Replies)
Hi,
I am stuck while developing a shell sub-routine which checks the log file for "success" or "failure". The subroutine reads the log file and checks for key word "success", if found it set the variable (found=1). It returns success or failure based on this variable.
My problem is, I can... (2 Replies)
I've been wondering if I can make mutexes much easier to use in C++ with creative use of a locking class and variable scope, but I'm not sure if things happen in the order I want. Here's pseudocode for something that could use the class:
int someclass::getvalue()
{
int retval;
... (0 Replies)