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
.
Not the same, but idea that works anyway.
Output:
Again? Y/n N
Again? Y/n Anything
Again? Y/n 7
Again? Y/n &
Again? Y/n nsijf
$
Now only checks 1st character of $i read.
Here is my solution that using localised regex. So in german also "j" for "Ja" would be interpreted as yes.
First argument is the question, if the second argument is "y" than yes would be the default answer otherwise no would be the default answer. The return value is 0 if the answer was "yes" and 1 if the answer was "no".
Here is a basic usage
Here is roughly a snippet that you want. Let me find out how to forward the arguments.
Watch out for
yes | command name here
:)This may be a hack:
as in question In Unix / Bash, is "xargs -p" a good way to prompt for confirmation before running any command?
we can using
xargs
to do the job:of course, this will be set as an alias, like
hgpushrepo
Example:
Well, here's my version of
confirm
, modified from James' one:These changes are:
local
to prevent variable names from collidingread
use$2 $3 ...
to control its action, so you may use-n
and-t
read
exits unsuccessfully,echo
a line feed for beautyGit on Windows
only hasbash-3.1
and has notrue
orfalse
, so usereturn
instead. Of course, this is also compatible with bash-4.4 (the current one in Git for Windows).