unix scripting - how functions work

Upload: ganesh1895

Post on 03-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Unix Scripting - How Functions Work

    1/18

    All rights reserved. Reproduction and/or distribution in whole or inpart in electronic, paper or other forms without written permission

    is prohibited.

    Working with Korn ShellSkillSoft Corporation. (c) 2003. Copying Prohibited.

    Reprinted for Raman Ramachandran, [email protected]

    Reprinted with permission as a subscription benefit ofBooks24x7,http://www.books24x7.com/

    http://www.books24x7.com/http://www.books24x7.com/
  • 8/12/2019 Unix Scripting - How Functions Work

    2/18

    Table of ContentsChapter 6: How Functions Work.....................................................................................................1

    Using Functions in the Korn Shell..................................................................................................2The Structure of a Function in the Korn Shell.........................................................................2Command Line Functions.......................................................................................................3Viewing a Function..................................................................................................................5The Return Types of a Function.............................................................................................6

    Programming UsingFunctions.....................................................................................................10Using Local and Global Variables with Functions.................................................................10Calling a Function Recursively..............................................................................................12Loading a Function Automatically.........................................................................................13Passing Parameters..............................................................................................................15

    i

  • 8/12/2019 Unix Scripting - How Functions Work

    3/18

    Chapter 6: How Functions WorkA function is a set of statements that run together to perform a particular task. Functions increasethe reusability of code, because you can use the same function multiple times in a program. Youinvoke a function using the function name.

    In the Korn shell, when you invoke a function, the shell interpreter transfers program control to the

    first statement in the function. After the interpreter interprets all the statements in a function, theKorn shell transfers control back to the calling function or to the module that invoked the function.

    This chapter explains how to use functions in the Korn shell. It also describes how to view andcreate command line functions. The chapter explains programming with functions using variablesand recursion and gives instructions on loading a function automatically.

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    4/18

    Using Functions in the Korn ShellFunctions in the Korn shell are similar to the functions of other programming languages, such as Cand C++. A Korn shell function is stored in the Korn shell memory, and you can invoke the functionusing the function name.

    Unlike a Korn shell program, running a function does not create a separate shell process. The

    advantages of using a function in a Korn shell program are:Improves processing speed because the function resides in the Korn shell memory.

    Creates an optimal Korn shell program because repeated code fragments are replaced bycalls to the function.

    The Structure of a Function in the Korn Shell

    When you create a function, enclose all the statements to run together in curly braces. A functionconsists of an optional return statement that specifies the success or failure of a function.

    The syntax to create a function in the Korn shell is:

    function name_of_function{statement1[ [ [statement2] ...] return value ]}

    Alternatively, you may use this syntax:

    name_of_function(){statement1

    [ [ [statement2] ...] return value ]}

    Both syntaxes show how to create a function with a name specified in the name_of_functionvariable. The function name should not contain parentheses. The syntax includes an optional returnstatement that indicates whether the function executed successfully or not. The return statement isalso used to return the function result to the calling Korn shell program.

    For example, in the UNIX operating system, the who am i builtin command identifies the end userlogged in at a terminal. The code for creating a userdefined function that performs the samefunction as the who am i command is:

    function user

    { var=$(who am i) print "$var"}

    This code creates a function that identifiesthe end user currently working on a terminal. The varvariable displays the name of the user.

    You run a function by typing the name of the function on the console. A pair of parentheses shouldsucceed the function name. Figure 61 shows how the user function identifies the end user workingon a terminal:

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    5/18

    Figure 61:Using the user FunctionIn contrast to programming languages such as C and C++, Korn shell function definitions precedethe statement that calls the function. In a Korn shell program that uses a function, the Korn shellfunction can access the programs variables and the program can access the functions variables.You can also pass parameters to a Korn shell function, by typing them at the command line thatinvokes the function.

    However, the parameters that you pass to a Korn shell program from the command line are notavailable to functions in the Korn shell program.

    Command Line Functions

    The Korn shell allows you to create command line functions to perform a specific task. Thecommand line functions are volatile functions. This means that a command line function is deletedwhen you exit the Korn shell.

    The syntax for a command line function is:

    function name_of_function

    This syntax creates a function with the name specified in the name_of_function command lineparameter.

    After you create a command line function, you can invoke the function by typing the function nameat the console.

    Listing 61 shows a command line function that shows the current date and time:Listing 61: Using the Command Line Function

    # function week> {> if [[ z $1 ]]> then> date +%c> return 0> fi> case $1 in> abw) var=$(date +%a)> print "$var";;> wk) var=$(date +%A)> print "$var";;

    Working with Korn Shell 3

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    6/18

    > abm) var=$(date +%b)> print "$var";;> mn) var=$(date +%B)> print "$var";;> cen) var=$(date +%C)> print "$var";;> dy) var=$(date +%d)> print "$var";;> dt) var=$(date +%D)> print "$var";;

    > tm) var=$(date +%T)> print "$var";;> *) print "Please enter the values in correct format\n"> return 1;;> esac }

    The above listing shows the week function, which displays the current date and time. The weekfunction uses a case statement to handle various options for presenting the results of the datecommand.

    The week function also includes options for viewing the current day, date, and time. Table 61

    describes the command options for viewing the current day, date, and time:

    Table 61: Command Options Available with the week Function

    Command Option Description

    abw Displays the abbreviated name of a weekday. For example, if the day ofthe week is Monday, the abw command option displays mon.

    wk Displays the complete name of a weekday.

    abm Displays the abbreviated name of a month. For example, if the month isJanuary, the abm command option displays jan.

    mn Displays the complete name of a month.

    cen Displays the century as a decimal number.dy Displays the current date.

    dt Displays the current date. The dt command option displays the currentdate in the month/date/year format.

    tm Displays the current time. The tm command option displays the currenttime in the hour:minute:second format.

    When you enter the week function with a command option at the command line, the time or dateappears in the format specified by the command option. Figure 62 shows the output of Listing 61,as the week function displays the current date and time:

    Working with Korn Shell 4

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    7/18

    Figure 62:Viewing the Current Date and TimeViewing a Function

    After creating a command line function, you can view the function name and definition. The builtinKorn shell command, typeset, displays the function definition and name. The typeset includes threeoptions for viewing a command line function:

    typeset f: Shows the definitions of all the command line functions that have been created inthe current Korn shell session. Since the command line functions live only as long as the

    current session, you cannot view functions created in earlier Korn shell sessions.

    typeset +f: Shows just the names of command line functions that have been created in thecurrent Korn shell session.

    typeset f fun_name: Shows the definition of the command line function represented by thefun_name parameter.

    Figure 63 shows how to use the typeset command to view a command line function definition:

    Working with Korn Shell 5

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    8/18

    Figure 63:Using the typeset CommandThe Return Types of a Function

    The Korn shell uses the return statement to return an integer or string value to a calling program.The return value of a function is stored in the builtin Korn shell variable, $?. The return statementcan return an integer value up to 255. Listing 62 shows how to use a return statement to returninteger values:

    Listing 62: Using the return Statement to Return Integer Values

    #!/bin/kshfunction week{ if [[ z $1 ]] then var=$(date +%c) return 0 fi case $1 in abw) var=$(date +%a) return 1;; wk) var=$(date +%A) return 2;; abm) var=$(date +%b) return 3;;

    mn) var=$(date +%B) return 4;; cen) var=$(date +%C) return 5;; dy) var=$(date +%d) return 6;; dt) var=$(date +%D) return 7;; tm) var=$(date +%T) return 8;; *) return 9 ;; esac}# Korn shell program execution starts from hereweek $1case $? in

    Working with Korn Shell 6

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    9/18

    0) print "Current date and time is: " print "$var" ;; 1) print "Abbreviated name for current weekday is: " print "$var" ;; 2) print "Complete name for current weekday is: " print "$var" ;; 3) print "Abbreviated name for current month is: " print "$var" ;; 4) print "Complete name for current month is: " print "$var" ;;

    5) print "Current century in decimal format is: " print "$var" ;; 6) print "Current day of the month is: " print "$var" ;; 7) print "Current date is: "print "$var" ;; 8) print "Current time is: " print "$var" ;; 9) print "Enter the correct option for the function" print "$var" ;;esac

    The above listing shows how to use the return statement to view the current date and time. Thefunction parameters are passed through the statement in the Korn shell program that invokes theweek function.

    For each function parameter, the week function returns an integer value. The Korn shell programconsists of case control to display the date and time using the Korn shell builtin variable, $?. Figure64 shows the output of Listing 62 that displays the date and time:

    Figure 64:Viewing Date and TimeYou can also return a functions result in a string. To return a function value in a string, enclose thefunction name in parentheses preceded by the dollar ($) sign. The $ sign attached to a variabledenotes the value stored in the variable. Listing 63 shows how a function result can be returned ina string:

    Listing 63: Returning a Function Result in a String

    #!/bin/kshfunction cmds

    Working with Korn Shell 7

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    10/18

    { case $1 in env) echo "The value of builtin variable, ENV, is: $ENV";; home) echo "The value of builtin variable, HOME, is: $HOME" ;; shl) echo "The value of builtin variable, SHELL, is: $SHELL";; mail) echo "The value of builtin variable, MAIL, is: $MAIL";;

    *) echo "Incorrect parameter";; esac}if [[ z $1 ]] then echo "Please enter a function parameter. The function parametersavailable with function are env, home, shl, and mail" exit 1fival=$(cmds $1)print "$val"

    The above listing shows the cmds function, which returns the function value in the val string.

    The cmds function includes a case statement that handles the builtin Korn shell variables ENV,HOME, SHELL, and MAIL. Table 62 describes the Korn shell builtin variables that the cmdsfunction uses:

    Table 62: Korn Shell BuiltIn Variables in the cmds Function

    BuiltInVariable

    Description

    ENV Specifies the environment file that the UNIX operating system invokes when theKorn shell starts.

    HOME Specifies the logon directory for a UNIX user.

    SHELL Specifies the path for the shell program that currently runs UNIX commands.MAIL Specifiesthe file where email messagesare stored.

    Figure 65 shows the output of Listing 63, which displays the value of the builtin Korn shellvariables and returns the function result in a string variable:

    Working with Korn Shell 8

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    11/18

    Figure 65:Running Builtin Korn Shell Variables

    Working with Korn Shell 9

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    12/18

    Programming Using FunctionsA local variable is accessible within a function while a global variable is accessible to the entire shellprogram. When programming with functions, you can automatically load a function into a program toimprove the performance of a Korn shell program. You can pass parameters between a functionand the calling program either by reference or by value.

    Using Local and Global Variables with FunctionsIn the Korn shell, a function can include both local and global variables. A local variable isaccessible only within a function, but a global variable is accessible to the entire shell program.

    A local variable holds a value within the function where it is defined. Local variables enable you todivide a large program into small modules so that each module can be developed independent ofthe other.

    You can use the Korn shell keyword, typeset, to define a local variable. As a local variables scopeis restricted to the function where it is defined, you can use the same local variable name in differentKorn shell functions. Listing 64 shows how to use the same local variable name with multiplefunctions in a Korn shell program:

    Listing 64: Using Local Variables

    #!/bin/kshfunction fun1{ typeset local1 local1="In function, fun1, variable, local1 contains: My name is John" echo $local1}function fun2{ typeset local1 local1="In function, fun2, variable, local1 contains: My name is Sandra" echo $local1}local1="In program, variable, local1 contains: My name is Sam"

    echo $local1fun1fun2

    The above listing uses two local1 local variables in two functions, fun1 and fun2.

    The Korn shell keyword, typeset, declares the local1 local variable. This variable is used in the locKorn shell program, and in the fun1 and fun2 functions. Figure 66 shows the output of Listing 64that displays the values of local variables:

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    13/18

    Figure 66:Displaying the Values of Local VariablesIn addition to local variables, you can also define global variables in a function. You can accessglobal variables from any function present in a Korn shell program. You can make a variable globalby:

    Not declaring the variable in the Korn shell program: Allows you to use a variable globally in

    a Korn shell program. When the Korn shell reads a variable for the first time, it checkswhether the variable is declared. If the variable is not declared, the Korn shell assumes it isa global variable.

    Not declaring a variable in the function: Allows you to use the variable globally.

    If a Korn shell program contains a local variable and a global variable with the same name, the localvariable takes priority. For example, suppose that a Korn shell program contains both a localvariable and a global variable named var. When you run the Korn shell program, the var variablestores the value assigned to the var local variable. Listing 65 shows how to use a global variable in

    a function:Listing 65: Using Global Variables

    #!/bin/kshfunction fun{ echo "In function, fun, value for first command line parameter is: $0\n" var="In function, my name is Jack."}typeset varvar="In calling program, my name is John."echo "Value of variable, var, is: $var\n"echo "In calling program, value for first command line parameter is: $0\n"funecho "Value of variable, var, is: $var\n"

    echo "In calling program, value for first command line parameter is: $0\n"

    The above listing uses the var global variable, which is defined in both the fun function, and in theglb Korn shell program. The listing also uses the first command line parameter along with thestatement that invokes the function or program.

    When invoked, the fun function changes the value of the var variable. Figure 67 shows the outputof Listing 65, which uses a global variable to access the value of a variable:

    Working with Korn Shell 11

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    14/18

    Figure 67:Accessing a Global VariableCalling a Function Recursively

    A function that calls itself is referred to as a recursive function. A recursive function always contain a

    condition which, when met, stops the functions execution.

    A recursive function increases CPU overhead because the function increases the number offunction calls, but it also allows you to create simple programs with minimal code. Listing 66 showsa recursive function that calculates the factorial of a number:

    Listing 66: Using a Recursive Function

    #!/bin/kshinteger prodfunction fact{integer value=0integer param=0

    if (( $1 == 1 ))thenprod=1elseparam=`expr $1 1`fact $paramvalue=`expr $1 \* $prod`prod=$valuefi}if [[ z $1 ]]thenprint "Please enter a number\n"exitfiif (( $1

  • 8/12/2019 Unix Scripting - How Functions Work

    15/18

    Listing 66 that displays the factorial of a number:

    Figure 68:Factorial of a Number Using Recursive FunctionLoading a Function Automatically

    Loading a function automatically means that the function definition is saved in a separate locationthan the location where the calling program is saved. The calling program can make a reference toan automatically loaded function. The function is loaded in the Korn shells memory only when it iscalled. To load a function automatically, you specify the autoload keyword before the name of thefunction.

    To load a function automatically:

    Create a directory where you can store the function that you want to autoload. Forexample, you might create a directory in the /usr/lib directory. The commands to create theautoloaded functions directory are:

    $ cd /usr/lib$ mkdir autoloaded

    1.

    Open the Korn shell environment file, profile, in the /etc directory. Use the Korn shell builtinvariables, PATH and FPATH, to autoload a function. The commands that you need toappend to the environmental file, profile, are:

    FPATH=/usr/lib/autoloadedPATH=$PATH:$FPATHexport FPATH PATH

    2.

    Store the function that you want to autoload in the /usr/lib/autoloaded directory. Forautomatic loading, the function name must be the same as the file name.

    3.

    When the Korn shell interpreter reads the autoload keyword, the interpreter searches the shellsenvironment file for the FPATH builtin variable. This variable provides the interpreter with the pathto the directory that stores the autoloaded functions. Listing 67 shows how to load a functionautomatically:

    Listing 67: Loading a Function Automatically

    Working with Korn Shell 13

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    16/18

    #!/bin/kshinteger prodautoload factif [[ z $1 ]]thenprint "Enter an integer value\n"exitfiif (( $1

  • 8/12/2019 Unix Scripting - How Functions Work

    17/18

    Figure 69:Factorial of a Number Using AutoLoaded FunctionPassing Parameters

    You can pass parameters between a function and a calling program either by reference or value.Passing parameters by reference means that the address of a parameter is passed between the

    calling program and the function. Parameters that you want to pass by reference should bepreceded by the Korn shell keyword, nameref, in the body of the function. The parameter should bespecified without the dollar ($) sign. Listing 69 shows how to pass parameters by reference,exchanging the values of two parameters:

    Listing 69: Passing Parameters by Reference

    #!/cathy/bin/kshfunction swap{integer tnameref m=$1nameref n=$2t=$mm=$nn=$t

    }if [[ z $1 && z $2 ]]thenprint "Error, both the parameters are missing\n"exitfiif [[ z $1 || z $2 ]]thenprint "Error, one of the parameters is missing\n"exitfia=$1b=$2print "Before function call, the values are val1: $a and val2: $b"swap a bprint "After function call, the values are val1: $a and val2: $b"

    The above listing shows how to exchange the values of the $1 and $2 command line parameters.The listing alsochecks whether both parameters are supplied.

    The $1 and $2 command line parameters are stored in the a and b variables, respectively. Theaddresses of the variables are passed to the swap function. Figure 610 shows the output of Listing69 that swaps the values of two parameters:

    Working with Korn Shell 15

    Reprinted for [email protected], IBM Skil lSoft, Ski llSoft Corporation (c) 2003, Copying Prohibited

  • 8/12/2019 Unix Scripting - How Functions Work

    18/18

    Figure 610:Swapping Parameter ValuesUnlike passing parameters by reference, passing parameters by value passes the actual variable. Ifa parameter is passed by reference, it acts as a global variable. A parameter passed by value actsas a local variable. If you are working with a complex program that needs global variables, passinga parameter by reference is preferred.

    Working with Korn Shell 16

    R i t d f ib h d @i ib IBM Skil lS ft Ski llS ft C ti ( ) 2003 C i P hibit d