#!/bin/bash # # Usage: untar tarball [-]destdir [specific-files-or-directories-to-be-untarred] # # Does: tar xf tarball -C destdir --strip-components=1 # # For example, given the RAPS17.tgz -tarball # * with conventional command "tar xf RAPS17.tgz" it would create RAPS17 # * we want to be able to strip the leading RAPS17/ component out and untar into (say) gnu.dp/ directory # * the command is thus: "untar RAPS17.tgz gnu.dp" would create RAPS17 for (presumably) GNU double precision builds # errcnt=0 if [[ $# -ge 2 ]] ; then tarball=$1 destdir=$2 shift 2 enforce=0 if [[ "$(echo $destdir | cut -c1)" = "-" ]] ; then enforce=1 destdir=$(echo $destdir | cut -c2-) fi if [[ ! -f $tarball ]] ; then echo "Error: tarball $tarball does not exist" >&2 ((errcnt += 1)) fi if [[ $enforce -eq 0 ]] && [[ -d $destdir ]] ; then echo "Error: destdir $destdir already exists -- use -$destdir syntax for destdir" >&2 ((errcnt += 1)) elif [[ -f $destdir ]] ; then echo "Error: destdir $destdir already exists but is a file" >&2 ((errcnt += 1)) fi if [[ $errcnt -eq 0 ]] ; then mkdir -p $destdir || : common="$tarball -C $destdir --strip-components=1 ${*:-}" is_gz=0 echo $tarball | egrep -q 'gz$' || is_gz=$? if [[ $is_gz -eq 0 ]] ; then # tarball is gzipped -- trying to get speedup with parallel gunzip -- unpigz unpigz=$(which unpigz 2>/dev/null || echo "unpigz") if [[ -x "$unpigz" ]] ; then # multithreaded gzip cmd="tar -I $unpigz -xf $common" else cmd="tar -zxf $common" fi else cmd="tar -xf $common" fi echo "Executing $cmd" >&2 $cmd || errcnt=$? fi else echo "Usage: $0 tarball [-]destdir [extra-files-or-directories-to-untar]" >&2 ((errcnt += 1)) fi exit $errcnt