1

EDIT: This question was marked as duplicate. While this might be true on a larger scale, the answer provided by Kusalananda gave me a better understanding of my options.

If a syntax error inside a shell script happens, the interpretation of the script isn't necessarily terminated but simply continues at the next line of the script. Is there a way to catch the number of those syntax errors while the script is interpreted?

I would really like to have something like a compiler report, stating the number of errors and warnings after the compiling has finished. Just for linux shell.

cas
  • 78,579
YokeM
  • 13
  • 3
  • 2
    It's not exactly what you asked for, but you may want to consider using set -e at the start of your script so that the script does terminate when an error happens, which is probably a better way of debugging/testing your script than just "counting the number of errors". – Malte Skoruppa Jul 01 '16 at 10:29
  • 1
    @ThomasDickey Not exactly a duplicate, IMHO. – Kusalananda Jul 01 '16 at 10:44
  • @ThomasDickey I still not think it is a duplicate... – Kusalananda Jul 01 '16 at 11:47
  • I have marked the question as dublication since the functions I needed are in fact descriped by POSIX. I will leave a note stating, that the answer providet by Kusalananda gives a better understanding of the solution though. – YokeM Jul 01 '16 at 12:07

1 Answers1

2

Yes. Most shells have the facility to terminate the current script if any of the commands therein generates an error. This is usually done with

set -e

and may be turned off with

set +e

EDIT: I misunderstood the question. Bash and other shells also has the -n option that does not execute the script, but checks it for syntax errors.

$ bash -n script.sh

Both the -e and -n options are described by POSIX. For -n, the standard says:

The shell shall read commands but does not execute them; this can be used to check for shell script syntax errors. An interactive shell may ignore this option.

CLARIFICATION: This answer is regarding syntax checking, not checking for POSIX conformance. A shell that extends POSIX with its own semantics may still check the syntax (with -n) of a script written using the extended semantics. This does not say anything about the POSIX conformity of the shell nor script, only whether the shell script was syntactically correct for the shell in question, which was the question.

Kusalananda
  • 333,661
  • Thank you, first time I read the answers of the linked question I had a hard time understanding while mine was a dublication. After reading your answer, it makes more sense now. – YokeM Jul 01 '16 at 11:46