Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
up vote 6 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

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
    
Stops on Allow? 1) yes 2) no #? neither yes nor no work causing infinite loop (Debian 8) – vladkras Jan 13 at 15:13
    
@vladkras read help select: you're supposed to input 1 or 2 rather than yes or no. The automatic retry for invalid inputs is intended behavior (so that the script writer doesn't have to reinvent a while herself, retaining conciseness and clarity), and EOF input terminates the loop. Knowing that, you can check yourself that the script above works. I won't be modifying it, since it's a crude proof of concept anyway. – ulidtko Jan 14 at 13:58
    
that's true. I was confused by your y e s Enter example – vladkras Jan 14 at 16:54

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.