变量在awk命令

时间:2016-12-09 05:37:55

标签: bash awk

我有以下代码来获取值的百分比

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



}

此代码的输出是

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

为什么计算不起作用。当我直接为totalNoOfLinesInTheRepo赋值时,它会给出结果,但即使我将totalNoOfLinesInTheRepo的值赋给另一个变量并在计算中使用它(如在代码中),它也给了我上面的输出。我做错了,请帮我解决这个问题,提前谢谢

0 个答案:

没有答案