使用 case 语句时 bash 语法错误
我有 bash 脚本,我在工作中经常使用它来自动化大型工作。我今天做了一些改变,但一切看起来都很好。脚本本身约有 1700 行长。脚本的第一部分一切都很好,并且很好地贯穿了所有用户输入和逻辑。然后它进入脚本的核心并在第 875 行停止工作(使用 bash -x 测试脚本以找到断点)。但是,它会出现以下错误:
script.sh: line 1341: syntax error near unexpected token `;;'
script.sh: line 1341: ` ;;'
第 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"
;;
由于我所做的事情的敏感性,我显然必须删除相当多的信息。我知道这可能会让帮助我的事情变得更加困难。但是,所有 VAR##="STR##" 都是带有字符串值的标准变量声明,没有什么特别的(没有变量替换等)。所有变量稍后都会在脚本中使用。 VERSION 的代码返回一个字符串值,该值在嵌套的 case
中使用。
在我今天进行更改之前,该脚本一直运行良好,但除了调整一些 STR 值之外,我确实没有触及此部分。我尝试在引号“”中设置 $VAR3
和 $VERSION
变量,以及用作案例的 STR 值。我尝试完全取出这个块,但它在下一个块上失败(STR1 具有不同的值,因此更改了变量声明)。我将其正在执行的操作输出到控制台,并在大多数函数之后检查错误。控制台上没有任何异常,错误日志中也没有任何异常。
任何帮助将不胜感激,我知道我要求很多。
顺便说一句,这里是第 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
再次感谢。
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 -x
to find the break point). However, it breaks with the following error:
script.sh: line 1341: syntax error near unexpected token `;;'
script.sh: line 1341: ` ;;'
Line 1341 is in the middle of a case
statement. The following code is the beginning of that block of code where it is breaking:
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
.
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 $VAR3
and $VERSION
variables 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.
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 -x
I could see the VAR2 variable get set, but the script breaks before the next for
loop starts.
## 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题可能出在第 875 行(或更早一点)和第 1341 行之间。可能是引用放错了位置,或者是一些不那么微妙的东西。如果没有这些行之间的所有原始材料,我们基本上不可能进行调试。
建议 1:使用“bash -n -v”运行,看看这是否能让您深入了解问题。
建议 2:将脚本分割成更容易管理的更小的部分 - 并且可以单独调试。我拥有的最大脚本(在我的 bin 目录中有 400 个脚本)来自 autoconf 套件 - 它们的长度不到 1100 行;第二大的是我的,750 行的脚本太大了。其次最大的脚本是 600 到 700 行 Perl(包括 Perl 文档)。
说了“缺少引号”,我看到靠近第 875 行的片段有:
第二个回显缺少闭合双引号。
您还提到进行更改,尽管还没有接近脚本中断的点。由于您的代码处于版本控制之下(您不会梦想在没有备份的情况下使用 1700 行脚本,不是吗?),您应该再次查看实际的更改。
或者甚至备份到以前的工作版本,然后再次进行更改,一次一项,小心翼翼,直到您明白为什么会破坏某些内容。
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.
Suggestion 1: run with 'bash -n -v' and see whether that gives you any insight into the problem.
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..n big. The next biggest scripts are between 600 and 700 lines of Perl (including Perl documentation).
Having said 'missing quote', I see that your fragment close to line 875 has:
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.
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.
在本节中,等号周围有空格:
将其去掉,就可以了(除非这是一个发布错误)。
You have spaces around your equal signs in this section:
Take those out and you may be OK (unless that's a posting error).