[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Preferred method wrapping mkdir
From: |
Joseph Pesco |
Subject: |
Re: [Help-bash] Preferred method wrapping mkdir |
Date: |
Fri, 20 Nov 2015 12:51:28 -0800 |
On Sat, 2015-11-21 at 02:49 +0800, konsolebox wrote:
> On Sat, Nov 14, 2015 at 12:55 AM, Joseph Pesco <address@hidden>
> wrote:
> > I've developed a passion for wrapping mkdir up in a utility
> > function.
> > Is there a reason for not using either of the following two methods
> > to
> > do it? Is there a better method I've not thought of?
>
> Same idea as everyone but try to emulate argument parsing if
> possible;
> which is what I do most of the time.
>
> function mkdir {
> local OPTS=() DIRS=()
>
> while [[ $# -gt 0 ]]; do
> case $1 in
> --help)
> echo "Usage: [MKDIR_OPTIONS] [--] DIR ..."
> return 2
> ;;
> -m|--mode)
> OPTS+=("${@:1:2}")
> shift
> ;;
> -*)
> OPTS+=("$1")
> ;;
> --)
> DIRS+=("${@:2}")
> break
> ;;
> *)
> DIRS+=("$1")
> ;;
> esac
>
> shift
> done
>
> if [[ address@hidden -eq 0 ]]; then
> echo "No directory specified."
> return 1
> fi
>
> command mkdir "address@hidden" -- "address@hidden" || return ## &&
> comand cd ...
> command cd -- "address@hidden:0:1}" ## Or "address@hidden:(-1)}", or
> disallow multiple dirs, or don't cd if multiple dirs were specified
> }
>
> You can explicitly check validity of mkdir options on the wrapper
> itself if you want.
>
> If you're concerned about compatibility just tweak it to make it more
> universal.
Yes, I accept the need to parse! Earlier this was suggested (and I
implemented something very similiar in my cp function, though I had to
add the function reserve word and haven't explained to myself why yet.
mkdir() { command mkdir "$@" && cd "${@:(-1)}"; }
The difficulty with "overloading" shell commands is breaking the
enviroment by changing default expectations!
I decided to modify the PATH in .bash_profile as follows. (I'm not fond
of dangling dot and prefer to type ./ where ever I go, but this is
personal peference I believe.)
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/.cfs/bin
Joe Pesco