Intuitive pseudo object orientation for bash (Shell Script)
This was more of an experiment whether I can get some of OO greatness into bash scripts. I'm not planning to continue any of it at the moment. But feel free to fork it if you are interested!
- "Classes" containing "methods".
- Created objects saved in variables.
- Public / private attributes.
- Inheritance (extending classes).
- Traits (multiple inheritance).
- Object cloning.
- Exceptions - throw and catch them.
As intuitive as possible (it's still bash though).
- 'Classes' are functions, prefixed with 'class::'.
- 'Methods' are functions within classes, prefixed with 'method::'.
- 'Objects' are created by calling new [Class name], save it's return value into a variable.
#!/bin/bash
source ooengine || exit 1
# A simple base class named 'TestClass'.
class::TestClass() {
# Public attribute, 'get' and 'set'-able.
public attributePublic "optional default value"
# Private attribute, only internal 'get' and 'set'-able.
private attributePrivate "optional default value"
# Public method, reachable with any reference.
method::publicTestMethod() {
echo "Do something here."
# Call private method.
$this internalTestMethod
}
# Public getter method for private attribute 'attributePrivate'.
method::getAttributePrivate() {
$this get attributePrivate
}
# Public setter method for private attribute 'attributePrivate'.
method::setAttributePrivate() {
$this set attributePrivate "$1"
}
# Private method, only internal reachable with $this reference.
__method::internalTestMethod() {
echo "Or do something here."
}
}
# Create an object of the class.
testObject=$(new TestClass)
# Call a method.
$testObject publicTestMethod
# Get a public attribute.
value=$($testObject get attributePublic)
echo $value
# Set a public attribute.
$testObject set attributePublic "New value"
# Get a private attribute.
# echo $($testObject get attributePrivate) # This would fail.
value=$($testObject getAttributePrivate)
echo $value
# Set a private attribute.
# $testObject set attributePrivate # This would fail.
$testObject setAttributePrivate "New value"
# Destruct the object.
$testObject destruct
Please have a look at the examples in ./examples directory!
- Console (Incomplete) - Console/IO help functions.
- String (Incomplete) - String class.
- Array (Incomplete)
- MySQL (Incomplete)
- ...
Use libraries by importing them:
import String # (searches for $librarypath/String/String)
import String/StringManipulation # (searches for $librarypath/String/StringManipulation)
Use own library directories:
add ./mylibraries
import MyLibrary
import MyLibrary/SubClass