Hour 13

1: Add tar file extraction to the mytar script.

Assume that the -x option indicates that the user wants to extract tar files and that the correct value of TARGS for extracting tar files is -xvf.

A1: One correct implementation is as follows:
#!/bin/sh

USAGE="Usage: `basename $0` [-c|-t] [files|directories]"
if [ $# -lt 2 ] ; then
    echo "$USAGE" ;
    exit 1 ;
fi

case "$1" in
    -t|-x) TARGS=${1}vf ; shift
        for i in "$@" ; do
            if [ -f "$i" ] ; then
                FILES=`tar $TARGS "$i" 2>/dev/null`
                if [ $? -eq 0 ] ; then
                    echo ; echo "$i" ; echo "$FILES"
                else
                    echo "ERROR: $i not a tar file."
                fi
            else
                echo "ERROR: $i not a file."
            fi
        done
        ;;
    -c) shift ; TARGS="-cvf" ;
        tar $TARGS archive.tar "$@"
        ;;
     *) echo "$USAGE"
        exit 0
        ;;
esac
exit $?
2: Add the extract option to ...

Get Sams Teach Yourself Shell Programming in 24 Hours, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.