aboutsummaryrefslogblamecommitdiff
path: root/sh/shrc.d/gt.sh
blob: ef24a4bb7e63b162cea620a1d9c511825c582e4b (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                                                                              


                          

                                              


                








                                                                               

                                                     


                           
 
                                                                   

                               
 
# If the argument is a directory, change to it.  If it's a file, change to its
# parent.  Stands for "get to".
gt() {

    # Check argument count
    if [ "$#" -ne 1 ] ; then
        printf >&2 'gt(): Need one argument\n'
        return 2
    fi

    # Make certain there are no trailing slashes to foul us up, and anchor path
    # if relative
    while : ; do
        case $1 in
            */) set -- "${1%/}" ;;
            /*) break ;;
            *) set -- "$PWD"/"$1" ;;
        esac
    done

    # If target isn't a directory, chop to its parent
    if ! [ -d "$1" ] ; then
        set -- "${1%/*}"
    fi

    # Try to change into the determined directory, or root if empty
    # shellcheck disable=SC2164
    cd -- "${1:-/}"
}