what is "sourcing" in unix/linux shell..

Let me explore “sourcing” in a UNIX/LINUX shell by few words.

I have seen the usage of “sourcing” for a long time in my linux systems.

Today I thought of making it familiar to my friends by this blog..

[humble@humble ]$ cat /etc/rc5.d/S07iscsid |head -n 30|grep -v “#”

. /etc/rc.d/init.d/functions

exec=/sbin/iscsid
prog=iscsid
config=/etc/iscsi/iscsid.conf
lockfile=/var/lock/subsys/$prog

[humble@humble ]$

==> Above shows content of “iscsi” service startup script. Most of the init scripts will have some entry like “. /etc/rc.d/init.d/functions”..

What is it?

By this way it is sourcing “/etc/rc.d/init.d/functions” to this corresponding shell script..

So what advantage it brings?

Let us have a look at this file ..

[humble@humble git_text]$ cat /etc/rc.d/init.d/functions|head -n 18
# -*-Shell-script-*-
#
# functions This file contains functions to be used by most or all
# shell scripts in the /etc/init.d directory.

#

TEXTDOMAIN=initscripts

# Make sure umask is sane
umask 022

# Set up a default search path.
PATH=”/sbin:/usr/sbin:/bin:/usr/bin”
export PATH

# Get a sane screen width
[ -z “${COLUMNS:-}” ] && COLUMNS=80

[humble@humble git_text]$

the “comment” session gives you enough data on why you need to source it in other init scripts.

By sourcing in a shell we are embedding all the variables/functions..etc from this sourced file.. That being said , in some way we can say it is inheriting all the data of “sourced” file , here /etc/rc.d/init.d/functions.

It makes easy to substitute “some of the variables and functions” which are needed by the scripts to get it work.. By placing all of these variables in one destination, it is easy to make changes and it avoid rewriting of the same in all the required places.

When-ever you perform sourcing in a shell , it will not spawn a new shell for it , instead it embed everything in the current shell…

In simple words the “shebang” ( #!/bin/bash) does not have any effect when you perform this .. So new shell is not spawned for its operation. Also you are provided with all the data of sourced file in current shell..

hmmm.. U got something on this and it is enough to start with :)…

Generate and Apply patch in linux using diff and patch commands

Generate patch file:

Patch file says the difference between the existing version and the ‘patched’ version of the file.

Once you have edited the source you can create patch file using diff command…

I normally use below syntax of “diff” for creating a patch file..

#diff -Naur “original file” “modified version”

for ex: suppose u have below file in the system..

[root@humbles-lap patch]# cat main_orig.c
# include <stdio.h>

int main ()
{
    printf ("\n I am original source \n");
}

[root@humbles-lap patch]# cat main.c
# include <stdio.h>

int main ()
{
    printf ("\n I am patched source \n");
}

[root@humbles-lap patch]# diff -Naur main_orig.c main.c
— main_orig.c    2012-07-19 01:48:26.195898607 +0530
+++ main.c    2012-07-19 01:48:37.497899155 +0530
@@ -2,5 +2,5 @@

int main ()
{
–    printf (“\n I am original source \n”);
+    printf (“\n I am patched source \n”);
}
[root@humbles-lap patch]#

As this is an example, I used diff against different files “main_orig.c” and “main.c”, dont get confused with that .. 🙂

“-” and “+” signs tell you which line is removed and which is added in its place..

How to apply patch in a linux system..

To apply patch, you can use “patch” command .

#patch -p1 <“patch file”

ex:

#patch -p1 < main.patch

“-pnum” option controls how file names found in the patch file are used/treated..

Refer #man patch file for more information on this..