How To Write and Use Custom Shell Functions and Libraries

[‘

n

In Linux, shell scripts help us in so many different ways including performing or even automating certain system administration tasks, creating simple command line tools and many more.

n

In this guide, we will show new Linux users where to reliably store custom shell scripts, explain how to write custom shell functions and libraries, use functions from libraries in other scripts.

n

Where to Store Shell Scripts

n

In order to run your scripts without typing a full/absolute path, they must be stored in one of the directories in the $PATH environment variable.

n

To check your $PATH, issues the command below:

n

$ echo $PATHrnrn/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/gamesrn

n

Normally, if the directory bin exists in a users home directory, it is automatically included in his/her $PATH. You can store your shell scripts here.

n

Therefore, create the bin directory (which may also store Perl, Awk or Python scripts or any other programs):

n

$ mkdir ~/binrn

n

Next, create a directory called lib (short for libraries) where you’ll keep your own libraries. You can also keep libraries for other languages such as C, Python and so on, in it. Under it, create another directory called sh; this will particularly store you shell libraries:

n

$ mkdir -p ~/lib/sh rn

n

Create Your Own Shell Functions and Libraries

n

A shell function is a group of commands that perform a special task in a script. They work similarly to procedures, subroutines and functions in other programming languages.

n

The syntax for writing a function is:

n

function_name() { list of commands }rn

n

For example, you can write a function in a script to show the date as follows:

n

showDATE() {date;}rn

n

Every time you want to display date, simply invoke the function above using its name:

n

$ showDATErn

n

A shell library is simply a shell script, however, you can write a library to only store your functions that you can later call from other shell scripts.

n

Below is an example of a library called libMYFUNCS.sh in my ~/lib/sh directory with more examples of functions:

n

#!/bin/bash rnrn#Function to clearly list directories in PATH rnshowPATH() { rn        oldifs="$IFS"   #store old internal field separatorrn        IFS=:              #specify a new internal field separatorrn        for DIR in $PATH ;  do echo $DIR ;  donern        IFS="$oldifs"    #restore old internal field separatorrn}rnrn#Function to show logged userrnshowUSERS() {rn        echo -e “Below are the user logged on the system:\n”rn        wrn}rnrn#Print a user’s details rnprintUSERDETS() {rn        oldifs="$IFS"    #store old internal field separatorrn        IFS=:                 #specify a new internal field separatorrn        read -p "Enter user name to be searched:" uname   #read usernamern        echo ""rn       #read and store from a here string values into variables using : as  a  field delimiterrn    read -r username pass uid gid comments homedir shell <<< "$(cat /etc/passwd | grep   "^$uname")"rn       #print out captured valuesrn        echo  -e "Username is            : $username\n"rn        echo  -e "User's ID                 : $uid\n"rn        echo  -e "User's GID              : $gid\n"rn        echo  -e "User's Comments    : $comments\n"rn        echo  -e "User's Home Dir     : $homedir\n"rn        echo  -e "User's Shell             : $shell\n"rn        IFS="$oldifs"         #store old internal field separatorrn}rn

n

Save the file and make the script executable.

n

How To Invoke Functions From a Library

n

To use a function in a lib, you need to first of all include the lib in the shell script where the function will be used, in the form below:

n

$ ./path/to/librnORrn$ source /path/to/librn

n

So you would use the function printUSERDETS from the lib ~/lib/sh/libMYFUNCS.sh in another script as shown below.

n

You do not have to write another code in this script to print a particular user’s details, simply call an existing function.

n

Open a new file with the name test.sh:

n

#!/bin/bash rnrn#include librn.  ~/lib/sh/libMYFUNCS.shrnrn#use function from librnprintUSERDETSrnrn#exit scriptrnexit 0rn

n

Save it, then make the script executable and run it:

n

$ chmod 755 test.shrn$ ./test.sh rn

n

Write Shell Functions
Write Shell Functions

n

In this article, we showed you where to reliably store shell scripts, how to write your own shell functions and libraries, invoke functions from libraries in normal shell scripts.

n

Next, we will explain a straight forward way of configuring Vim as an IDE for Bash scripting. Until then, always stay connected to TecMint and also share your thoughts about this guide via the feedback form below.

n

‘]