第56行语法错误:意外的文件结束

我输入了mychoice.sh bash示例的代码,我不断收到语法错误:意外的文件结束。

我错过了什么吗?

mychoice.sh

 #!/bin/bash E='echo -e';e='echo -en';trap ";exit" 2 ESC=$( $e "\e") TPUT(){ $e "\e[${1};${2}H";} CLEAR(){ $e "\ec";} CIVIS(){ $e "\e[25l"} DRAW(){ $e "\e%\e(0";} WRITE(){ $e "\e(B";} MARK(){ $e "\e[7m";} UNMARK(){ $e "\e[27m";} R(){ CLEAR ;stty sane;$e "\ec\e[37;44m[J";}; HEAD(){ DRAW for each in $(seq 1 13);do $E " xx" done WRITE;MARK;TPUT; 1 5 $E "BASH SELECTION MENU ";UNMARK;} i=0; CLEAR; CIVIS;NULL=/dev/null FOOT(){ MARK;TPUT 13 5 printf "ENTER - SELECT,NEXT ";UNMARK;} ARROW(){ read -s -n3 key 2>/dev/null >&2 if [[$key = $ESC[A ]];then echo up;fi if [[$key = $ESC[B ]];then echo dn;fi;} M0(){ TPUT 4 20; $e "Login info";} M1(){ TPUT 5 20; $e "Network";} M2(){ TPUT 6 20; $e "Disk";} M3(){ TPUT 7 20; $e "Routing";} M4(){ TPUT 8 20; $e "Time";} M5(){ TPUT 9 20; $e "About ";} M6(){ TPUT 10 20; $e "Exit ";} LM=6 MENU(){ for each in $(seq 0 $LM);do M${each};done;} POS(){ if [[ $cur == up ]];then ((i--));fi if [[ $cur == dn ]];then ((i++));fi if [[ $i -lt 0 ]];then i=$LM;fi if [[ $i -gt $LM ]];then i=0;fi;} REFRESH(){ after=$((i+1)); before=$((i-!)) if [[ $before -lt 0 ]];then before=$LM;fi if [[ $after -gt 0 ]];then after=0;fi if [[ $j -lt $i ]];then UNMARK;M$before;else UNMARK;M$after;fi if [[ $after -eq 0 ]] || [$before -eq $LM ];then UNMARK; M$before; M$after;fi;j=$i;UNMARK;M$before;M$after;} INIT(){ R;HEAD;FOOT;MENU;} SC(){ REFRESH;MARK;$S;$b;cur='ARROW';} ES(){ MARK;$e "ENTER = main menu ";$b;read;INIT;};INIT while [[ "$0" != " " ]]; do case $i in 0) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS(w )\n";ES;fi;; 1) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS(ifconfig )\n";ES;fi;; 2) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS(df -h )\n";ES;fi;; 3) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS(route -n )\n";ES;fi;; 4) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS(date )\n";ES;fi;; 5) S=M2;SC;if [[ $cur == "" ]];then R;$e "\nS($e by oTo )\n";ES;fi;; 6) S=M2;SC;if [[ $cur == "" ]];then R;exit 0;fi;; esac;POS;done; 

您可以通过shell检查器运行脚本:

 $ shellcheck myscript Line 7: CIVIS(){ $e "\e[25l"} ^-- SC1009: The mentioned parser error was in this function. ^-- SC1073: Couldn't parse this brace group. ^-- SC1083: This } is literal. Check expression (missing ;/\n?) or quote it. Line 23: if [[$key = $ESC[A ]];then echo up;fi ^-- SC1035: You need a space after the [[ and before the ]]. Line 24: if [[$key = $ESC[B ]];then echo dn;fi;} ^-- SC1035: You need a space after the [[ and before the ]]. Line 42: if [[ $after -eq 0 ]] || [$before -eq $LM ];then ^-- SC1035: You need a space after the [ and before the ]. Line 56: ^-- SC1056: Expected a '}'. If you have one, try a ; or \n in front of it. ^-- SC1072: Missing '}'. Fix any mentioned problems and try again. $ 

由于shell检查器告诉我们您需要更改第7行:

 CIVIS(){ $e "\e[25l"} 

至:

 CIVIS(){ $e "\e[25l";} 

几点评论:

  • 不要使用变量来调用命令,如E='echo -e';e='echo -en' ; 使用函数而不是至少有些描述性的名称。 或者,如果需要反斜杠解释,请使用printf
  • test命令之后添加空格, [[[ (是的,它不是操作符,它实际上是一个名为“test”的命令 , [只是它的简写,这就是你需要空格的原因)
  • 使用小写字母表示函数和变量名称(这样可以避免与标准环境变量混淆)
  • 除了WinEunuuchs2Unix已经发布的内容之外,shellcheck还注意到了一件事:

     SC(){ REFRESH;MARK;$S;$b;cur='ARROW';} ^-- SC2154: b is referenced but not assigned. 

    此变量$b未分配,因此,如果不使用它或者为其分配一些值,请考虑删除它。