bash 使用case语句时的bash语法错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3818763/
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
bash syntax error when using case statement
提问by desertwebdesigns
I have bash script that I use regularly in my job to automate a large job. I was making some changes today, but everything seemed fine. The script itself is about 1700 lines long. The first part of the script is all good and runs through all the user input and logic just fine. It then proceeds into the core of the script and stops working at exactly line 875 (tested the script with bash -xto find the break point). However, it breaks with the following error:
我有 bash 脚本,我经常在工作中使用它来自动化大型工作。我今天做了一些改变,但一切似乎都很好。脚本本身大约有 1700 行。脚本的第一部分都很好,可以很好地贯穿所有用户输入和逻辑。然后它进入脚本的核心并在第 875 行停止工作(测试脚本bash -x以找到断点)。但是,它因以下错误而中断:
script.sh: line 1341: syntax error near unexpected token `;;'
script.sh: line 1341: ` ;;'
Line 1341 is in the middle of a casestatement. The following code is the beginning of that block of code where it is breaking:
第 1341 行位于case语句的中间。以下代码是该代码块中断的开头:
if [[ $VAR1 = "TRUE" && $VAR2 = "VAL2" ]]; then
VERSION=`XXXXXXXXXXXXXXXX`
## Set variables based on location $VAR3
case $VAR3 in
STR1 )
case $VERSION in
STR2 )
VAR4 = "STR5"
VAR5 = "STR6"
VAR6 = "STR7"
VAR7 = "STR8"
Line 1341 ---> ;;
STR3 )
VAR4="STR9"
VAR5="STR10"
VAR6="STR11"
VAR7="STR12"
;;
STR4 )
VAR4="STR13"
VAR5="STR14"
VAR6="STR15"
VAR7="STR16"
;;
esac
VAR8="STR17"
VAR9="STR18"
VAR10=1
VAR11="STR19"
;;
Because of the sensitive nature of what I do, I obviously had to remove quite a bit of information. I know this may make things more difficult to help me with. However, all VAR##="STR##" are standard variable declarations with string values, nothing special (no variable substitution, etc). All the variables are used later in the script. The code for VERSION returns a string value, which is used in the nested case.
由于我所做工作的敏感性,我显然不得不删除相当多的信息。我知道这可能会使帮助我的事情变得更加困难。但是,所有 VAR##="STR##" 都是带有字符串值的标准变量声明,没什么特别的(没有变量替换等)。所有变量稍后在脚本中使用。VERSION 的代码返回一个字符串值,该值用于嵌套的case.
The script was working fine up until my changes today, but I really didn't touch this section, with the exception of tweaking some of the STR values. I tried setting $VAR3and $VERSIONvariables in quotes "", as well as the STR values used as the cases. I tried taking out this block entirely, only to have it fail on the next block (STR1 has a different value thus change the variable declarations). I have it output to the console what it is doing as well as checks for errors after most functions. There is nothing out of the ordinary on the console and nothing in the error log.
在我今天进行更改之前,该脚本一直运行良好,但我真的没有触及这一部分,除了调整了一些 STR 值。我尝试在引号“”中设置$VAR3和$VERSION变量,以及用作案例的 STR 值。我尝试完全取出这个块,只是让它在下一个块上失败(STR1 具有不同的值,因此更改了变量声明)。我将它输出到控制台它正在做什么以及在大多数功能之后检查错误。控制台上没有任何异常,错误日志中也没有任何异常。
Any help would be appreciated, and I know I'm asking a lot.
任何帮助将不胜感激,我知道我问了很多。
By the way here is the code around line 875 where the script stops running (no errors generated based on the code here). Again, with bash -xI could see the VAR2 variable get set, but the script breaks before the next forloop starts.
顺便说一下,这里是脚本停止运行的第 875 行附近的代码(没有根据此处的代码生成错误)。同样,bash -x我可以看到 VAR2 变量被设置,但脚本在下一个for循环开始之前中断。
## Create file ##
echo 'Creating files . . . '
j=0
p=1111
if [ $VAR1 = "TRUE" ]
then
VAR2=1
else
VAR2=2
fi
for i in `seq 1 $HOWMANY`; do <----Line 875
echo -n "Creating file . . . "
echo "XXXXXXXXXXX
Thanks again.
再次感谢。
回答by Jonathan Leffler
The problem is likely somewhere between line 875 (or a bit earlier) and line 1341. It maybe a misplaced quote or something less subtle. It will be essentially impossible for us to debug without all the original material between those lines.
问题可能出现在第 875 行(或更早一点)和第 1341 行之间。这可能是一个错位的引用或不那么微妙的东西。如果没有这些行之间的所有原始材料,我们基本上不可能进行调试。
Suggestion 1: run with 'bash -n -v' and see whether that gives you any insight into the problem.
建议 1:使用 'bash -n -v' 运行,看看是否能让您深入了解问题。
Suggestion 2: split the script into smaller pieces that are more easily managed - and that can be separately debugged. The biggest scripts I have (out of 400 in my bin directory) are from the autoconf suite - they weigh in at just under 1100 lines; the next biggest is mine, and the 750 line script is too d..nbig. The next biggest scripts are between 600 and 700 lines of Perl (including Perl documentation).
建议 2:将脚本拆分为更易于管理的更小部分 - 并且可以单独调试。我拥有的最大的脚本(我的 bin 目录中有 400 个)来自 autoconf 套件——它们的重量不到 1100 行;接下来最大的是我的,750行脚本太d..n大。下一个最大的脚本是 600 到 700 行 Perl(包括 Perl 文档)。
Having said 'missing quote', I see that your fragment close to line 875 has:
说完“缺少报价”,我看到您靠近第 875 行的片段有:
echo -n "Creating file . . . "
echo "XXXXXXXXXXX
with a missing close double quote from the second echo.
缺少第二个回声中的关闭双引号。
You also mentioned making changes, albeit not close to the point where the script breaks. Since you have the code under version control (you wouldn't dream of playing with a 1700 line script without backups, would you?), you should look at the actual changes again.
您还提到了进行更改,尽管还没有接近脚本中断的程度。由于您的代码处于版本控制之下(您不会梦想在没有备份的情况下使用 1700 行脚本,是吗?),您应该再次查看实际更改。
Or even back up to the previous working version, and make the changes again, one at a time, carefully, until you see why you broke something.
或者甚至备份到以前的工作版本,并再次进行更改,一次一个,小心翼翼,直到您明白为什么会破坏某些东西。
回答by Paused until further notice.
You have spaces around your equal signs in this section:
在本节中,您的等号周围有空格:
case $VERSION in
STR2 )
VAR4 = "STR5"
VAR5 = "STR6"
VAR6 = "STR7"
VAR7 = "STR8"
Take those out and you may be OK (unless that's a posting error).
把这些去掉,你可能会没事(除非这是一个发布错误)。

