Re: panes.switch and working dir

Adam Krolnik ha scritto:

Instead of file-roller, I wrote a simple script to unpack an archive and
open emelfm2 on it. When closed, I delete the unpacked files...

I wrote another script that unpacks an archive, optionally creating a directory named after the file prefix and then extracting it there. Put it somewhere in your path, then type unpack -h for a list of the available options.

Currently supported formats:
zip rar tar.gz tar.bz2 tgz tbz
(more to be added, it's very simple)

Known bugs and issues:
* Currently manages only one archive at a time

I use this way, for all supported formats:
Extract here: unpack -o %d %f
Extract in folder: unpack -d -o %d %f
Extract in other pane: unpack -o %D %f
Extract in folder in other pane: unpack -d -o %D %f

I'm not a very skilled programmer, so forgive the verbosity. I tested it for a while and seems to work flawlessly. Anyway, I'm not responsible for any damage that will occur to your files for having used it ;)
Hope this list accepts attachments, otherwise I'll post it inline.

Regards,
Giancarlo
#!/bin/bash

#####################################################################
# Changelog:
# 0.1 (2009-02-02):     First release
#
# Known bugs and issues:
# * Currently manages only one archive at a time
#
# This program is released under the terms of the GNU GPL License.
#
#####################################################################

name="unpack"
version=0.1
release_date=2009-02-02
author="Giancarlo Bianchi <giancarlo.b@xxxxxxxxx>"
license=GPL
supported_formats="zip rar tar.gz tar.bz2 tgz tbz"

# Prevents weird behaviours with file names containing spaces.
# FIXME: this has a drawback if managing more than 1 file at a time.
export IFS=$'\n'

verbose=0
extract_to_dir=0
output_dir=`pwd`

function usage()
{
        cat << EOQ

${name} - extracts various archive formats with a common interface.

Usage:
        ${name} [-dohv] <file>

Options:
        -d              Extract the archive to a directory named
                        after the file prefix. This directory will
                        be created unless it already exists.
        -o <dir>        If given, ${name} will chdir to <dir>, then
                        unpack <file>.
        -h              Shows this help.
        -v              Prints some infos about this script.

Supported formats:
${supported_formats}

EOQ
}

function print_info()
{
        cat << EOQ

${name} ${version} (${release_date})
This program is released under the terms of the ${license} license.

EOQ
}

# Gets a command as an argument
# Exits if no suitable program to extract an archive is found.
function check_dep()
{
        if [[ "x`which $1`" == "x" ]] ; then
                echo "No suitable programs to extract this archive were found."
                exit 1
        fi
}

# Used with -d switch. If any file named after the file prefix
# exists, directory creation is skipped and program exits. If it's
# a directory, directory creation is skipped, but the archive will
# be extracted there. Otherwise directory will be created.
function make_dir()
{
        if [[ ! -d $1 ]] ; then
                if [[ -e $1 ]] ; then
                        echo "Couldn't create $1. Exiting."
                        exit 1
                fi
                mkdir $1
        fi
}

# Used with -d switch. Tests the return value of archive extraction,
# and removes the empty dir if packaging went wrong
function clean()
{
        if [[ $1 != 0 ]] ; then
                rm -rf $2
        fi
}

while getopts "do:hv" option ; do
        case ${option} in
                d )
                        extract_to_dir=1
                        ;;
                o )
                        output_dir=${OPTARG/%\//}
                        ;;
                v )
                        print_info
                        exit 0
                        ;;
                h )
                        usage
                        exit 0
                        ;;
                \? )
                        echo -e "Please type ${name} -h for help on usage."
                        exit 1
                        ;;
        esac
done
shift $(( $OPTIND - 1 ))

argsnum=$#
args=$@

# If launched with no arguments, this script prints its usage
if [[ ${argsnum} -lt 1 ]] ; then
        usage
        exit 0
fi


for i in ${args} ; do
        archive_name=`basename ${i}`
        case ${i} in
                *.[zZ][iI][pP] )
                        check_dep unzip
                        basename=${archive_name/.[zZ][iI][pP]/}
                        if [[ ${extract_to_dir} != 0 ]] ; then
                                output_dir=${output_dir}/${basename}
                                make_dir ${output_dir}
                        fi
                        unzip -qq ${i} -d ${output_dir}
                        ;;
                *.[rR][aA][rR] )
                        check_dep unrar
                        basename=${archive_name/.[rR][aA][rR]/}
                        if [[ ${extract_to_dir} != 0 ]] ; then
                                output_dir=${output_dir}/${basename}
                                make_dir ${output_dir}
                        fi
                        unrar -inul -o+ x ${i} ${output_dir}
                        ;;
                *.[tT][aA][rR] )
                        check_dep tar
                        basename=${archive_name/.[tT][aA][rR]/}
                        if [[ ${extract_to_dir} != 0 ]] ; then
                                output_dir=${output_dir}/${basename}
                                make_dir ${output_dir}
                        fi
                        tar -xf ${i} -C ${output_dir}
                        ;;
                *.[tT][gG][zZ] )
                        check_dep tar
                        basename=${archive_name/.[tT][gG][zZ]/}
                        if [[ ${extract_to_dir} != 0 ]] ; then
                                output_dir=${output_dir}/${basename}
                                make_dir ${output_dir}
                        fi
                        tar -xzf ${i} -C ${output_dir}
                        ;;
                *.[tT][aA][rR].[gG][zZ] )
                        check_dep tar
                        basename=${archive_name/.[tT][aA][rR].[gG][zZ]/}
                        if [[ ${extract_to_dir} != 0 ]] ; then
                                output_dir=${output_dir}/${basename}
                                make_dir ${output_dir}
                        fi
                        tar -xzf ${i} -C ${output_dir}
                        ;;
                *.[tT][aA][rR].[bB][zZ]2)
                        check_dep tar
                        basename=${archive_name/.[tT][aA][rR].[bB][zZ]2/}
                        if [[ ${extract_to_dir} != 0 ]] ; then
                                output_dir=${output_dir}/${basename}
                                make_dir ${output_dir}
                        fi
                        tar -xjf ${i} -C ${output_dir}
                        ;;
                *.[tT][aA][rR].[zZ])
                        check_dep tar
                        basename=${archive_name/.[tT][aA][rR].[zZ]/}
                        if [[ ${extract_to_dir} != 0 ]] ; then
                                output_dir=${output_dir}/${basename}
                                make_dir ${output_dir}
                        fi
                        tar -xZf ${i} -C ${output_dir}
                        ;;
                * )
                        echo "${name} can't manage this format."
                        exit 1
                        ;;
        esac
        clean $? ${basename}
done

exit 0

Other related posts: