3

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?

3 Answers 3

2

Thanks for all the input, I ended up with the following:

NEEDED_SPACE_KB=$(echo n | apt-get install $INSTALLABLE_DEBUG_PACKAGES | grep "disk space" | awk '{ print $4, $5 }' | sed 's#GB# * 1000 MB#; s#MB# * 1000 #; s#kB##; s#B# /1000#;' | bc)
1

The first seems to be accurate.

The second one looks like it might be considering the size of all the dependencies instead of the needed dependencies.

apt-cache depends

brings up all dependencies.

Example:

If I were to install "potrace" I would only need "potrace" and "libpotrace0" but the list of dependencies from apt-cache depends also brings up "libc6" and "zlib1g" which are both already installed on my system.

1
  • no, I exclude the installed dependencies in the if expression
    – Esokrates
    Aug 2, 2014 at 10:47
1

I assume little packets are not asked for input Yes or No and are not shown with the size after first running of the script. That's why your script probably may take long time sometimes and show confusing calculation.

Anyway I wrote this script in order to get always total size in kB.

#!/bin/bash
LIST="openarena chromium-bsu"
echo n | sudo apt-get install $LIST | grep "disk space" | while IFS=, read a b; do echo $b ; done  | 
sed 's/,//g' |
while IFS=\  read a b c; do 
if [ "$b" == "kB" ]; then echo $a $b;
else
 if [ "$b" == "MB" ]; then echo -n $(($a*1024)); echo " kB" 
 fi
fi
 done

produces:

454656 kB

EDIT: Explanation for the first sentence about size: I mean that for little packages(I can't remember a threshold of size) apt-get doesn't ask fro Yes or No, so for them echo N is pointless for the next times the script is running because apt-get will install them and it will show the package size only first time of running the script, other times you run the script such little packages are already installed and you are missing their size in calculation. That is partially answers why it takes long time sometimes.

2
  • 1
    thanks for your answer, but I am afraid I do not get what you mean with the first two sentences. what little packets, what input, what confusion?
    – Esokrates
    Aug 2, 2014 at 14:13
  • sorry I didn't explain that point properly, please see in in the edit part of the answer. Aug 2, 2014 at 23:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .