简体   繁体   中英

bash get package size with dependencies apt

how can i get the total sum for all the packages in the loop?
I guess I must use bc but I'm clueless right now

#!/bin/bash
a=$(sudo apt install $1 -s 2>/dev/null | grep Inst | awk '{ print $2 }') 
for i in $a; do
    b=$(apt show $i 2>/dev/null | grep Installed-Size | awk '{ print $2 }')  
done

instead of using apt to extract infos about packages in scripts, you can use dpkg-query , which has a stable interface, as already mentioned in the comments.

for example you can get just the Installed-Size of the packages, one per line, an then sum the numbers with awk :

$ dpkg-query -Wf '${Installed-Size}\n' | awk '{ sum += $0 } END { print sum " KB" }'
4650121 KB

note, Installed-Size will give you:

an estimate of the total amount of disk space required to install the named package. Actual installed size may vary based on block size, file system properties, or actions taken by package maintainer scripts.

The disk space is given as the integer value of the estimated installed size in bytes, divided by 1024 and rounded up.

for more infos about dpkg-query : man dpkg-query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM