Basics

Here are the basics of POSIX script that is compact to remember and versatile to carry out complicated works. These are:

  • the basic syntax
  • the techniques covering logical and needs
  • the tips in data processing, piping and integration
  • how to execute POSIX mode
  • testing your POSIX shell script

Before we start, you should know some very basic practice for BASH script across all the following topics.

Basic Script Creation

In UNIX system, any executable must have the "execute" permission. This includes the new bash script as well. Without the "execute" permission, you can't run the shell script.

Hence, a good practice for creating a shell script is to:

  1. Create the empty script file
  2. Grant execute permission
  3. Edit the bash script

Here's the example of the commands, say we name our script demo.sh:

holloway:Desktop$ touch demo.sh          # create the empty file
holloway:Desktop$ chmod +x demo.sh       # grant execute permission to file
holloway:Desktop$ ls                     # check the file
demo.sh
holloway:Desktop$

Throughout the following sub-chapters, when we say "create a bash script", we simply mean these 3 steps instructions.

POSIX Shell Script File Extensions

Since this is a shell script, POSIX shell script is strictly .sh extension.

NOTE: In UNIX system, file extension doesn't matters. What really matters is the SHEBANG line inside the script, which we will cover in the sub-topic.

SHEBANG Line

The first line of any executable scripts must always be the SHEBANG line. This is a special line that tells the operating system how to execute the script. In our case, we wants specifically /bin/sh to run:

#!/bin/sh

NOTE:

  • SHEBANG line is the first line of the script. It is symbolized by #! for the first 2 characters. It instructs the operating system to execute the program using that particular command.

Execute POSIX Mode

Unlike BASH specific script, you need to know how to make use of the available shell to run your POSIX script. Example, for BASH, you need to pass in the --posix flag; dash is POSIX mode on its own.

  • BASH - bash --posix path/to/script.sh
  • DASH - dash path/to/script.sh

Testing Your POSIX Shell Script

Unlike BASH script, POSIX shell scripts require extensive testing to guarantee its portability POSIX compliance. To simplify the scope and reach-ability, I categorize the level of testings as described in the following depth.


1st Degree - Development

At this level, you can use BASH POSIX mode to develop and to test your shell script. As long as you run the BASH in POSIX mode and have your SHEBANG line set correctly, you are fine with your script.


2nd Degree - Cross Shell Program Testing

At this level, you should use some other shell program apart from your 1st degree shell program to test your script. This is to test the script's cross-compatibility between different shell program. If you pass this level, your shell script is confidently usable across majority Linux and *nix operating system (but not all).

You can test your shell script in the same operating system, as long as it can cater multiple different shell program.


3rd Degree - Operating System Shell Program Testing

At this level, you should re-run your 2nd degree testing across different operating system, especially those that you're targeting for. This is an ideal level of testing as some, like MacOS, doesn't pack the default GNU Coreutils packages like Linux does. Hence, there are external program differences which might break your program.

Normally, at this level of test, virtualization solution is highly recommended.

Once you pass this degree of testing, your shell script is safe for POSIX compliance.

Now that you know the fundamental and very basic of creating, naming the bash script. We can start writing the POSIX Shell script. Please proceed to to the next sub-topic.