bin/lsarchive: refactor + port to bash

This commit is contained in:
Henrik Lissner 2016-11-09 12:49:19 +01:00
parent 5059f3bb18
commit ab50a589bf

View file

@ -1,13 +1,11 @@
#!/usr/bin/env bash
# #
# Lists the contents of archives. # Lists the contents of archives.
# #
# Authors: # Adapted from a script by Sorin Ionescu <sorin.ionescu@gmail.com>
# Sorin Ionescu <sorin.ionescu@gmail.com>
#
local verbose if (( $# == 0 ))
then
if (( $# == 0 )); then
cat >&2 <<EOF cat >&2 <<EOF
usage: $0 [-option] [file ...] usage: $0 [-option] [file ...]
@ -18,38 +16,40 @@ Report bugs to <sorin.ionescu@gmail.com>.
EOF EOF
fi fi
if [[ "$1" == "-v" || "$1" == "--verbose" ]]; then if [[ "$1" == "-v" || "$1" == "--verbose" ]]
verbose=0 then
shift verbose=0
shift
fi fi
while (( $# > 0 )); do while (( $# > 0 ))
if [[ ! -s "$1" ]]; then do
print "$0: file not valid: $1" >&2 if [[ ! -s "$1" ]]
then
>&2 echo "$0: file not valid: $1"
shift
continue
fi
case "${1:l}" in
*.tar.gz|*.tgz) tar t${verbose:+v}vzf "$1" ;;
*.tar.bz2|*.tbz|*.tbz2) tar t${verbose:+v}jf "$1" ;;
*.tar.xz|*.txz) tar --xz --help &> /dev/null \
&& tar --xz -t${verbose:+v}f "$1" \
|| xzcat "$1" | tar t${verbose:+v}f - ;;
*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \
&& tar --lzma -t${verbose:+v}f "$1" \
|| lzcat "$1" | tar x${verbose:+v}f - ;;
*.tar) tar t${verbose:+v}f "$1" ;;
*.zip) unzip -l${verbose:+v} "$1" ;;
*.rar) unrar &> /dev/null \
&& unrar ${${verbose:+v}:-l} "$1" \
|| rar ${${verbose:+v}:-l} "$1" ;;
*.7z) 7za l "$1" ;;
*) >&2 echo "$0: cannot list: $1"
success=1
;;
esac
shift shift
continue
fi
case "$1:l" in
(*.tar.gz|*.tgz) tar t${verbose:+v}vzf "$1" ;;
(*.tar.bz2|*.tbz|*.tbz2) tar t${verbose:+v}jf "$1" ;;
(*.tar.xz|*.txz) tar --xz --help &> /dev/null \
&& tar --xz -t${verbose:+v}f "$1" \
|| xzcat "$1" | tar t${verbose:+v}f - ;;
(*.tar.zma|*.tlz) tar --lzma --help &> /dev/null \
&& tar --lzma -t${verbose:+v}f "$1" \
|| lzcat "$1" | tar x${verbose:+v}f - ;;
(*.tar) tar t${verbose:+v}f "$1" ;;
(*.zip) unzip -l${verbose:+v} "$1" ;;
(*.rar) unrar &> /dev/null \
&& unrar ${${verbose:+v}:-l} "$1" \
|| rar ${${verbose:+v}:-l} "$1" ;;
(*.7z) 7za l "$1" ;;
(*)
print "$0: cannot list: $1" >&2
success=1
;;
esac
shift
done done