From ebe7a6992b69ccb999bf8cf5ae6ace47eac93b4d Mon Sep 17 00:00:00 2001 From: xeruf <27jf@pm.me> Date: Thu, 25 Nov 2021 14:08:40 +0100 Subject: [PATCH] bin/ex: automatically use subdirectory --- .local/bin/scripts/ex | 46 ++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 20 deletions(-) diff --git a/.local/bin/scripts/ex b/.local/bin/scripts/ex index 804e153..cbc8e24 100755 --- a/.local/bin/scripts/ex +++ b/.local/bin/scripts/ex @@ -1,24 +1,30 @@ -#!/bin/sh +#!/bin/sh -e # ex - archive extractor # usage: ex for arg do - if test -r "$arg"; then - case "$arg" in - *.tar.bz2) tar xjf "$arg" ;; - *.tar.*|*.tar) tar --extract --file "$arg";; - *.bz2) bunzip2 "$arg" ;; - *.rar) unrar x "$arg" ;; - *.gz) gunzip "$arg" ;; - *.tbz2) tar xjf "$arg" ;; - *.tgz) tar xzf "$arg" ;; - *.zip|*.jar) unzip "$arg" ;; - *.Z) uncompress "$arg";; - *.7z) 7z x "$arg" ;; - *.deb) ar x "$arg" ;; - *.zst) unzstd "$arg" ;; - *) echo "'$arg' cannot be extracted by ex" ;; - esac - else - echo "'$1' is not a readable file" - fi + if test -r "$arg"; then + arg="$(realpath "$arg")" + name="$(basename "${arg%.*}")" + if test "$(ls -U | wc -l)" -gt 2; then + mkdir "$name" + cd "$name" + fi + case "$arg" in + *.tar.bz2) tar xjf "$arg" ;; + *.tar.*|*.tar) tar --extract --file "$arg";; + *.bz2) bunzip2 "$arg" ;; + *.rar) unrar x "$arg" ;; + *.gz) gunzip "$arg" ;; + *.tbz2) tar xjf "$arg" ;; + *.tgz) tar xzf "$arg" ;; + *.zip|*.jar) unzip "$arg" ;; + *.Z) uncompress "$arg";; + *.7z) 7z x "$arg" ;; + *.deb) ar x "$arg" ;; + *.zst) unzstd "$arg" ;; + *) echo "'$arg' cannot be extracted by ex" ;; + esac + else + echo "'$1' is not a readable file" + fi done