[Top][All Lists]

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

Re: shell-mode flakey

From: Glenn Morris
Subject: Re: shell-mode flakey
Date: Tue, 18 Dec 2007 19:54:09 -0500
User-agent: Gnus (www.gnus.org), GNU Emacs (www.gnu.org/software/emacs/)

Roland Winkler wrote:

> When I discussed this some time ago with one of the bash
> maintainers, he said that he had just recovered all files of a
> colleague who had typed "rm foo *" instead of "rm foo*". For those
> and only those cases I'd like to have a reliable interactive query
> before rm will do its job.

Totally off-topic, but here's what I use for this kind of thing in
bash. Uses an alias + a function. It occasionally chokes on odd
characters in filenames, but otherwise works for me.

## Implement an equivalent of tcsh's 'rmstar'.
## After gnu.bash.bug: address@hidden
function rmfunc()
    unset _rmfunc

    [ $# -lt 2 ] && {
        echo "rm: too few arguments"
        return 1

    local noglob

    case $1 in
        *f*) noglob=t ;;


    ## Glob expansion was on.
    if [ ! "$noglob" ]; then

        set +f                  # reactivate glob expansion

        local arg star dir input

        for arg; do


            case "$arg" in
                '*'|*/'*'|*/'*'/*) star=t ;;

            [ $star ] || continue


            [ "$dir" = "$arg" ] && dir=.

            while : ; do
                echo -n "Remove all files in $dir [yn"\!"]? "

                read input

                case $input in
                    y) break 1 ;;
                    n) return 1 ;;
                    '!') break 2 ;;
                    *) echo "Please answer with one of y, n or "\! ;;
        done                    # $arg

        set -f

        ## Escape [ #\t${}()].
        IFS=$'\n' set -- $(for arg; do
            echo "$arg"
            done | sed -e 's/\([ '$'\t''${}()#]\)/\\\1/g')

        [ "$noglob" ] || set +f

        ## Conflict here. Need quotes to handle names
        ## with spaces, yet quotes prevents glob expansion.
        ## Need eval for glob expansion.
        eval command rm "$@"

    else                        # glob expansion was off

        command rm "$@"

}                               # function rmfunc

## Need to prevent glob expansion before passing args to rmfunc.
## Note use "&&" rather than ";" so that "<command> && rm" works
## as expected.
alias rm='_rmfunc="$-" && set -f && rmfunc "$_rmfunc"'

reply via email to

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