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

View File

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

View File

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