- if [[ $# -ge 3 && ! -d ${!#} ]]; then
- mkdir -p "${!#}"
- elif [[ $# -ge 2 && -d ${!#} ]]; then
- local oldcwd=$PWD
- cd ${!#} # last arg
- for x in "${@:1:$(($#-1))}"; do # all but last arg
- # remove any trailing slashes
- x="${x%%+(/)}"
- # remove any leading directory components
- x="${x##*/}"
- [[ -e "$x" || -L "$x" ]] && $remove "$x"
- done
- cd "$oldcwd"
+ if [[ $# -ge 3 ]]; then
+ if [[ -d ${!#} ]]; then
+ local oldcwd=$PWD
+ cd ${!#} # last arg
+ for x in "${@:1:$(($#-1))}"; do # all but last arg
+ # remove any trailing slashes
+ x="${x%%+(/)}"
+ # remove any leading directory components
+ x="${x##*/}"
+ [[ -e "$x" || -L "$x" ]] && $remove "$x"
+ done
+ cd "$oldcwd"
+ else
+ mkdir -p "${!#}"
+ fi