Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Is there a utility that always prompts the user for confirmation before executing a command similar to the way sudo asks for password?

share|improve this question
1  
Do you really want that for any command? Including cd and ls? –  Carsten Thiel Jan 19 '11 at 8:16
    
I'd like to have it in my own aliases in .bashrc. –  m33lky Jan 20 '11 at 5:03
add comment

2 Answers

up vote 5 down vote accepted

Do you want it to work without typing an extra command, e.g.

$ rm file

Or only when the user types something like

$ confirm rm file

Or only when the user tries to run certain commands, e.g.

$ rm file

but not for

$ echo "Hello"

If option 1, that can be done using the preexec hook in zsh, or the DEBUG trap in bash.

If option 2, put something like this in /etc/bash.bashrc or other shell startup file.

confirm() {
    echo -n "Do you want to run $*? [N/y] "
    read -N 1 REPLY
    echo
    if test "$REPLY" = "y" -o "$REPLY" = "Y"; then
        "$@"
    else
        echo "Cancelled by user"
    fi
}

If option 3, you could modify the confirm script above, or, some commands have an option to ask before doing something, e.g. rm -i. You could put alias rm='rm -i' in /etc/bash.bashrc.

share|improve this answer
add comment

I have played with bash a bit and figured out a hack by which this could be made possible.

#!/bin/bash

debug_trap () {
    echo "executing $BASH_COMMAND"
    echo "Allow?"
    select choice in yes no
    do
        if [ "$choice" = "yes" ]
        then break
        elif [ "$choice" = "no" ]
        then return 1
        fi
    done
}

shopt -s extdebug
trap debug_trap DEBUG

You can save this script under, say, confirm-any-command.sh and source it in your bashrc. It will ask for confirmation of each command you will try to execute.

Please note that this is nothing more than a proof-of-concept hack; I doubt it can really be any useful in this form. If you will have to confirm each and every command you type, you will very soon acquire a habit to automatically hit "yes" after each command. Your mental mapping for "end of command" will change from just Enter to Enter,yes,Enter - you will be typing it as a whole, don't even trying to spend some time on verifying that you really want this command to execute. This is counterproductive and won't help you.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.