nop*_*ole 211 bash alias confirmation
在这个特殊情况下,我想在Bash中添加一个确认
Are you sure? [Y/n]
对于Mercurial来说hg push ssh://username@www.example.com//somepath/morepath
,它实际上是一个别名.是否有可以添加到别名的标准命令来实现它?
原因是hg push
并且hg out
听起来很相似,有时当我想要时hgoutrepo
,我可能会意外地输入hgpushrepo
(两者都是别名).
更新:如果它可以像是带有另一个命令的内置命令,例如:confirm && hg push ssh://...
那个很棒......只是一个可以请求a yes
或者no
继续其余命令的命令yes
.
Pau*_*ce. 307
这些是Hamish的答案更紧凑和多样化的形式.它们处理大写和小写字母的任何混合:
read -r -p "Are you sure? [y/N] " response
case "$response" in
[yY][eE][sS]|[yY])
do_something
;;
*)
do_something_else
;;
esac
Run Code Online (Sandbox Code Playgroud)
或者,对于Bash> =版本3.2:
read -r -p "Are you sure? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]
then
do_something
else
do_something_else
fi
Run Code Online (Sandbox Code Playgroud)
注意:如果$response
是空字符串,则会出错.要修复,只需添加引号:"$response"
.-始终使用变量双引号包含字符串(如:喜欢使用"$@"
代替$@
).
或者,Bash 4.x:
read -r -p "Are you sure? [y/N] " response
response=${response,,} # tolower
if [[ "$response" =~ ^(yes|y)$ ]]
...
Run Code Online (Sandbox Code Playgroud)
编辑:
为了响应您的编辑,以下是confirm
基于我的答案中的第一个版本创建和使用命令的方式(它与其他两个版本的工作方式类似):
confirm() {
# call with a prompt string or use a default
read -r -p "${1:-Are you sure? [y/N]} " response
case "$response" in
[yY][eE][sS]|[yY])
true
;;
*)
false
;;
esac
}
Run Code Online (Sandbox Code Playgroud)
要使用此功能:
confirm && hg push ssh://..
Run Code Online (Sandbox Code Playgroud)
要么
confirm "Would you really like to do a push?" && hg push ssh://..
Run Code Online (Sandbox Code Playgroud)
Ham*_*jan 19
这里大概是你想要的一个片段.让我找出如何转发论点.
read -p "Are you sure you want to continue? <y/N> " prompt
if [[ $prompt == "y" || $prompt == "Y" || $prompt == "yes" || $prompt == "Yes" ]]
then
# http://stackoverflow.com/questions/1537673/how-do-i-forward-parameters-to-other-command-in-bash-script
else
exit 0
fi
Run Code Online (Sandbox Code Playgroud)
小心yes | command name here
:)
bri*_*urg 12
使用回车可以轻松绕过确认,我发现连续提示输入有效是很有用的.
这是一个简单的功能.如果未收到Y | N,则"无效输入"显示为红色,并再次提示用户.
prompt_confirm() {
while true; do
read -r -n 1 -p "${1:-Continue?} [y/n]: " REPLY
case $REPLY in
[yY]) echo ; return 0 ;;
[nN]) echo ; return 1 ;;
*) printf " \033[31m %s \n\033[0m" "invalid input"
esac
done
}
# example usage
prompt_confirm "Overwrite File?" || exit 0
Run Code Online (Sandbox Code Playgroud)
您可以通过传递参数来更改默认提示
小智 11
为了避免显式检查'yes'的这些变体,可以将bash正则表达式运算符'=〜'与正则表达式一起使用:
read -p "Are you sure you want to continue? <y/N> " prompt
if [[ $prompt =~ [yY](es)* ]]
then
(etc...)
Run Code Online (Sandbox Code Playgroud)
测试用户输入是以'y'还是'Y'开头,后跟零或更多'es'.
这可能是一个hack:
在问题中的Unix / bash中,是“xargs的-p”的好办法确认提示的运行任何命令之前?
我们可以xargs
用来完成这项工作:
echo ssh://username@www.example.com//somepath/morepath | xargs -p hg push
Run Code Online (Sandbox Code Playgroud)
当然,这将被设置为别名,例如 hgpushrepo
例:
$ echo foo | xargs -p ls -l
ls -l foo?...y
-rw-r--r-- 1 mikelee staff 0 Nov 23 10:38 foo
$ echo foo | xargs -p ls -l
ls -l foo?...n
$
Run Code Online (Sandbox Code Playgroud)
这可能有点太短了,但对于我自己的私人使用来说,效果很好
read -n 1 -p "Push master upstream? [Y/n] " reply;
if [ "$reply" != "" ]; then echo; fi
if [ "$reply" = "${reply#[Nn]}" ]; then
git push upstream master
fi
Run Code Online (Sandbox Code Playgroud)
该read -n 1
只读取一个字符。无需按回车键。如果它不是“n”或“N”,则假定它是“Y”。只需按 Enter 也意味着 Y。
(至于真正的问题:将其设为 bash 脚本并将别名更改为指向该脚本而不是之前指向的内容)
归档时间: |
|
查看次数: |
159664 次 |
最近记录: |