Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

The discussion in the comments of this answerthis answer made me wonder how hard it would be to write a built-in only version of dirname.

The discussion in the comments of this answer made me wonder how hard it would be to write a built-in only version of dirname.

The discussion in the comments of this answer made me wonder how hard it would be to write a built-in only version of dirname.

added 79 characters in body
Source Link

Update: Made a gist.

Update: Made a gist.

Tweeted twitter.com/#!/StackCodeReview/status/639217898922344448
Update for local script changes (no effect on actual code).
Source Link
#!/bin/sh

p=$1
alt=$2
skip78=

stripslashes() {
    i=$1
    while [ "$i" != "$o" ]; do
        o=$i
        i=${i%/}
    done
    eval "$2=\$i"
}

[ "$p" = '//' ] || {
    case "$p" in
        *[!/]*|'')
            stripslashes "$p" p
            case "$p" in
                */*)
                    p=${p%/*}
                    ;;
                *)
                    p=.
                    skip78=skip78
                    ;;
            esac
            ;;
        *)
            p=/
            skip78=skip78
            ;;
    esac
}
[ -n "$skip78" ] || {
    { [ "$p" != '//' ] || [ -nz "$2""$alt" ]; } && {
        stripslashes "$p" p
        [ -z "$p" ] && p=/
    }
}

printf -- %s\\n "$p"
#!/bin/sh

p=$1
skip78=

stripslashes() {
    i=$1
    while [ "$i" != "$o" ]; do
        o=$i
        i=${i%/}
    done
    eval "$2=\$i"
}

[ "$p" = '//' ] || {
    case "$p" in
        *[!/]*|'')
            stripslashes "$p" p
            case "$p" in
                */*)
                    p=${p%/*}
                    ;;
                *)
                    p=.
                    skip78=skip78
                    ;;
            esac
            ;;
        *)
            p=/
            skip78=skip78
            ;;
    esac
}
[ -n "$skip78" ] || {
    { [ "$p" != '//' ] || [ -n "$2" ]; } && {
        stripslashes "$p" p
        [ -z "$p" ] && p=/
    }
}

printf -- %s\\n "$p"
#!/bin/sh

p=$1
alt=$2
skip78=

stripslashes() {
    i=$1
    while [ "$i" != "$o" ]; do
        o=$i
        i=${i%/}
    done
    eval "$2=\$i"
}

[ "$p" = '//' ] || {
    case "$p" in
        *[!/]*|'')
            stripslashes "$p" p
            case "$p" in
                */*)
                    p=${p%/*}
                    ;;
                *)
                    p=.
                    skip78=skip78
                    ;;
            esac
            ;;
        *)
            p=/
            skip78=skip78
            ;;
    esac
}
[ -n "$skip78" ] || {
    { [ "$p" != '//' ] || [ -z "$alt" ]; } && {
        stripslashes "$p" p
        [ -z "$p" ] && p=/
    }
}

printf -- %s\\n "$p"
added 40 characters in body
Source Link
Ethan Bierlein
  • 15.9k
  • 4
  • 60
  • 146
Loading
fixed link; edited tags
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
Loading
Source Link
Loading