鱼不会从带有“ return”的函数中返回指定的状态代码

时间:2019-01-29 14:53:21

标签: scripting fish

我在其他功能中使用此简单功能进行可重复使用的用户确认:

function read_confirm --description 'Ask the user for confirmation' --argument prompt
    if test -z "$prompt"
        set prompt "Do you want to continue? [Y/n]: "
    end 

    while true
        read -l -P $prompt confirm
        switch $confirm
        case N n
            return 1
        case '' Y y
            return 0
        end
    end
end

这在以前的版本中很好用,但是现在当我安装版本 3.0.0 时,看来我总是得到状态码0。

 ~ read_confirm
Do you want to continue? [Y/n]:
 ~ echo $status
0
 ~ read_confirm
Do you want to continue? [Y/n]: y
 ~ echo $status
0
 ~ read_confirm
Do you want to continue? [Y/n]: Y
 ~ echo $status
0
 ~ read_confirm
Do you want to continue? [Y/n]: n
 ~ echo $status
0
 ~ read_confirm
Do you want to continue? [Y/n]: N
 ~ echo $status
0
 ~

起初我以为case语句不起作用,但是正确读取了用户输入,它没有前导'\ n',它将转到'N n'大小写,函数将正确地“完成” ,但是“返回1”总是将状态设置为0,我不知道为什么。鱼的API是否发生了变化?我是在错误地使用它,需要重写吗?

更新: 创建了一个错误报告,因为这是对以前版本的改进 https://github.com/fish-shell/fish-shell/issues/5600

感谢您的帮助, 斯蒂芬

1 个答案:

答案 0 :(得分:2)

这是一个已知的错误,将在下一个dot版本中修复。参见https://github.com/fish-shell/fish-shell/issues/5513