2

I only have 20 % test coverage for my project. https://coveralls.io/github/montao/opsh

I want to add a test for syntax error. I am unable to receive the output "syntax error" from a simple oneliner shell command. Could you help me with some example(s)?

0
5
;

returns:

bash: syntax error near unexpected token `;'
4
  • 1
    That's surprising to me because a) the ; looks so innocent and b) it is the command termination character (or whatever it is called exactly) so I had bet it is just a no-op. Cool.
    – PerlDuck
    Apr 29 '18 at 12:12
  • @PerlDuck You can provoke syntax errors with every kind of smiley, try :-), ;-) and ];->. Just as if they wanted it to be like that…
    – dessert
    Apr 29 '18 at 12:25
  • @PerlDuck sorry, I deleted the fork bomb, just in case.
    – terdon
    Apr 29 '18 at 12:28
  • @PerlDuck nah, I just happened to be reading this just after you posted.
    – terdon
    Apr 29 '18 at 12:31
5

You can use the vast majority of bash's reserved words and control operators to provoke a syntax error – let's do a quick test to get a (of course incomplete!) list:

$ 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 `]]'

I took those from man bash/DEFINITIONS and /RESERVED WORDS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.