[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 14:11:59 -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 agree the need to parse! I modified the cp wrapper first based
on the results of this discussion and it being the most challanging
I've yet to tackle. A search for a marker in the body of the target
file to copy alters the behavior of the function. The case of copying
multiply files isn't yet handled though the need identified.
The cfs_cp () was in a file often use sourced from .bashrc called
bash_cfs and living in the same directory as .bashrc. It still lives
there, but after being renamed cp the function reserved word was
required for the function declaration. That portion of my
configuration once in ~/bin has been moved to .cfs/bin and the PATH is
modified as follows in .bash_profile:
PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/.cfs/bin
This is how I handle what you've spelled out:
# Hack alert: removing options!
while [ "${1:0:1}" == "-" ]; do
shift 1
done
Joe address@hidden