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

时间:2009-11-25 04:19:37

标签: bash shell

以下是一直给我提问的代码片段。有人可以向我解释为什么我的代码无效。

# 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

4 个答案:

答案 0 :(得分:4)

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"

这就是菜单的样子:

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

答案 1 :(得分:3)

中有错误
if ["$userChoice" == "1"]
then 
    Administrator
fi

和其他类似的if语句。您需要在[之后添加一个空格,并在]之前添加一个空格。在类似Bourne的shell中,[不是shell的条件语法的一部分,而是一个常规命令,它期望它的最后一个参数为]

答案 2 :(得分:2)

像这样做你的菜单

# 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

答案 3 :(得分:1)

按照现在的格式化,代码在Cygwin下使用bash运行'OK'。

但是,应该改进菜单功能中的逻辑 - 使用'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

然后你可以在某处迭代菜单......