I do lots of unix shell scripting. It’s easy and I can get things done without hassle.
Okay, now this one is maybe easy. But I would like to call a subroutine defined in the library script file, let’s say “lib.inc”.

lib.inc

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh
 
# This is our real simple sub routine returns 0 value when it is happy
is_happy ()
{
     #First arg $1, Second arg  $2, third $3 ... so on.
     if [ "$1" = ":)" ] ; then
     	return 0
     else
     	return 1
     fi
}

You can include the lib.inc by using “.” notation.
When you call the sub routine , call it just like you execute an external command. And the return value can be obtained using $?. Just like getting the return value of external command after execution.

This is main script calling the is_happy().

1
2
3
4
5
6
7
8
9
10
11
12
#!/bin/sh
#including the lib.inc file
. lib.inc
 
#calling subroutine
is_happy ":)"
 
if [ "$?" -eq 0 ] ; then
        echo "I am happy!"
else
        echo "No I am not happy!"
fi

That was easy right? There will be more fun on shell scripting at my Coding Haven soon.