From f83aab2aa76524e69ae06b68344e722ffc7912ad Mon Sep 17 00:00:00 2001 From: Luke Smith Date: Thu, 4 Oct 2018 22:03:35 -0400 Subject: [PATCH] ext alias added and extract script -c option fixed --- .scripts/ext | 1 + .scripts/extract | 52 ++++++++++++++++++++++++++++-------------------- 2 files changed, 31 insertions(+), 22 deletions(-) create mode 120000 .scripts/ext diff --git a/.scripts/ext b/.scripts/ext new file mode 120000 index 0000000..562d2b3 --- /dev/null +++ b/.scripts/ext @@ -0,0 +1 @@ +extract \ No newline at end of file diff --git a/.scripts/extract b/.scripts/extract index ccc8904..40f4d66 100755 --- a/.scripts/extract +++ b/.scripts/extract @@ -1,36 +1,44 @@ -#!/bin/bash +#!/bin/sh # A general, all-purpose extraction script. # # Default behavior: Extract archive into new directory # Behavior with `-c` option: Extract contents into current directory while getopts "hc" o; do case "${o}" in - h) echo -e "Options:\n -c: Extract archive into current directory rather than a new one." && exit ;; - c) dirpref="" && archive=${@:2} ;; + c) extracthere="True" ;; + *) printf "Options:\\n -c: Extract archive into current directory rather than a new one.\\n" && exit ;; esac done -[ -z ${dirpref+x} ] && dirpref="../" && archive="$@" +if [ -z "$extracthere" ]; then + archive="$(readlink -f "$*")" && + directory=${archive%.*} && + mkdir -p "$directory" && + cd "$directory" || exit +else + archive="$(readlink -f "$(echo "$*" | cut -d' ' -f2)")" +fi + +[ "$archive" = "" ] && printf "Give archive to extract as argument.\\n" && exit if [ -f "$archive" ] ; then - [[ "$dirpref" == "../" ]] && NAME=${archive%.*} && mkdir "$NAME" && cd "$NAME" case "$archive" in - *.tar.bz2) tar xvjf "$dirpref""$archive" ;; - *.tar.gz) tar xvzf "$dirpref""$archive" ;; - *.tar.xz) tar xvJf "$dirpref""$archive" ;; - *.lzma) unlzma "$dirpref""$archive" ;; - *.bz2) bunzip2 "$dirpref""$archive" ;; - *.rar) unrar x -ad "$dirpref""$archive" ;; - *.gz) gunzip "$dirpref""$archive" ;; - *.tar) tar xvf "$dirpref""$archive" ;; - *.tbz2) tar xvjf "$dirpref""$archive" ;; - *.tgz) tar xvzf "$dirpref""$archive" ;; - *.zip) unzip "$dirpref""$archive" ;; - *.Z) uncompress "$dirpref""$archive" ;; - *.7z) 7z x "$dirpref""$archive" ;; - *.xz) unxz "$dirpref""$archive" ;; - *.exe) cabextract "$dirpref""$archive" ;; - *) echo "extract: '$archive' - unknown archive method" ;; + *.tar.bz2) tar xvjf "$archive" ;; + *.tar.gz) tar xvzf "$archive" ;; + *.tar.xz) tar xvJf "$archive" ;; + *.lzma) unlzma "$archive" ;; + *.bz2) bunzip2 "$archive" ;; + *.rar) unrar x -ad "$archive" ;; + *.gz) gunzip "$archive" ;; + *.tar) tar xvf "$archive" ;; + *.tbz2) tar xvjf "$archive" ;; + *.tgz) tar xvzf "$archive" ;; + *.zip) unzip "$archive" ;; + *.Z) uncompress "$archive" ;; + *.7z) 7z x "$archive" ;; + *.xz) unxz "$archive" ;; + *.exe) cabextract "$archive" ;; + *) printf "extract: '%s' - unknown archive method\\n" "$archive" ;; esac else - echo "File \"$archive\" not found." + printf "File \"%s\" not found.\\n" "$archive" fi