我已经输入了这个 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 " x x"
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;
答案1
您可以通过以下方式运行脚本外壳检查器:
$ 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";}
答案2
一些评论:
- 不要使用变量来调用命令,例如
E='echo -e';e='echo -en'
;使用至少具有一定描述性名称的函数。printf
如果您需要反斜杠解释,也可以使用 test
在命令后添加空格,[
并且[[
(是的,它不是一个运算符,它是实际上是一个名为“测试”的命令并且[
只是它的简写,这就是为什么需要空格)- 使用小写字母作为函数和变量名称(这样可以避免与标准环境变量混淆)
除了 WinEunuuchs2Unix 已经发布的内容之外,shellcheck 还指出了另一件事:
SC(){ REFRESH;MARK;$S;$b;cur='ARROW';} ^-- SC2154: b is referenced but not assigned.
此变量
$b
未分配,因此,如果不使用它,请考虑删除它,或者为其分配一些值。