Sunday, February 20, 2011

Linux Tip: Creating Shell Script Functions and Exposing Them in the Current Runtime Environment

Creating shell scripts are easy to develop at the same time hard. Imagine that you have almost all access to the kernel, services, modules and available functions. Well it's procedural but that doesn't mean you can't group applications together while specifying their run sequence and simply apply your OOP and design patterns in these "bits" of applications.

So how do you group them? You create a shell function or subroutine. Here's how

Create your bash / shell script function:
vi /path/to/your/desired/function/file
#my_function
my_function(){
    echo "Isang halimbawa galing kay ulaptech"
}

Expose the bash function:
Note: You can type it to your shell console and the command becomes available. You can also use the same method to include it to other bash scripts and it becomes available as a command.
Warning: Make sure you don't use the same namespace used by the OS and other functions or you'll end up with all sorts of weird results.
vi /path/to/your/desired/function/file
#include it to your runtime environment
.  /path/to/your/desired/function/file

The guide above is applicable to most linux systems but was primarily tested for CentOS and RHEL.
  • Related Links Widget for Blogspot

No comments: