Bash Shell 脚本错误:意外标记附近的语法错误 '{

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1794594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 21:25:42  来源:igfitidea点击:

Bash Shell Script error: syntax error near unexpected token '{

bashshell

提问by Julian

Below is the snippet of code that keeps giving me the problem. Could someone explain to me why my code isn't working.

以下是不断给我带来问题的代码片段。有人可以向我解释为什么我的代码不起作用。

# Shell Version of Login Menu

Administrator ()
{
    clear
    ./Administrator.sh
}

# ------ Student User Menu ------
Student_Menu()
{
    clear
    ./studentMenu.sh
}

# ------ Borrow Menu ------
Borrow_Menu()
{
    clear
    ./BorrowBookMenu.sh
}

# ============================================
# ------ Main Menu Function Definition -------
# ============================================

menu()
{
echo ""
echo "1. Administrator"
echo ""
echo "2. Student user"
echo ""
echo "3. Guest"
echo ""
echo "4. Exit"
echo ""
echo "Enter Choice: "
read userChoice

if ["$userChoice" == "1"]
then 
    Administrator
fi

if ["$userChoice" == "2"]
then
    Student_Menu
fi


if ["$userChoice" == "3"]
then
    Borrow_Menu
fi

if ["$userChoice" == "4"]
then
    echo "GOODBYE"
    sleep
    exit 0
fi

echo
echo ...Invalid Choice...
echo 
sleep
clear
menu
}

# Call to Main Menu Function
menu

回答by Paused until further notice.

Bash has a menu feature called "select":

Bash 有一个名为“select”的菜单功能:

#!/bin/bash
choices=(Administrator "Student user" Guest Exit)
savePS3="$PS3"
PS3="Enter choice: "
while :
do
    select choice in "${choices[@]}"
    do
        case $REPLY in
            1) clear
               ./Administrator.sh
               break;;
            2) clear
               ./studentMenu.sh
               break;;
            3) clear
               ./BorrowBookMenu.sh
               break;;
            4) echo "GOODBYE"
               break 2;;
            *) echo
               echo "Invalid choice"
               echo
               break;;
        esac
    done
done
PS3="$savePS3"

This is what the menu looks like:

这是菜单的样子:

1) Administrator
2) Student user
3) Guest
4) Exit
Enter choice: 3

回答by Idelic

You have an error in

你有一个错误

if ["$userChoice" == "1"]
then 
    Administrator
fi

and the other similar ifstatements. You need to add a space after the [and a space before the ]. In Bourne-like shells, [is not part of the shell's grammar for conditionals, but a regular command which expects its last argument to be ].

以及其他类似的if声明。您需要在 之后添加一个空格,在[之前添加一个空格]。在类似 Bourne 的 shell 中,[它不是 shell 的条件语法的一部分,而是一个常规命令,它的最后一个参数是].

回答by ghostdog74

do your menu like this

像这样做你的菜单

# Shell Version of Login Menu

Administrator ()
{
#     clear
    ./Administrator.sh
}

# ------ Student User Menu ------
Student_Menu()
{
#     clear
    ./studentMenu.sh
}

# ------ Borrow Menu ------
Borrow_Menu()
{
#     clear
    ./BorrowBookMenu.sh
}

# ============================================
# ------ Main Menu Function Definition -------
# ============================================

menu()
{
    while true
    do
        echo ""
        echo "1. Administrator"
        echo ""
        echo "2. Student user"
        echo ""
        echo "3. Guest"
        echo ""
        echo "4. Exit"
        echo ""
        echo "Enter Choice: "
        read userChoice
        case "$userChoice" in
            "1") Administrator ;;
            "2") Student_Menu ;;
            "3") Borrow_Menu ;;
            "4") echo "bye" ;exit ;;
             *) echo "Invalid";;
        esac
    done
}

menu

回答by Jonathan Leffler

As now formatted, the code works 'OK' with bash under Cygwin.

按照现在的格式,代码在 Cygwin 下与 bash 一起工作“OK”。

However, the logic in the menu function should be improved - using 'elif' to select alternative actions, and 'else' to deal with errors.

但是,菜单功能中的逻辑应该改进 - 使用 'elif' 选择替代操作,使用 'else' 处理错误。

if [ "$userChoice" = 1 ]
then Administrator
elif [ "$userChoice" = 2 ]
then Student_User
elif [ "$userChoice" = 3 ]
then Borrow_Menu
elif [ "$userChoice" = 4 ]
then echo Goodbye; sleep 1; exit 0
else echo "Unrecognized choice $userChoice"; sleep 1; clear
fi

And then you can iterate the menu somewhere...

然后你可以在某个地方迭代菜单......