Flylib.com

Books Software

 
 
 

Displaying Function Definitions


Displaying Function Definitions

Use the declare (or typeset ) utility to display the names and definitions of defined functions. Use the F option to display the names of defined functions, and the f option to display the definitions of defined functions, as shown in Figure 13.2. If you follow the option with the name of a function, declare shows information about that function only. If you do not include a function name, declare shows information about all defined functions.

start figure


declare -F


ucname


lcname


/home/jsmith $


declare -f


function ucname()


{


echo   tr '[:lower:]' '[:upper:]'


}


function lcname()


{


echo   tr '[:upper:]' '[:lower:]'


}


/home/jsmith $


declare -f ucname


function ucname()


{


echo   tr '[:lower:]' '[:upper:]'

}

/home/jsmith $

end figure

Figure 13.2: Use the declare utility to display the names and definitions of functions.

In V5R2, Qshell defines the alias functions as the equivalent of declare -f. Figure 13.3 illustrates this concept.

start figure


functions


function lcname()


{


echo   tr '[:upper:]' '[:lower:]'


}


/home/jsmith $

end figure

Figure 13.3: The functions alias lists all defined functions.

When you use the declare utility to display the definition of a function, do not expect to see the same syntax that was used when the function was defined. For example, in Figure 13.4, the rev (reverse) function is defined with the old, Bourne-shell syntax, but the declare utility (executed through the functions alias) displays the function definition in the new function definition syntax. Notice, too, that the $ n notation for positional parameters is used when defining the function, but the displayed definition uses the ${ n } notation.

start figure


rev() {


>


echo


>


}


/home/jsmith $


rev a b


b a


/home/jsmith $


functions rev


function rev()


{


echo


}


/home/jsmith $

end figure

Figure 13.4: The internal definition of a function might not match the original definition.



Deleting Function Definitions

To "undefine" a function, use the unset utility with the -f option. Figure 13.5 illustrates the unset command.

start figure


lcname $filename


/qsys.lib/mylib.lib/myfile.file/mymbr.mbr


/home/jsmith $


unset -f lcname


/home/jsmith $


lcname $filename


qsh: 001-0019 Error found searching for command lcname.


No such path or directory.

end figure

Figure 13.5: After the unset utility runs, Qshell no longer finds the lcname function.



Parameters

You may pass arguments to a function, just as you pass them to a script. The function can reference the passed values as positional parameters, using the same $n or ${n} notation that scripts use.

Special parameters also have appropriate values within a function. For example, the special parameter $# used in the nonfunction portion of a script returns the number of arguments passed to the script. When $# is used within a function, it returns the number of arguments passed to the function. The special parameter $0 is an exception. It does not have a special value within a function. Whether it's used in a function or in the nonfunction section of a script, $0 always returns the script name . (For a full list of special parameters, see chapter 5.)

The script in Figure 13.6 illustrates the use of positional parameters in a function. In it, the stripcmt.qsh script removes comments from stdin and writes to stdout . The script contains function delcmt , which deletes the first pound sign and everything following it from the first positional parameter. Notice that function delcmt refers to the first positional parameter twice. Also notice that the stripcmt.qsh script is not foolproof; it assumes that the first pound sign in a line indicates a comment. That might not be true.

start figure


cat stripcmt.qsh


function delcmt


{


if [[ -z "" ]]


then


echo ""


return 0


fi


echo ""  cut -d'#'-f1


return 0


}


# end function delcmt


while read line


do


delcmt "$line"


done


/home/jsmith $


cat bu.qsh


# Backup script.


#


# Copy the files listed in the positional parameters to


# the bu directory


if [[ -e bu ]]           # bu exists; be sure it's a directory


then


if [[ ! -d bu ]]


then


print -u2 "bu is not a directory  backup


aborted."


exit 1


fi


else


mkdir bu


fi


for file


do


cp $file bu/$file.bu


done


/home/jsmith $


stripcmt.qsh <bu.qsh


if [[ -e bu ]]


then


if [[ ! -d bu ]]


then


print -u2 "bu is not a directory  backup aborted."


exit 1


fi


else


mkdir bu


fi


for file


do


cp $file bu/$file.bu


done


/home/jsmith $

end figure

Figure 13.6: Stripcmt.qsh uses a function to remove comments from input lines.