Use the
declare
(or
|
|
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 $
|
|
In V5R2, Qshell defines the alias functions as the equivalent of declare -f. Figure 13.3 illustrates this concept.
|
|
functions
function lcname()
{
echo tr '[:upper:]' '[:lower:]'
}
/home/jsmith $
|
|
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.
|
|
rev() {
>
echo
>
}
/home/jsmith $
rev a b
b a
/home/jsmith $
functions rev
function rev()
{
echo
}
/home/jsmith $
|
|
To "undefine" a function, use the unset utility with the -f option. Figure 13.5 illustrates the unset command.
|
|
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.
|
|
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
The script in Figure 13.6 illustrates the use of positional parameters in a function. In it, the stripcmt.qsh script
|
|
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 $
|
|