The following is no duplicate of How to determine the size of a package while using apt prior to downloading?, it is more an additional question.
Consider the following two implementations to get the required disk space:
#!/bin/bash
echo n | sudo apt-get install $LIST | grep "disk space" | awk '{ print $4, $5 }'
and
#!/bin/bash
PACKAGE_LIST=$(dpkg --get-selections | awk '{ print $1 }' | grep -v -e "-dbg" | cut -f1 -d":")
getsize () {
size=$(apt-cache --no-all-versions show $1 | grep Installed-Size | awk '{ print $2 }')
((NEEDED+=$size))
}
for package in $LIST; do
getsize $package
DEPENDENCIES=$(apt-cache depends $package | grep Depends | awk '{ print $2 }')
for dependency in $DEPENDENCIES; do
if [[ ! $PACKAGE_LIST =~ [^.[:alnum:]-]"$dependency"[^.[:alnum:]-] ]]; then
getsize $dependency
fi
done
done
echo "$NEEDED kb"
In theory they should return similar results, but for big packages in $LIST
they return very different results, the second implementation returns much bigger results (for example 13.5 GB vs 16.2 GB).
I know the second implementation is slower, because it tries to do the job of apt-get in bash, which is a lot slower, but my intention of writing the second version was to get the result in kb, because I was not able to find out how to get the result in kb from the first implementation, mainly because apt-get scales the unit, from bytes up to gigabytes and I do not know, whats the biggest unit it considers.
Could someone help me find out why there is such a big difference?