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.

Get Mastering FreeSWITCH now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.