Use OOP Encapsulation Style Write Bash Script

OOP(Object-Oriented Programming), I am trying some OOP idea to make bash scripting more easier, easy to understand, modify and expand function in the Open Source project: MobileMate. There are three basic OO features: encapsulation, function polymorphism, and inheritance. Here only borrow its encapsulation idea. As a result, for a non-professional in programming, there are more layers, more classes, small class, small function, simple code, more tests in my coding.

Overview
I use converting part of MobileMate V0.1a42.1 code as an example to decription the way of using OOP encapsulation.

The relationship between scripts as below:
Caller: bin/guiconvert.sh
Be Called:
Generate converting scripts:
if convert all files in a folder: share/folderconvertor
if convert a single file:
if use MENCODER to decode: share/convertor
if use LIBAV to decode: share/lconvertor
Execute converting scripts to do the real converting:
share/taskmanager

Caller: share/folderconvertor
Be Called:
if use MENCODER to decode: share/convertor
if use LIBAV to decode: share/lconvertor

Caller: share/lconvertor
Be Called:
if convert to video: share/lv2vconvertor
if convert to audio: share/lx2aconvertor

Also, these scripts call some auxiliary “classes”, for example, “share/configurator” to hundle configuration files, “share/filedetector” to detect file information, “share/helper” holds some utility functions.

A “class”
Here, as an example, I use “share/lconvertor” to describe how I make a bash script class.

The “class”, “share/lconvertor”, the task is converting file to video or audio file using Libav as decoding tool.

There are three data members in “lconvertor”: lconvertor_input, lconvertor_output, lconvertor_option.

lconvertor_input: source file.
lconvertor_output: destination directory.
lconvertor_option: converting options.

There are method(function) members in “lconvertor”: lconvertor_init(), lconvertor_destroy(), lconvertor_set(), lconvertor_get(), lconvertor_is2video(), lconvertor_getvf(), lconvertor_getaf(), lconvertor_doconverts().

lconvertor_init(): initialize data members and maybe some other initialization operations at the begin.
lconvertor_destroy(): data members cleanup and maybe some other cleanup operation at the end.
lconvertor_set(): set the data members.
lconvertor_get(): get the data members’ value.
lconvertor_is2video(): check if the input file is a video or audio.
lconvertor_getvf(): according to the lconvertor_option, form the video filters chain string.
lconvertor_getaf(): according to the lconvertor_option, form the audio filters chain string.
lconvertor_doconverts(): according to the lconvertor_input, lconvertor_output, lconvertor_option, lconvertor_getvf(), lconvertor_getaf(), form the new options for the next layer class to be called, call the lv2vconvertor if converting to a video file and call the lx2aconvertor if converting to an audio file.

Well, you can see every data or method member has a prefix “lconvertor_”, it’s the “class” name. Because in the bash script, the data and method member is all public access, so need a technism to avoid conflict. Although the data member will be set a initial value when import the class “. …/share/lconvertor” or call the lconvertor_init() and lconvertor_destroy(). It’s double ensure avoid variable and function name conflict.

In fact, there are some full implimentations of shell OOP, for example: https://www.usenix.org/legacy/publications/library/proceedings/bos94/full_papers/haemer.ps , A New Object-Oriented Programming Language: sh Jeffrey Haemer, Canary Software, Inc..

Shell script is a useful tool, how to use it finish the job properly, it’s decided by own favorite seriously. With the limited shell scripting skill, and like the code more humanization, I chose oop encapsulation to write shell, and chose more layers, more classes, small class, small function, simple code, and more tests in my coding.

Leave a comment

Your comment

IMPORTANT! To be able to proceed, you need to solve the following simple math (so we know that you are a human) :-)

9 plus 7 equal to?
Please leave these two fields as-is:

蜀ICP备2021012931号-1 川公网安备51092202000377号