2009-11-25 110 views
1

以下是一直不断给我提出问题的代码片段。有人可以向我解释为什么我的代码无法正常工作。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 
+0

嘿朱利安。我建议你把这个剧本分成小块。单独运行每个块,直到找出错误的来源。 – jkndrkn

回答

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 
+0

正是我在找什么,但为什么我的菜单抛出这么多的错误和怪异的声明? – Julian

+0

另一件事,你为什么要评论“清晰”命令? – Julian

+0

,因为我想看看会发生什么!你不必在开发过程中使用它。 – ghostdog74

1

正如现在格式化的,代码在Cygwin下用bash运行'OK'。

然而,在菜单功能的逻辑应该改进 - 使用“ELIF”选择替代行动,和“其他”处理错误。

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 

然后你就可以遍历某个菜单...

3

你的菜单中你有

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

和其他类似的错误if声明。您需要在[之后加一个空格,并在]之前加一个空格。在类似Bourne外壳,[不是shell的文法条件句的一部分,但预计其最后一个参数是]常规命令。

4

Bash有所谓的 “选择” 菜单功能:

#!/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 
相关问题