Approach Bash Like a Developer - Part 5 - Success!
This is part five of a series on how to approach bash programming in a way that’s safer and more structured than your basic script.
See part 1 if you want to catch the series from the start.
Last time, we created a failing test. We’ll work on making it work now, but I’m going to jump straight to the structure that a project with tests should take.
The project will be a hello, world! script, so let’s start with:
The hello_world function exists, but does nothing.
Move hello-world_shpec.bash to the shpec directory.
Open hello-world_shpec.bash in an editor and add the following to the beginning:
That finds the true location of hello-world_shpec.bash via readlink, then trims off the filename from the path. It then adds the relative path to our hello-world source file.
Note that on Mac, you’ll need to install GNU readlink via homebrew, then use greadlink instead of the readlink above. You may simply want to alias readlink to greadlink in your scripts. See the post on aliases for how to create aliases in scripts.
Shpec now outputs the following:
Still failing. Excellent!
At this point, all we need to do is update the hello_world function:
Shpec’s output:
Success! Excellent!
Continue with part 6 - outline script