From cab40e02b995984ee66afb40736c352e3e1ef014 Mon Sep 17 00:00:00 2001 From: oxnz Date: Tue, 17 Nov 2015 19:29:22 +0800 Subject: [PATCH] improve extract function to support a short help message as well as some more formats support --- plugins/available/extract.plugin.bash | 80 +++++++++++++++++++-------- 1 file changed, 56 insertions(+), 24 deletions(-) diff --git a/plugins/available/extract.plugin.bash b/plugins/available/extract.plugin.bash index bb520451..e357814a 100644 --- a/plugins/available/extract.plugin.bash +++ b/plugins/available/extract.plugin.bash @@ -1,27 +1,59 @@ cite about-plugin about-plugin 'one command to extract them all...' -extract () { - if [ $# -ne 1 ] - then - echo "Error: No file specified." - return 1 - fi - if [ -f $1 ] ; then - case $1 in - *.tar.bz2) tar xvjf $1 ;; - *.tar.gz) tar xvzf $1 ;; - *.bz2) bunzip2 $1 ;; - *.rar) unrar x $1 ;; - *.gz) gunzip $1 ;; - *.tar) tar xvf $1 ;; - *.tbz2) tar xvjf $1 ;; - *.tgz) tar xvzf $1 ;; - *.zip) unzip $1 ;; - *.Z) uncompress $1 ;; - *.7z) 7z x $1 ;; - *) echo "'$1' cannot be extracted via extract" ;; - esac - else - echo "'$1' is not a valid file" - fi + +# extract file(s) from compressed status +extract() { + local opt + local OPTIND=1 + while getopts "hv" opt; do + case "$opt" in + h) + cat < + options: + -h show this message and exit + -v verbosely list files processed +End-Of-Usage + return + ;; + v) + local -r verbose='v' + ;; + ?) + extract -h >&2 + return 1 + ;; + esac + done + shift $((OPTIND-1)) + + [ $# -eq 0 ] && extract -h && return 1 + while [ $# -gt 0 ]; do + if [ -f "$1" ]; then + case "$1" in + *.tar.bz2|*.tbz|*.tbz2) tar "x${verbose}jf" "$1" ;; + *.tar.gz|*.tgz) tar "x${verbose}zf" "$1" ;; + *.tar.xz) xz --decompress "$1"; set -- "$@" "${1:0:-3}" ;; + *.tar.Z) uncompress "$1"; set -- "$@" "${1:0:-2}" ;; + *.bz2) bunzip2 "$1" ;; + *.deb) dpkg-deb -x${verbose} "$1" "${1:0:-4}" ;; + *.pax.gz) gunzip "$1"; set -- "$@" "${1:0:-3}" ;; + *.gz) gunzip "$1" ;; + *.pax) pax -r -f "$1" ;; + *.pkg) pkgutil --expand "$1" "${1:0:-4}" ;; + *.rar) unrar x "$1" ;; + *.rpm) rpm2cpio "$1" | cpio -idm${verbose} ;; + *.tar) tar "x${verbose}f" "$1" ;; + *.txz) mv "$1" "${1:0:-4}.tar.xz"; set -- "$@" "${1:0:-4}.tar.xz" ;; + *.xz) xz --decompress "$1" ;; + *.zip|*.war|*.jar) unzip "$1" ;; + *.Z) uncompress "$1" ;; + *.7z) 7za x "$1" ;; + *) echo "'$1' cannot be extracted via extract" >&2;; + esac + else + echo "extract: '$1' is not a valid file" >&2 + fi + shift + done }