【问题标题】:Bash dialog cancel button is returning with exit cod 0; so is escape keyBash 对话框取消按钮正在返回,退出代码为 0;转义键也是如此
【发布时间】:2020-05-29 16:06:19
【问题描述】:

我创建了一个简单的菜单对话框,但取消按钮和退出键返回 0 作为退出代码。我在这里做错了什么?

exec 3>&1;
selection=$(dialog \
    --title "Main Menu" \
    --backtitle "$BACK_TITLE" \
    --menu "Choose an option to configure. Up and down arrows change selection. Use the Enter key to make a selection." "$HEIGHT" "$WIDTH" 5 \
    0 "Select Directory" \
    1 "Select File Extension" \
    2 "Options" \
    3 "Execute with current config" \
    4 "Exit" \
    2>&1 1>&3);
exec 3>&-;
exitStatus=$?
echo "selected: $selection exit code: $exitStatus"

当我按下退出键或取消按钮时会产生上述情况。

selected: exit code: 0

【问题讨论】:

    标签: bash dialog


    【解决方案1】:

    您正在保存上一个exec 命令的退出状态,而不是dialog。将exitStatus=$? 紧跟在selection=... 之后。

    exec 3>&1;
    selection=$(dialog \
        --title "Main Menu" \
        --backtitle "$BACK_TITLE" \
        --menu "Choose an option to configure. Up and down arrows change selection. Use the Enter key to make a selection." "$HEIGHT" "$WIDTH" 5 \
        0 "Select Directory" \
        1 "Select File Extension" \
        2 "Options" \
        3 "Execute with current config" \
        4 "Exit" \
        2>&1 1>&3);
    exitStatus=$?
    exec 3>&-;
    echo "selected: $selection exit code: $exitStatus"

    【讨论】:

    • 啊废话!我知道这是一件愚蠢的事情。不想承认我已经尝试了多久。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-10
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2013-08-09
    相关资源
    最近更新 更多