1. Introduction

    System Information

  2. Inter-Process Communication
  3. Signals

    Programming in Various Environments

  4. Script Programming
    1. Script Variables
    2. Test Conditions
    3. Control and Iteration
    4. Commonly used Programs
    5. Shell Capabilities
    6. Example looping script
    7. Example using Variables
    8. Example working with files
    9. Example install script
  5. C and C++ Programming
    1. POSIX System Capabilities
    2. More POSIX
    3. Threads
    4. Mutexes
    5. An example viewmod program
    6. An example serial program
  6. X Programming
  7. Debugging
  8. Credits

Linux Script Examples

A makefile script example

Scripting that checks a file and modifies it if required. Since it is from a makefile, this file is similar to a normal script but is slightly modified with lines that end with ";\" characters. This section demonstrates modification of the rc.local file from a makefile so a script program, "printst", may be run in the background when the system starts. The variable "BINLOC" is defined earlier with the line "BINLOC = /usr/local/tmp". See the section on "Making Packages" under the header "Making makefiles".

# Add the path for $(BINLOC) to the /etc/rc.d/rc.local file
	@PATHIN=`grep "path=" /etc/rc.d/rc.local | grep "$(BINLOC)"`; \  # See if the BINLOC path is set in the rc.local file
	HEADER=""; \
	if \
	   [ -z "$$PATHIN" ]; \
	then \
	   echo Adding $(BINLOC) to path in /etc/rc.d/rc.local; \
	   echo >> /etc/rc.d/rc.local; \
	   echo "###### Added by Print Service Install on `date` by `whoami`" >> /etc/rc.d.rc.local; \
	   HEADER=Y; \
	   echo "export PATH=\$$PATH:$(BINLOC)" >> /etc/rc.d.rc.local; \
	else \
	   echo $(BINLOC) already set in path in file /etc/rc.d/rc.local; \
	fi; \
	PRINTST=`grep -i "printst &" /etc/rc.d/rc.local`; \  # See if the string printst is in the rc.local file
	if \
	   [ -z "$$PRINTST" ]; \
	then \
	   echo Adding printst command to /etc/rc.d/rc.local file; \
	   if \
	      [ -z "$$HEADER" ]; \
	   then \
	      echo >> /etc/rc.d.rc.local; \
	      echo "###### Added by Print Service Install on `date` by `whoami`" >> /etc/rc.d.rc.local; \
	      HEADER=Y; \
	   fi; \
	   echo "printst &" >> /etc/rc.d.rc.local; \
	else \
	   echo Already have printst command in /etc/rc.d/rc.local file; \
	fi; \
	if \
	   [ -n "$$HEADER" ]; \
	then \
	   echo "###### End of Print Service Install Section ######"  >> /etc/rc.d.rc.local; \
	   echo >> /etc/rc.d.rc.local; \
	fi;