aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d/md.sh
blob: 7085d258a18ce3c87f5fd318691c274242c03259 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# Set marked directory to given dir or current dir
md() {

    # Accept up to one argument
    if [ "$#" -gt 1 ] ; then
        printf >&2 'md(): Too many arguments\n'
        return 2
    fi

    # If first arg unset or empty, assume the user means the current dir
    [ -n "$1" ] || set -- "$PWD"

    # If specified path is . or .., quietly expand it
    case $1 in
        .) set -- "${PWD%/}" ;;
        ..)
            set -- "${PWD%/}"
            set -- "${1%/*}"
            ;;
    esac

    # If specified path not a directory, refuse to mark it
    if ! [ -d "$1" ] ; then
        printf >&2 'md(): Not a directory\n'
        return 2
    fi

    # Save the specified path in the marked directory var
    # shellcheck disable=SC2034
    PMD=$1
}