8.7 Actual Shell Script



Then, let's see a shell script which is actually working.


Please refer to /etc/rc this time. The shell script of /etc/rc is performed when system starts, and the processing required for startup is described.


line 28
. /etc/init.d/functions


This line reads a file called /etc/init.d/functions. This file contains useful common functions for the shell scripts under /etc directories. Line 28 is performed for making those functions effective.


line 43
[ -d /etc/rc$runlevel.d ] || exit 0


The value of the run level is contained in $runlevel. When the run level is 3, existence of /etc/rc3.d is checked by -d.

|| is logical sum. The expression in left side is processed first. In this case, the existence of the directory of /etc/rc3.d is checked, and if it exists, the control moves to next expression. In other words, if the left expression is true, the control does not move into right side.

exit 0 means terminating processing here. Therefore, the 43rd line checks the existence of /etc/rc3.d, and if it does not exist, this processing ends at this point. The following value of 'exit' command means finish code of the shell script. When the script had completed normally, the value is 0. Otherwise, the value shows an error code. When the script omits the value, it is 0. Since the finish code is stored into special variable '$?', you can refer to it.


line 46
for i in /etc/rc$runlevel.d/K* ; do


This explanation is also based on $runlevel=3. There are two kinds of script files under /etc/rc3.d. The script files which start with S, make some services start. The script files which start with K, make some services stop.

If the 46th line is performed, the shell script list which starts with K will go into i. Although there are other various processings, "$i stop" which stops processing at 56 or 58th line is performed.

The real files of the shell scripts which start with K, are under /etc/init.d. The numerical value of double figures which starts after K is the turns to stop.


There is a file named /etc/init.d/functions. As explained above, this file contains useful common functions for the shell scripts. One function described in it is explained.


line 568
is_ignored_file() {
   case "$1" in
      *~ | *.bak | *.orig | *.rpmnew | *.rpmorig | *.rpmsave)
            return 0
            ;;
   esac
   return 1
}


This is a function named is_ignored_file(). It is the function used for the check of files in /etc/rc. The first argument is file name. It checks whether file name ends with .bak, .orig, .rpmnew, .rpmorig, or .prmsave in the case sentence. If it matches, the function returns 0, otherwise it returns 1.

This function checks whether specific file can be ignored or not. It is judged by the file name. If it can be ignored, this function returns 0, otherwise returns 1. This can be used, even if it is not in /etc/rc. In addition, you can modify all other shell scripts by changing original.

For example, when the files which name terminates with '.abc' should be ignored, you can add the entry '.abc' in the original file. This modification will affect the files in /etc/rc, but also other files which use this function(is_ignored_file()).

The use of common function increases maintainability of the shell scripts.



Previous Next