Previous | Next

Objectives| Introduction| Shell Functions| Input/Output for Shell Programs| Trapping Signals| Regular Expressions| ex, vi and Regular Expressions| awk| Perl| Conclusion| Review Questions

Section 4


ADVANCED UNIX USE


Objectives


At the end of this section you will

Introduction


In section 3 you were provided with an introduction to some simple UNIX comamnds and to the art of writing shell scripts. In this section you will encounter some of the more advanced commands and some of the advanced techniques that can be used in writing shell scripts.

Before getting started lets be certain about the terminology that is to be used in relation to shell programs. Table 4.1 lists the terminology that will be used throughout this course in relation to shell programs. Other people may use slightly different terms.

Term		Explanation

Shell program	an executable file that contains UNIX and shell
		 commands and can be interpreted by a UNIX shell
Shell script	same as a shell program
Shell procedure	same as a shell program
shell function	similar to a function in C or Pascal, only used
		 within a shell program (introduced below)

	Table 4.1. Terminology for shell scripts and functions.

Shell Functions


Any good programming langauges provides support for functions. Functions serve to group collections of commands that carry out an often required task. The Bourne shell programming language is no exception.
Actually that isn't quite true. Older versions of the Bourne shell actually didn't support functions. On most System V based UNIX operating systems this is no longer a problem as the Bourne shell has been updated. However some BSD based machines may have old versions of the Bourne shell that don't recognise shell functions. (bash supports them)
		Bourne Shell Function Syntax

	name()
	{
		command-list
	}

	Figure 4.1. Bourne Shell Function Syntax.
Section 3 introduced the predefined shell variables $0 $1...$9 and the special meaning that shell programs give them. The meaning of these variables change when they are used inside of a shell function.

Some of the differences include:

There is only one pool of shell variables in a shell program. The Bourne shell does not support any notion of local variables. Any variable created in a shell function can be accessed from outside of that function.
For example:
Create a shell procedure containing the following code
#!/bin/sh

# an example shell function called hello
hello()
{
    # display the function's 1st param
    echo hello there $1
    # display all function parameters
    echo $*
    echo $0
    local_var="hello"
}

# call the function and pass to it all the 
# programs parameters
hello $*
# show that there is no scope rules
echo "Local variable value = " $local_var
# call the fucntion but pass it no parameters
hello
In keeping with the standard concept of functions, shell functions also have a return status. The return command indicates the shell function's return status. A successful shell function should return a value of 0 while a shell function that fails should return a non zero value.

For example:

temp()
{
  return 1
}

if temp
then
  echo succeeded
else
  echo failed
fi
Exercise 4-1. Convert the shell script file_type from review question three from section 3 into a function. The function should be called file_type and it should set a variable type to the type of the file passed in (the function should only take one parameter).

An example use of the function would be
file_type /etc/passwd
echo "/etc/passwd is a $type."


Input/Output for Shell Programs


This section shows you how to