Page 1 of 1

Fix this script pls - unrar to specified dir

Posted: Sat Jul 14, 2012 7:52 pm
by Ozman
The situation is this: I have a main directory that contains sub-directories. Each sub-directory contains a file that is split into .rar parts. The script below, when put into the main directory, will extract each file from each sub-directory and put the assembled file into that sub-directory. However, I want all assembled files to be extracted to one directory: either /home/abc/main or preferably /storage/temp

I cannot work out how to do that with the script below. I have tried the options, but I am obviously doing something wrong.

Code: Select all

#!/bin/bash 

#unrarall
# Copyright (C) 2011 Brendan Le Foll & Dan Liew
#
# This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
##########################################################################

# Set some defaults
declare -x DIR="`pwd`"
declare -ix CLEAN=0
declare -rx UNRARALL_VERSION="0.2"
declare -ix FORCE_CLEAN=0
declare -ix VERBOSE=0
declare -x UNRAR_METHOD="e"

function usage() 
{
  echo "Usage: unrarall [ --dir DIRECTORY ] [ --clean | --force ] [ --full-path ] [ --verbose ] 
               unrarall --help
	       unrarall --version
	
	Usage (short options):
  	       unrarall [ -d DIRECTORY ] [ -c | -f ] [ -v ]
	       unrarall -h
DESCRIPTON
unrarall is a utility to unrar and cleanup (delete) all rar files within a directory DIRECTORY. Sub-directories are automatically recursed and if a rar file exists in a sub-directory then the rar file is extracted into that subdirectory.

Use --clean if you want to cleanup. Otherwise no cleaning is done. It can also be used to delete rar files that have already been used for extraction with --force. Use with caution!

OPTIONS

-c, --clean      Clean after unrar. If the extraction fails then the directory will NOT be cleaned. Use --force to override this.
-d, --dir        Use directory DIRECTORY. If this argument is not supplied the current working directory is used.
-f, --force      Force clean even if unrar fails. Implies --clean. Use this if you've already used unrarall on the directory before
                 and now you want to remove the rar files. This should be pretty fast as unrar is set to not overwrite already extracted files.
--full-path      Extract full path inside rar files instead of just extracting the files in the rar file which is the default behaviour.
-h, --help       Displays this help message and exits.
-v, --verbose    Show extraction progress as unrarall executes. This is not done by default
--version        Give version information version.

VERSION: $UNRARALL_VERSION

"
}

#function to display pretty messages
function message()
{
  #Assume $1 is message type & $2 is message
  #See http://www.frexx.de/xterm-256-notes/ for information on xterm colour codes

  case "$1" in
    error)
      #use escape sequence to show red text
      echo -e "\033[31m${2}\033[0m" 1>&2
    ;;
    ok)
      #use escape sequence to show green text
      echo -e "\033[32m${2}\033[0m"
    ;;
    *)
      echo "$2"
  esac

}

# Parse command line arguments
while [ -n "$1" ]; do
  case "$1" in
    -h | --help )  
      usage	
      exit 0
      ;;
    --version )  
      echo "$UNRARALL_VERSION"
      exit 0
      ;;
    -d | --dir )  
      shift
      DIR="$1"
      ;;
    -c | --clean )  
      CLEAN=1
      ;;
    -f | --force )  
      FORCE_CLEAN=1
      CLEAN=1
      ;;
    -v | --verbose )
      VERBOSE=1
      ;;
    --full-path )
      UNRAR_METHOD="x"
      ;;
    *)
      # user issued unrecognised option
      message error "Unrecognised option: $1" 
      usage
      exit 1
      ;;
  esac
  shift
done