Created
May 31, 2017 18:09
-
-
Save IQAndreas/95aba1fb53eb12d929434420e8ccb41b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# --------------------------------------------------------------------------- # | |
# Created by Andreas Renberg (https://github.com/IQAndreas) | |
# Based on work by Doctor J (https://stackoverflow.com/a/1403489/617937) | |
# This code is in the public domain and under no license. | |
# Well, my code at least. The ~8 lines by Doctor J still belong to him. | |
# --------------------------------------------------------------------------- # | |
# Personal notes: What if a file is named 'file.'? | |
# Right now, it gives basename='file.' ext='' | |
# But I really don't know a "right" way of doing this behavior. | |
# This is fine. | |
FULL_PATH=; | |
print_value='%f'; # Default | |
extended=; | |
for arg in "$@" | |
do | |
case "$arg" in | |
# You can set these arguments more than once. Only the last one is used. | |
--dir) print_value='%d';; # (/path/file.tar.gz -> /path/) | |
--filename) print_value='%f';; # (/path/file.tar.gz -> file.tar.gz) | |
--basename) print_value='%b';; # (/path/file.tar.gz -> file.tar) | |
--ext) print_value='%e';; # (/path/file.tar.gz -> gz) | |
--extension) print_value='%e';; # (/path/file.tar.gz -> gz) # Alternative to --ext | |
# Extended extensions means the file 'file.tar.gz' has the extension 'tar.gz' | |
--extended) extended=true;; # (/path/file.tar.gz -> tar.gz) | |
--*) | |
>&2 echo "Unknown argument '$arg'"; | |
exit 1; | |
;; | |
*) | |
if [[ -z "$FULL_PATH" ]]; then | |
FULL_PATH="$arg"; | |
else | |
>&2 echo "Too many file names provided"; | |
exit 1; | |
fi | |
;; | |
esac | |
#DISABLED | |
# --basename-extended) print_value='%B';; # (/path/file.tar.gz -> file) # Actually, this means extended extensions | |
# --ext-extended) print_value='%E';; # (/path/file.tar.gz -> file) # Actually, this means extended extensions | |
done | |
# Thank you, Doctor J | |
# https://stackoverflow.com/a/1403489/617937 | |
filename="${FULL_PATH##*/}" # Strip longest match of */ from start | |
dir="${FULL_PATH:0:${#FULL_PATH} - ${#filename}}" # Substring from 0 thru pos of filename | |
if [[ -n "$extended" ]]; then | |
base="${filename%%.[^.]*}"; # Strip longest match of . plus at least one non-dot char from end | |
ext="${filename:${#base} + 1}"; # Substring from len of base thru end | |
else | |
base="${filename%.[^.]*}"; # Strip shortest match of . plus at least one non-dot char from end | |
ext="${filename:${#base} + 1}"; # Substring from len of base thru end | |
fi | |
# If we have an extension and no base, it's really the base | |
if [[ -z "$base" && -n "$ext" ]]; then | |
base=".$ext" | |
ext="" | |
fi | |
# This will eventually be replaced by a "date +format" type deal | |
case "$print_value" in | |
%d) echo "$dir";; | |
%f) echo "$filename";; | |
%b) echo "$base";; | |
%e) echo "$ext";; | |
*) | |
>&2 echo "NOTE TO DEVELOPER: You done fudged up. I have no idea what the flag '$print_value' is supposed to represent."; | |
exit 1; | |
;; | |
esac |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment