如何在 bash shell 中引发语法错误

如何在 bash shell 中引发语法错误

我的项目测试覆盖率只有 20%。https://coveralls.io/github/montao/opsh

我想添加一个语法错误测试。我无法从简单的一行 shell 命令中收到“语法错误”输出。您能帮我举几个例子吗?

答案1

;

返回:

bash: syntax error near unexpected token `;'

答案2

您可以使用绝大多数的bash保留字和控制运算符来引发语法错误——让我们做一个快速测试来获得一个(当然不完整的!)列表:

$ tabs 10
  for i in '|' '&' ';' '(' ')' '<' '>' '||' '&&' \
           ';;' '|&' 'case' 'coproc' 'do' 'done' \
           'elif' 'else' 'esac' 'fi' 'for' 'function' \
           'if' 'in' 'select' 'then' 'until' 'while' \
           '{' '}' '[[' ']]';
  do
    printf "%s\t" "$i"; bash -c $i |& sed '2d;s/.*://'
  done
|          syntax error near unexpected token `|'                                                                                                      
&          syntax error near unexpected token `&'
;          syntax error near unexpected token `;'
(          unexpected end of file
)          syntax error near unexpected token `)'
<          syntax error near unexpected token `newline'
>          syntax error near unexpected token `newline'
||         syntax error near unexpected token `||'
&&         syntax error near unexpected token `&&'
;;         syntax error near unexpected token `;;'
|&         syntax error near unexpected token `|&'
case       syntax error near unexpected token `newline'
coproc     syntax error near unexpected token `newline'
do         syntax error near unexpected token `do'
done       syntax error near unexpected token `done'
elif       syntax error near unexpected token `elif'
else       syntax error near unexpected token `else'
esac       syntax error near unexpected token `esac'
fi         syntax error near unexpected token `fi'
for        syntax error near unexpected token `newline'
function   syntax error near unexpected token `newline'
if         unexpected end of file
in         syntax error near unexpected token `in'
select     syntax error near unexpected token `newline'
then       syntax error near unexpected token `then'
until      unexpected end of file
while      unexpected end of file
{          unexpected end of file
}          syntax error near unexpected token `}'
[[         unexpected token `EOF' in conditional command
]]         syntax error near unexpected token `]]'

我从man bash/定义/保留字

相关内容