Utility functions
We've used a lot of self-written functions. It's time to have a look at them. Let's open the top half of the utils.lua file, in the preceding screenshot.
The first three functions serve mainly to build the fourth function, stamp(), that we use throughout our scripts to do structured logging.
shell() is an example of a typical Lua interaction with an operating system. It executes a command, and returns the output as a string. We use it in stamp() to obtain the result of the command date.
trim() uses Lua native string manipulation, and is equivalent to the chomp() command in Perl, and many similar others in different languages: it deletes the trailing newline in a string, if it exists, and returns the string without the newline.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access