Newer
Older
invertedlogic / Scripts / tar.sh
#!/bin/bash
#===============================================================================
#
#          FILE:  tar.sh
#         USAGE:  ./tar.sh 
#   DESCRIPTION:  TAR command as a shell script!
#                 A bit of a toy/novelty idea to use a script for this
#       CREATED:  17/11/2017
# 
#===============================================================================

if [ $# -lt 2 ]
then
  echo "Usage:  $0  (-x) | (-c  files)  archive.tar"
  exit -1
fi

if [ x"$1" == x"-x" ]
then
  echo "Extracting..."
  LINE=0
  LINES=0
  FILE=
  cat "$2" | while IFS= read -r TEXT
  do
    if [ x"$LINES" == x"$LINE" ]
    then
      LINE=0
      LINES=`echo $TEXT`
      read FILE
      echo "Extracting $LINES lines from $FILE "
      if [ -f $FILE ]
      then
        echo "$FILE file already exists. Aborting."
        exit -1
      fi
    else
      echo "${TEXT}" >> $FILE
      LINE=$(($LINE + 1))
    fi
  done

elif [ x"$1" == x"-c" ]
then
  if [ $# -lt 3 ]
  then
    echo "No files to add"
    exit -1
  else
    echo "Creating..."
    shift
    rm -rf .tmp.tar
    while [ $# -gt 1 ]
    do
      echo "Adding $1"
      echo "`cat \"$1\" | wc -l`" >> .tmp.tar
      echo "$1" >> .tmp.tar
      cat "$1" >> .tmp.tar
      shift
    done
    mv .tmp.tar "$1"
    echo "Created archive $1"
  fi
else
  echo "Usage:  $0  (-x) | (-c  files)  archive.tar"
  exit -1
fi