Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- no_extglob () {
- local path="${1:-dummy}" # store argument with dummy fallback
- [[ "$path" =~ ^(.*[^/])/*$ ]]
- path="${BASH_REMATCH[1]:-/}" # strip trailing /
- [[ "/$path" =~ ^(.+)(/[^/]*/*)$ ]]
- path="${BASH_REMATCH[1]:-.}" # stript last component and default to curdir
- [[ "$path" =~ ^(.*[^/])/*$ ]]
- path="${BASH_REMATCH[1]:-/}" # strip trailing /
- echo "${path/#\/?*/${path:1}}" # strip leading / if strlen>1 (looks weird in vim, but is correct)
- }
- with_extglob () {
- local path="${1:-dummy}" # store argument with dummy fallback
- path="${path//+(\/)/\/}" # remove excessive /
- path="/${path%/}" # strip trailing / and prepare edgecase on next step: 'no / in path'
- path="${path%/?*}" # strip last component
- path="${path:-.}" # if empty default to curdir
- echo "${path/#\/?*/${path:1}}" # strip leading / if strlen>1 (looks weird in vim, but is correct)
- }
- shopt -u extglob
- shopt -s extglob # comment out to run without extglob
- case "$(shopt extglob)" in
- *on)
- #echo "using extglob"
- strategy=with_extglob
- ;;
- *)
- #echo "using regex"
- strategy=no_extglob
- ;;
- esac
- for arg in "$@"; do
- $strategy "$arg"
- done
Advertisement
Add Comment
Please, Sign In to add comment