Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to syntax check portable POSIX shell scripts? [duplicate]

The following shell script executes well when provided /bin/true for the first argument, but may otherwise fail with a syntax error during execution!

#!/bin/sh
if $1 ; then exit; fi
/tmp/asdf <<< ASDF # Something with syntax error in POSIX

Surely some syntax errors (if not all?) can be avoided by static checking? How do I statically check whether a given Shell Command Language script is syntactically valid?

EDIT: Checking for syntax errors in Bash scripts has been answered in this question.

EDIT #2: Note that Bash fails to properly check whether the syntax adheres to POSIX even when executed with the +B and --posix flags in addition to -n.

like image 454
mceo Avatar asked Apr 27 '16 10:04

mceo


People also ask

How do I check if a shell script is POSIX compatible?

You can use ShellCheck (GitHub) as a linter for your shell scripts. There is also an online version. To detect POSIX compatibility issues (e.g. SC2039 ), the shebang line of your shell script should be #!/bin/sh. You can also pass --shell=sh to shellcheck.

How to check if a variable is empty or not in POSIX?

Reference syntax and commands for POSIX shell scripting. For use in [ ] if [ ]; then and test. Note: When writing a bash or zsh script, use [ [ instead of POSIX [. Use these to check if a variable is empty or non-empty. If the length of string is non-zero. If the length of string is zero. Equal to. Not equal to.

What is POSIXLY_CORRECT in Bash?

If it is set while the shell is running, bash enables posix mode, as if the command set -o posix had been executed. Many other GNU utilities will also honor POSIXLY_CORRECT, so if you're on a system with predominantly GNU tools (e.g. most Linux systems), this is a good start if your goal is POSIX conformance. Show activity on this post.

How do I make Bash run in POSIX-compliant mode?

Bash will run in POSIX-compliant mode if the POSIXLY_CORRECT environment variable is set. From the manpage: POSIXLY_CORRECT If this variable is in the environment when bash starts, the shell enters posix mode before reading the startup files, as if the --posix invocation option had been supplied.


2 Answers

All POSIX-compatible Shell Command Language shells support the set -n built-in which can be used to check the syntax of the script. Therefore it is possible to prepend

set -n

to your code to syntax check it. Note also that standard sh utility is also required to support a command-line -n flag, which has equivalent semantics to using set -n. Bash and possibly other shells also support this command-line flag. Therefore you can simply run the following to syntax check your script:

sh -n yourScriptFilename.sh

WARNING: This does not give you a guarantee that the script has fully POSIX compatible syntax. For example, Bash allows bashisms (e.g. arrays and c{a,u}t) to go unnoticed even when using the --posix (and/or +B) command line option in addition to -n when invoked as sh. Other shells might have similar issues.

like image 80
mceo Avatar answered Oct 04 '22 13:10

mceo


With bash you can use -n:

bash -n file.sh

Output:

a.sh: line 3: syntax error near unexpected token `then'
a.sh: line 3: `if then fi # Something with syntax error'

Since bash supports the --posix options you may run

bash --posix -n file.sh

to perform a posix compatible check. I don't know how posixly correct that mode is in detail.

like image 39
hek2mgl Avatar answered Oct 04 '22 12:10

hek2mgl