如何在鱼壳中获得用户确认? [英] How to get user confirmation in fish shell?
本文介绍了如何在鱼壳中获得用户确认?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在鱼壳脚本中收集用户输入,特别是以下常见形式:
I'm trying to gather user input in a fish shellscript, particularly of the following oft-seen form:
This command will delete some files. Proceed (y/N)?
经过一番搜索,我仍然不知道如何干净地做到这一点.
After some searching around, I am still not sure how to do this cleanly.
这是在鱼身上做这件事的一种特殊方式吗?
Is these a special way of doing this in fish?
推荐答案
我所知道的最好方法是使用内置的 read
.如果你在多个地方使用它,你可以创建这个辅助函数:
The best way I know of is to use the builtin read
. If you are using this in multiple places you could create this helper function:
function read_confirm
while true
read -l -P 'Do you want to continue? [y/N] ' confirm
switch $confirm
case Y y
return 0
case '' N n
return 1
end
end
end
并在您的脚本/函数中像这样使用它:
and use it like this in your scripts/functions:
if read_confirm
echo 'Do stuff'
end
有关更多选项,请参阅文档:https://fishshell.com/docs/current/commands.html#read
See documentation for more options: https://fishshell.com/docs/current/commands.html#read
这篇关于如何在鱼壳中获得用户确认?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文