繁体   English   中英

需要Perl单行正则表达式来摆脱shell脚本文件中的if条件语法错误

[英]Need Perl one-liner regex to get rid of the if condition syntax error from a shell script file

我在使用时检测到shell脚本文件时出现语法错误

sh -n test.sh

test.sh: line 255: syntax error near unexpected token `then'
test.sh: line 255: `      if [[ $type == "SA" ]] then'
test.sh: line 387: syntax error near unexpected token `then'
test.sh: line 387: `      if (( MM == 1 )) then'

显然,这错误是由于错位then在后if条件,因为它是存在于同一直线上,而不; 这个错误发生在很多地方。

手动更正每行代码是非常繁琐的,因为这个shell脚本包含近1000行。 所以,为了纠正if condition ,我已经制定了这个Perl命令

perl -pi -e 's/(?<=\)) then/$1; then/' test.sh
perl -pi -e 's/(?<=\]) then/$1; then/' test.sh

我想通过考虑以下事项来改进上面的Perl单行程序

  • 我想结合两个替换来形成一个命令。 也就是说,考虑shell脚本中的'if [[]]'和'if(())'

  • 有可能是闭合之间的多个空格)]then

  • 以后可能会有空白then

  • 我需要替换它; 或换行符"\\n"以修复此shell语法错误

  • 除了我正在使用的问题之外,有没有更好的方法来解决这个问题?

因为你没有捕获任何东西所以不需要使用$1

 's/(?<=\)) then/; then/'

使用| 介绍替代品:

's/(?<=\)|\]) then/; then/'

使用\\s表示空格和+量词来标记重复:

's/(?<=\)|\])\s+then/; then/'

使用/x可读性:

's/ (?<= \) | \] ) \s+ then/; then/x'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM