Is there a utility that always prompts the user for confirmation before executing a command similar to the way sudo
asks for password?
3 Answers
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
.
-
I would like the confirmation only for one single command that always gives me headache. How would the script look like?– DanijelApr 17, 2019 at 8:03
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.
-
Stops on
Allow? 1) yes 2) no #?
neitheryes
norno
work causing infinite loop (Debian 8)– vladkrasJan 13, 2016 at 15:13 -
@vladkras read
help select
: you're supposed to input1
or2
rather thanyes
orno
. The automatic retry for invalid inputs is intended behavior (so that the script writer doesn't have to reinvent awhile
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.– ulidtkoJan 14, 2016 at 13:58 -
First, open the terminal.
Then, type
cd ~
touch .sure
chmod 700 .sure
Next, open .sure and paste this inside.
#!/bin/bash --init-file
PS1='> '
alias y='
$1
exit
'
alias n='killall Terminal'
echo ''
echo 'Are you sure? Answer y or n.'
echo ''
After that, close the file.
~/.sure ; ENTER COMMAND HERE
This will give you a prompt of are you sure before continuing the command.
cd
andls
?