helper function

This commit is contained in:
arzzen 2015-12-22 15:23:59 +01:00
commit 07a6571948

48
pert
View file

@ -3,19 +3,34 @@
# help text
if [ -z "$1" ] || [[ "$1" =~ [-]*(help|h) ]]; then
echo -e "\nA command line PERT calculator for quick estimates."
echo -e "Comma separated task list in the form \"1,2,12 4,5,9 2,3,6\", where whitespace separates tasks.\n"
echo -e "Usage:\n\tpert [optimistic,realistic,pessimistic]\n"
echo -e "\nComma separated task list in the form \"1,2,12 4,5,9 2,3,6\", where whitespace separates tasks."
echo -e "\nUsage:\n\tpert [optimistic,realistic,pessimistic]\n"
echo -e "Example:"
echo -e "\tpert 1,3,4"
echo -e "\tpert 10,15,20 5,7,10"
echo -e "\tpert \"1,2,3\" \"15,17,20\"\n"
exit 1
fi
# helper
function _calc
{
scale=2
echo "scale=$scale; $@" | bc -l | sed 's/^\./0./'
}
function _divider
{
divider=------------------------------
divider=" "$divider$divider$divider
width=83
printf "%$width.${width}s\n" "$divider"
}
header="\n %-12s |%11s |%10s |%12s |%9s |%7s |%9s\n"
format=" %-12s |%11s |%10s |%12s |%9s |%7s |%9s\n"
divider=------------------------------
divider=" "$divider$divider$divider
width=83
scale=2
counter=0
total_estimate=0
total_standard_deviation=0
@ -39,27 +54,26 @@ for var in "$@"; do
# header
if [[ $counter = 1 ]]; then
printf "$header" "task" "optimistic" "realistic" "pessimistic" "duration" "risk" "variance"
printf "%$width.${width}s\n" "$divider"
printf "$header" "#" "optimistic" "realistic" "pessimistic" "duration" "risk" "variance"
_divider
fi
# check values
if [ -z "$o" ] || [ -z "$r" ] || [ -z "$p" ]; then
#echo -e "\tbad input [$o,$r,$p]"
printf "$format" "$counter. bad input" $o $r $p
else
# pert estimate
pert_estimate=$(echo "scale=$scale; ($o+4*$r+$p)/6" | bc -l | sed 's/^\./0./')
total_estimate=$(echo "scale=$scale; $total_estimate + $pert_estimate" | bc -l | sed 's/^\./0./')
pert_estimate=$(_calc "($o+4*$r+$p)/6")
total_estimate=$(_calc "$total_estimate + $pert_estimate")
# standard deviation
standard_deviation=$(echo "scale=$scale; ($p-$o)/6" | bc -l | sed 's/^\./0./')
total_standard_deviation=$(echo "scale=$scale; $total_standard_deviation + $standard_deviation" | bc | sed 's/^\./0./')
standard_deviation=$(_calc "($p-$o)/6")
total_standard_deviation=$(_calc "$total_standard_deviation + $standard_deviation")
# variance
variance=$(echo "scale=$scale; $standard_deviation*$standard_deviation" | bc -l | sed 's/^\./0./')
total_variance=$(echo "scale=$scale; $total_variance + $variance" | bc | sed 's/^\./0./')
variance=$(_calc "$standard_deviation * $standard_deviation")
total_variance=$(_calc "$total_variance + $variance")
# row
printf "$format" "$counter. task" $o $r $p $pert_estimate $standard_deviation $variance
@ -70,7 +84,7 @@ done
if [[ $total_estimate > 0 ]]; then
# footer
printf "%$width.${width}s\n" "$divider"
_divider
printf "$format" "summary" "-" "-" "-" $total_estimate $total_standard_deviation $total_variance
echo -e "\nThree point estimate:"