【问题标题】:bash script case statements not working using dialogbash脚本案例语句无法使用对话框
【发布时间】:2019-04-11 17:23:04
【问题描述】:

我最近一直在尝试对话和 bash 脚本。我创建了一个包含三个选项的对话框菜单:用户、密码和组,然后还有一个退出选项。我试图在 case 语句中为每一个运行函数,但它似乎总是落入最后一条语句并输出 echo "Something Else..." 语句,无论我选择哪个选项而不是说运行相关函数中的 echo 语句,例如echo "进入用户子菜单"

我已经尝试在调试时运行它:

bash -x myscript

我得到以下输出:

+ choice='Users
Error: Expected a box option.
Use --help to list options.'
+ '[' 'Users
Error: Expected a box option.
Use --help to list options.' '!=' ' Quit ']'
+ case $choice in
+ echo 'Something else.  Where Am I?'
Something else. Where Am I?
+ exit 0

我仍在试图弄清楚“预期的框选项”是什么意思,这听起来与对话框有关,但我也想知道我的 if 语句或 bash 中的 case 语句是否有问题。

我的脚本代码:

#!/bin/bash

function quit {
    exit 0
}

function menu {

choice=$(dialog --backtitle "Rob Graves's Bash Scripting" \
--title "Main Menu"  --menu "Choose one" 30 50 4 "Users" \
"- Do something with users" "Passwords"\
 "- Do stuff with passwords"  "Groups" "- Do things with groups" \
 "Quit" "- Exit to desktop" --clear --nocancel 3>&1 1>&2 2>&3)

    if [ "$choice" != "Quit" ]; then

        case $choice in 
            Users)
                users                   #calls users function
                ;;
            Passwords)
                passwords               #calls passwords function
                ;;
            Groups)
                groups                  #calls groups function
                ;;
            *)
                echo "Something else.  Where Am I?"
                ;;
        esac

    else
        echo "Quitting..."
        quit
    fi

}

function users {
    echo "Entering Users sub-menu"
}

function passwords {
    echo "Entering Passwords sub-menu "
}

function groups {
    echo "Entering Groups sub-menu"

menu

exit 0

【问题讨论】:

  • 注意——为对话框提供 GUI 的快速插入式替换是 xdialog(命令是 Xdialog)。脚本的另一种选择。

标签: bash


【解决方案1】:

您的直接选项似乎是dialog 命令确实 像您提到的最后的选项--clear--nocancel 选项。重新排序似乎可以正常工作

choice=$(dialog --backtitle "Rob Graves's Bash Scripting" \
                --title     "Main Menu" \
                --clear \
                --nocancel \
                --menu  "Choose one" 30 50 4 \
                "Users"     "- Do something with users" \
                "Passwords" "- Do stuff with passwords" \
                "Groups"    "- Do things with groups" \
                "Quit"      "- Exit to desktop" 3>&1 1>&2 2>&3)

另外,最好始终引用您的 case 选项字符串,如下所示

case "$choice" in
    "Users")
        users                   #calls users function
        ;;
    "Passwords")
        passwords               #calls passwords function
        ;;
    "Groups")
        groups                  #calls groups function
        ;;
    *)
        echo "Something else.  Where Am I?"
esac

还请记住,您还可以在dialog 菜单和case 菜单中将"Users" 的选项添加为"1 - Users",如下所示。

case 声明中为

case "$choice" in
    "1 - Users")

另请注意,命令users(1)groups(1) 是作为GNU bash 的一部分提供的标准命令,并且对函数使用相同的名称可能会带来不确定性。始终选择明确的名称。

请记住在脚本失败的情况下使用非零退出代码退出脚本。例如在上面的默认情况下,记得添加一个exit 1,以便它添加另一种调试工具来查看,当脚本异常退出时,运行预期的序列流。

    *)
        echo "Something else.  Where Am I?"
        exit 1
        ;;

当它被命中并且你的脚本退出并执行echo $? 时会显示返回的代码。

还要从脚本中的函数定义中删除非标准的function 关键字。只要脚本在 bash shell 中运行就应该没问题,在纯 POSIX 的 shell 上,可能无法识别关键字。

您还应该使用 #!/usr/bin/env bash 来表示portability:不同的*nixes 将bash 放在不同的位置,使用/usr/bin/env 是运行第一个bash 的解决方法PATH

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-11
    • 2019-07-21
    • 2013-09-23
    • 2019-10-16
    • 2014-06-19
    • 2014-05-26
    • 2019-08-15
    • 1970-01-01
    相关资源
    最近更新 更多