aboutsummaryrefslogtreecommitdiff
path: root/sh/shrc.d/md.sh
blob: 1f8a8aafc9cbfeac898ad3844435b5d7facb172d (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
# 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
    PMD=$1
}