From 1671df94f63ba6565fe1e87499b5c0f523488598 Mon Sep 17 00:00:00 2001 From: John D Pell Date: Thu, 17 Feb 2022 22:02:35 -0800 Subject: [PATCH] plugin/extract: `shellcheck` --- clean_files.txt | 1 + plugins/available/extract.plugin.bash | 29 ++++++++++++++++----------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/clean_files.txt b/clean_files.txt index 54180c19fd..6a0f8ed957 100644 --- a/clean_files.txt +++ b/clean_files.txt @@ -98,6 +98,7 @@ plugins/available/colors.plugin.bash plugins/available/direnv.plugin.bash plugins/available/dirs.plugin.bash plugins/available/docker-machine.plugin.bash +plugins/available/extract.plugin.bash plugins/available/gif.plugin.bash plugins/available/git-subrepo.plugin.bash plugins/available/git.plugin.bash diff --git a/plugins/available/extract.plugin.bash b/plugins/available/extract.plugin.bash index b32a35a97a..c6ca728194 100644 --- a/plugins/available/extract.plugin.bash +++ b/plugins/available/extract.plugin.bash @@ -3,8 +3,8 @@ about-plugin 'one command to extract them all...' # extract file(s) from compressed status function extract() { - local opt - local OPTIND=1 + local opt OPTIND=1 verbose + local filename filedirname targetdirname while getopts "hv" opt; do case "$opt" in h) @@ -27,32 +27,37 @@ EOU done shift $((OPTIND - 1)) - [ $# -eq 0 ] && extract -h && return 1 - while [ $# -gt 0 ]; do - if [[ ! -f "$1" ]]; then + if [[ $# -eq 0 ]]; then + extract -h + return 1 + fi + + while [[ $# -gt 0 ]]; do + if [[ ! -f "${1:-}" ]]; then echo "extract: '$1' is not a valid file" >&2 shift continue fi - local -r filename=$(basename -- $1) - local -r filedirname=$(dirname -- $1) - local targetdirname=$(sed 's/\(\.tar\.bz2$\|\.tbz$\|\.tbz2$\|\.tar\.gz$\|\.tgz$\|\.tar$\|\.tar\.xz$\|\.txz$\|\.tar\.Z$\|\.7z$\|\.nupkg$\|\.zip$\|\.war$\|\.jar$\)//g' <<< $filename) - if [ "$filename" = "$targetdirname" ]; then + local -r filename=$(basename -- "$1") + local -r filedirname=$(dirname -- "$1") + # shellcheck disable=SC2001 # we don't depend on `extglob`... + targetdirname=$(sed 's/\(\.tar\.bz2$\|\.tbz$\|\.tbz2$\|\.tar\.gz$\|\.tgz$\|\.tar$\|\.tar\.xz$\|\.txz$\|\.tar\.Z$\|\.7z$\|\.nupkg$\|\.zip$\|\.war$\|\.jar$\)//g' <<< "$filename") + if [[ "$filename" == "$targetdirname" ]]; then # archive type either not supported or it doesn't need dir creation targetdirname="" else mkdir -v "$filedirname/$targetdirname" fi - if [ -f "$1" ]; then + if [[ -f "$1" ]]; then case "$1" in *.tar.bz2 | *.tbz | *.tbz2) tar "x${verbose}jf" "$1" -C "$filedirname/$targetdirname" ;; *.tar.gz | *.tgz) tar "x${verbose}zf" "$1" -C "$filedirname/$targetdirname" ;; *.tar.xz | *.txz) tar "x${verbose}Jf" "$1" -C "$filedirname/$targetdirname" ;; *.tar.Z) tar "x${verbose}Zf" "$1" -C "$filedirname/$targetdirname" ;; *.bz2) bunzip2 "$1" ;; - *.deb) dpkg-deb -x${verbose} "$1" "${1:0:-4}" ;; + *.deb) dpkg-deb -x"${verbose}" "$1" "${1:0:-4}" ;; *.pax.gz) gunzip "$1" set -- "$@" "${1:0:-3}" @@ -61,7 +66,7 @@ EOU *.pax) pax -r -f "$1" ;; *.pkg) pkgutil --expand "$1" "${1:0:-4}" ;; *.rar) unrar x "$1" ;; - *.rpm) rpm2cpio "$1" | cpio -idm${verbose} ;; + *.rpm) rpm2cpio "$1" | cpio -idm"${verbose}" ;; *.tar) tar "x${verbose}f" "$1" -C "$filedirname/$targetdirname" ;; *.xz) xz --decompress "$1" ;; *.zip | *.war | *.jar | *.nupkg) unzip "$1" -d "$filedirname/$targetdirname" ;;