help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] best practice with handling syntax errors


From: Greg Wooledge
Subject: Re: [Help-bash] best practice with handling syntax errors
Date: Thu, 8 Dec 2016 08:17:03 -0500
User-agent: Mutt/1.4.2.3i

On Thu, Dec 08, 2016 at 12:58:24PM +0100, Christof Warlich wrote:
> if [ -ee /etc/passwd ]; then
> 
>     : # note the syntax error above

Technically it's not a syntax error.  As far as the shell is concerned,
it's a well-formed command.

> line 2: [: -ee: unary operator expected
> script continues ...
> 
> While I'm fully aware that the reported syntax error does not directly
> come from the bash interpreter, but from the (builtin) test command,
> which in turn just returns a non-zero exit status due to its misuse,

Yes, this is exactly the same as if you had called an external utility
with an unrecognized option, and it exited with non-zero status.

> My question: Are there any best practice recommondations w.r.t. how to
> deal with situations like this?

Test the script.  See the error.  Fix the error.

Repeat testing until you see no more errors.

Only then allow other people to use the script.  (Of course, your testing
may not catch every error, but this is no different from any other
programming language.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]