help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Preferred method wrapping mkdir


From: konsolebox
Subject: Re: [Help-bash] Preferred method wrapping mkdir
Date: Sat, 21 Nov 2015 02:49:19 +0800

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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]