-->
Previous Table of Contents Next


Functions

The shell languages enable you to define your own functions. These functions behave in much the same way as functions you define in C and other programming languages. The main advantage of using functions as opposed to writing all of your shell code in line is for organizational purposes. Code written using functions tends to be much easier to read and maintain and also tends to be smaller, because you can group common code into functions instead of putting it everywhere it is needed. The tcsh shell does not support functions.

The syntax for creating a function in bash and pdksh is the following:


fname () {

      shell commands

}

pdksh also allows the following syntax:


function fname {

      shell commands

}

Both of these forms behave in the exact same way.

Once you have defined your function using one of these forms, you can invoke it by entering the following command:


fname [parm1 parm2 parm3 …]

Notice that you can pass any number of parameters to your function. When you do pass parameters to a function, it sees those parameters as positional parameters, just as a shell program does when you pass parameters to it on the command line. For example, the following shell program contains several functions, each of which is performing a task associated with one of the command-line options. This example illustrates many of the topics covered in this chapter. It reads all the files that are passed on the command line and—depending on the option that was used—writes the files out in all uppercase letters, writes the files out in all lowercase letters, or prints the files.


upper () {

      shift

      for i

      do

          tr a-z A-Z <$1 >$1.out

          rm $1

          mv $1.out $1

          shift

      done; }



lower () {

      shift

      for i

      do

          tr A-Z a-z <$1 >$1.out

          rm $1

          mv $1.out $1

          shift

      done; }



print () {

      shift

      for i

      do

           lpr $1

           shift

      done; }



usage_error () {

      echo “$1 syntax is $1 <option> <input files>”

      echo “”

      echo “where option is one of the following”

      echo “p -- to print frame files”

      echo “u -- to save as uppercase”

      echo “l -- to save as lowercase”; }

case $1

in

      p | -p)   print $@;;

      u | -u)         upper $@;;

      l | -l)   lower $@;;

      *)        usage_error $0;;

esac

Summary

This chapter introduced you to many of the features of the bash, pdksh, and tcsh programming languages. As you become familiar with using Linux, you will find that you use shell programming languages more and more often.

Even though the shell languages are very powerful and also quite easy to learn, you might run into some situations where shell programs are not suited to the problem you are solving. In these cases, you may want to investigate the possibility of using one of the other languages available under Linux. At this point, there are a lot of other programming languages you may want to look at. To learn more about:

awk, which is useful for handling both search patterns and large columns of numbers, see Chapter 25, “gawk.”
Perl, useful for many quick script tasks as well as Web pages, see Chapter 28, “Perl.”
Smalltalk/X, which is used to program object-oriented applications under the X GUI, see Chapter 31, “Smalltalk/X.”


Previous Table of Contents Next