refactor 'whatprovides' script

- Properly handle arguments.
- Remove functions where not necessary.
- Update help info.
This commit is contained in:
Leonid Pliushch 2020-11-19 15:18:16 +02:00
parent b6d1503aa6
commit 8ad9e1cbf9
No known key found for this signature in database
GPG Key ID: 45F2964132545795
1 changed files with 22 additions and 29 deletions

View File

@ -1,40 +1,33 @@
#!/bin/bash #!/bin/bash
SCRIPTNAME=whatprovides set -e
TWSRC="$PREFIX/opt/whatprovides/db"
package () {
cat "$TWSRC/$@"
}
finder () { SCRIPT_NAME=$(basename "$(realpath "$0")")
abs=`realpath $@` DB_PATH="/data/data/com.termux/files/usr/share/whatprovides/db"
grep -rwh $abs $TWSRC
}
show_usage () { show_usage () {
echo "Usage: $SCRIPTNAME [options] filepath" echo
echo "$SCRIPTNAME is a whatprovides of termux." echo "Usage: $SCRIPT_NAME [options] path/to/file"
echo " " echo
echo "-p [Package] list files of package." echo "Find out packages using specific files."
echo "-h show this help" echo
echo "Options:"
echo
echo " -h Show this help."
echo " -p [package] List files of package."
echo
} }
while getopts :hp:f: option; do while getopts :hp:f: option; do
case $option in case "$option" in
p) p) cat "${DB_PATH}/${OPTARG}"; exit 0;;
pkg=$OPTARG ?|h) show_usage; exit 0;;
package $pkg *) show_usage; exit 1;;
;;
h) show_usage;;
?) show_usage
exit ;;
esac esac
done done
if [[ $@ == "" ]] if [ $# -ge 1 ]; then
then grep -rwh "$(realpath "$1")" "$DB_PATH"
else
show_usage show_usage
elif [[ ! $@ =~ ^\-.+ ]] exit 1
then
finder $@
fi fi