pacman-* scripts: proper argument parsing

master
Pierre Neidhardt 2013-10-23 11:24:32 +02:00
parent 7fe481cbcb
commit a7bbda07de
3 changed files with 27 additions and 38 deletions

View File

@ -1,5 +1,4 @@
#!/bin/sh
[ -z "$(command -v pacman)" ] && echo "You need pacman to run this script." && exit 1
_printhelp()
{
@ -22,27 +21,21 @@ OPTION_LOCAL="-1"
while getopts ":hmqe" opt; do
case $opt in
e)
OPTION_EXPLICIT="e"
;;
OPTION_EXPLICIT="e" ;;
h)
_printhelp "$0"
exit 1
;;
exit 1 ;;
m)
OPTION_FOREIGN="m"
;;
OPTION_FOREIGN="m" ;;
q)
OPTION_LOCAL="-2"
;;
OPTION_LOCAL="-2" ;;
?)
_printhelp "$0"
exit 1
;;
exit 1 ;;
:)
echo "Missing argument."
_printhelp "$0"
exit 1
;;
exit 1 ;;
esac
done
@ -54,6 +47,8 @@ if [ $# -eq 0 ]; then
exit 1
fi
[ -z "$(command -v pacman)" ] && echo "You need pacman to run this script." && exit 1
PKGLIST="$(mktemp)"
FILE="$(mktemp)"
pacman -Qq${OPTION_FOREIGN}${OPTION_EXPLICIT} | sort > "$PKGLIST"

View File

@ -1,5 +1,4 @@
#!/bin/sh
[ -z "$(command -v pacman)" ] && echo "You need pacman to run this script." && exit 1
_printhelp()
{
@ -10,6 +9,7 @@ Display size of files in PACKAGES.
-h: Display this help.
-n: Sort by size.
EOF
}
@ -19,20 +19,16 @@ while getopts ":hn" opt; do
case $opt in
h)
_printhelp "$0"
exit 1
;;
exit 1 ;;
n)
OPTION_SORT="sort -h"
;;
OPTION_SORT="sort -h" ;;
?)
_printhelp "$0"
exit 1
;;
exit 1 ;;
:)
echo "Missing argument."
_printhelp "$0"
exit 1
;;
exit 1 ;;
esac
done
@ -44,4 +40,6 @@ if [ $# -eq 0 ]; then
exit 1
fi
[ -z "$(command -v pacman)" ] && echo "You need pacman to run this script." && exit 1
du -bch $(pacman -Qlq "$@" | grep -v ".*/$" | sort -u) 2>/dev/null | eval ${OPTION_SORT}

View File

@ -1,9 +1,4 @@
#!/bin/sh
if [ -f "${0%/*}/.pacman-init" ];then
. "${0%/*}/.pacman-init"
else
echo "Missing init file" && exit 1
fi
_printhelp()
{
@ -16,7 +11,6 @@ default.
-h: Show this help.
-n: Output is sorted by size.
-q: Uses installed packages database instead of repos database. It speeds up
queries and allows displaying size of local packages not available in
repos.
@ -34,23 +28,18 @@ while getopts ":hnq" opt; do
case $opt in
h)
_printhelp "$0"
exit 1
;;
exit 1 ;;
n)
SORT_SIZE="sort -n"
;;
SORT_SIZE="sort -n" ;;
q)
CMD="pacman -Qi"
;;
CMD="pacman -Qi" ;;
?)
_printhelp "$0"
exit 1
;;
exit 1 ;;
:)
echo "Missing argument."
_printhelp "$0"
exit 1
;;
exit 1 ;;
esac
done
@ -62,6 +51,12 @@ if [ $# -eq 0 ]; then
exit 1
fi
if [ -f "${0%/*}/.pacman-init" ];then
. "${0%/*}/.pacman-init"
else
echo "Missing init file" && exit 1
fi
## We use external variable for awk to fit current locales.
RESULT="$(eval "${CMD} $@" 2>/dev/null | awk -F "$SEP" -v filter="$pacman_size" -v pkg="$pacman_name" \
'$0 ~ pkg {pkgname=$2} $0 ~ filter {gsub(/\..*/,"") ; printf("%6s KiB %s\n", $2, pkgname)}' | eval "$SORT" | eval "$SORT_SIZE")"
@ -71,6 +66,7 @@ echo "$RESULT"
## Print total size.
echo "$RESULT" | awk '{TOTAL=$1+TOTAL} END {printf("Total: %d KiB\n",TOTAL)}'
## One line version. Approximately same performance. Note the combination of
## 'tee' and >(awk ...) to write the detailed list to standard output and to
## pass it to awk.