Free tech support / small logo


Nifty shell script of the day: calculating standard deviation

If you find yourself working on the command line and need to whip out a quick statistical analysis or two, you'll be glad to know that Wicked Cool Shell Scripts fan Josh Kotecha has sent in a very nice little shell script that does just that using the scriptbc script included in the book.

Dave's Answer:

Here's his script, as written:

#!/bin/sh
# copyright(c) j. kotecha 2004 -- http://www.vidia.com/
# this file may be used without fee as long as the copyright 
# notice is kept the program is is "as is" and not warranted 
# for any purpose. Use at your own risk

M="0"
s="0"
v="0"
d="0"

if [ $# -eq 0 ]; then
  exit 1
fi

if [ $1 = "-m" ]; then
   M=$2
   shift 2
else
   M=0
fi

list=$*
count=$#

for i in $*
do
  d=`scriptbc -p 9 $i - $M`
  v=`scriptbc -p 9 $d \* $d`
  s=`scriptbc -p 9 $s + $v`
done

std="`scriptbc -p 9 $s / $count`"
std="`scriptbc -p 9 sqrt \( $std \)`"

echo $std
The script itself could be fine-tuned a bit, but the important thing to see here is that Wicked Cool Shell Scripts really does enable readers to get more out of the command line and shell scripting environment, to create scripts and commands that they otherwise wouldn't have been able to create.

Great job, Josh!









Subscribe!
Never miss another Q&A article! Click to subscribe: Add to Google Reader Add to My Yahoo! Subscribe in NewsGator RDF XML
Comments
Rather amazingly, there are no comments on this article yet.

I have something to say, now that you mention it, but ...
Starbucks coffee cup I do have a lot to say, and questions of my own for that matter, but first I'd like to say thank you for all your efforts on this Web site by buying you a cup of coffee!

I do have a comment, now that you mention it!











Remember personal info?


Please note that I will never send you any unsolicited email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Recent Entries


Search
I Need Help!
Need Help? Ask Dave Taylor!


© 2002 - 2012 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]
"Ask Dave Taylor®" is a registered trademark of Intuitive Systems, LLC.