0

I have the following code to get the percentage of values

output(){

echo location of the saved outputs from diff.sh $outputDirectory

#for echoing the no of lines changed-------------------------------------
noChanges=$(cat "$outputDirectory"/diffChanges.txt | wc -l)
echo "no of lines changed" $noChanges

#for echoing the no of lines added--------------------------------------

#no of lines directly added to the repo
noAdditions=$(cat "$outputDirectory"/diffAdditions.txt | wc -l)
echo "no of lines added" $noAdditions

#for echoing the no of lines deleted------------------------------------
noDeletions=$(cat "$outputDirectory"/diffDeletions.txt | wc -l)
echo "no of lines deleted" $noDeletions

#Counting the total no of lines in the repo-----------------------------------------------------------------

totalNoOfLinesInTheRepo=$(git -C "$repoLocation" diff --shortstat $(git -C "$repoLocation" hash-object -t tree /dev/null) | cut -d " " -f5)
echo total no of lines in the repo $totalNoOfLinesInTheRepo
noLines=$totalNoOfLinesInTheRepo
#totalNoOfLinesInTheRepo=1000
#calcutlating percentages of line changes----------------------------------------------------------------------------
changesPercentage=$(awk "BEGIN { cp=100*${noChanges}/${noLines}; i=int(cp); print (cp-i<0.5)?i:i+1 }")
dditionPercentage=$( awk "BEGIN {ap=100*${noAdditions}/${totalNoOfLinesInTheRepo};j=int(ap);print (ap-j<0.5)? j:j+1 }")
deletionPercentage=$( awk "BEGIN {dp=100*${noDeletions}/${totalNoOfLinesInTheRepo};k=int(dp);print (dp-k<0.5)? k:k+1}")


echo "percentage of lines changes"
echo percentage of changed lines $changesPercentage
echo percentage of added lines $additionPercentage
echo percentage of deleted lines $deletionPercentage



}

The out put of this code is

no of lines changed 238
no of lines added 4344
no of lines deleted 971
total no of lines in the repo 425987
percentage of lines changes
percentage of changed lines 0
percentage of added lines 1
percentage of deleted lines 0

Why the calculation is not working. When I assign value to the totalNoOfLinesInTheRepo directly it gives results, but even if I assigned the value of totalNoOfLinesInTheRepo to another variable and use it in the calculation ( as in code) it too gives me the above output. What I am doing wrong, please help me to fix this,thanks in advance

Kasun Siyambalapitiya
  • 3,956
  • 8
  • 38
  • 58
  • @Inian this is the output ` 2583 files changed, 425987 insertions(+) 425987` – Kasun Siyambalapitiya Dec 09 '16 at 06:01
  • Can't post answer because it was assigned to duplicate, but basically your script worked using test values as it was and shell substitution, but the proper way to use awk would be, for example: `changesPercentage=$(awk -v noChanges="${noChanges}" -v noLines="${noLines}" 'BEGIN { cp=100*noChanges/noLines; i=int(cp); print (cp-i<0.5)?i:i+1 }')` – hmedia1 Dec 09 '16 at 06:17
  • (ninjaed) What's the error? 238/425987 to the nearest integer percent is 0, 4344/425987 is 1, 971/425987 is 0. **Looks right to me.** Although it's _simpler_ to code round-nonnegative-to-nearest-int as just e.g. `print int(100*numerator/divisor+0.5)`. – dave_thompson_085 Dec 09 '16 at 06:17

0 Answers0