Niklas Rosencrantz Asked: 2018-04-29 13:28:33 +0800 CST2018-04-29 13:28:33 +0800 CST 2018-04-29 13:28:33 +0800 CST 如何在 bash shell 中引发语法错误 772 我的项目只有 20% 的测试覆盖率。https://coveralls.io/github/montao/opsh 我想为语法错误添加一个测试。我无法从简单的 oneliner shell 命令接收输出“语法错误”。你能帮我举一些例子吗? command-line testing bash 2 个回答 Voted Best Answer Chai T. Rex 2018-04-29T14:01:10+08:002018-04-29T14:01:10+08:00 ; 返回: bash: syntax error near unexpected token `;' dessert 2018-04-30T04:05:39+08:002018-04-30T04:05:39+08:00 您可以使用绝大多数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/DEFINITIONS和/RESERVED WORDS中获取了那些。
返回:
您可以使用绝大多数
bash
的保留字和控制运算符来引发语法错误——让我们做一个快速测试来获得一个(当然不完整!)列表:我从
man bash
/DEFINITIONS和/RESERVED WORDS中获取了那些。