In this particular case, I'd like to add a confirm in Bash for
Are you sure? [Y/n]
for Mercurial's hg push ssh://username@www.example.com//somepath/morepath
, which is actually an alias. Is there a standard command that can be added to the alias to achieve it?
The reason is that hg push
and hg out
can sound similar and sometimes when I want hgoutrepo
, I may accidentlly type hgpushrepo
(both are aliases).
Update: if it can be something like a built-in command with another command, such as: confirm && hg push ssh://...
that'd be great... just a command that can ask for a yes
or no
and continue with the rest if yes
.
To avoid explicitly checking for these variants of 'yes' you could use the bash regular expression operator '=~' with a regular expression:
That tests whether the user input starts with 'y' or 'Y' and is followed by zero or more 'es's.
Add the following to your /etc/bashrc file. This script adds a resident "function" instead of an alias called "confirm".
Late to the game, but I created yet another variant of the
confirm
functions of previous answers:To use it:
Features:
Bugs:
echo -en
works withbash
but might fail in your shellecho
orxargs
This may be a little too short, but for my own private use, it works great
The
read -n 1
just reads one character. If it's not a lowercase 'n', it is assumed to be a 'Y'.(as for the real question: make that a bash script and change your alias to point to that script instead of what is was pointing to before)