helm/bin/lsarchive.sh

52 lines
1.3 KiB
Bash
Raw Normal View History

2016-11-09 03:49:19 -08:00
#!/usr/bin/env bash
2016-11-21 20:58:53 -08:00
2015-06-19 07:04:48 -07:00
# Lists the contents of archives.
#
2016-11-09 03:49:19 -08:00
# Adapted from a script by Sorin Ionescu <sorin.ionescu@gmail.com>
2015-06-19 07:04:48 -07:00
2016-11-21 20:58:53 -08:00
if (($# == 0)); then
2015-06-19 07:04:48 -07:00
cat >&2 <<EOF
usage: $0 [-option] [file ...]
options:
-v, --verbose verbose archive listing
Report bugs to <sorin.ionescu@gmail.com>.
EOF
fi
2016-11-21 20:58:53 -08:00
if [[ $1 = -v || $1 = --verbose ]]; then
2016-11-09 03:49:19 -08:00
verbose=0
2015-06-19 07:04:48 -07:00
shift
2016-11-09 03:49:19 -08:00
fi
2015-06-19 07:04:48 -07:00
2016-11-21 20:58:53 -08:00
while (($# > 0)); do
if [[ ! -s $1 ]]; then
2016-11-09 03:49:19 -08:00
>&2 echo "$0: file not valid: $1"
shift
continue
fi
2016-11-21 20:58:53 -08:00
case ${1:l} in
2016-11-09 03:49:19 -08:00
*.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
2015-06-19 07:04:48 -07:00
2016-11-09 03:49:19 -08:00
shift
2015-06-19 07:04:48 -07:00
done