zazzybob's solution is an elegant and simple solution.
In response to recursion within a Korn shell function, there are two points to consider. All variables, including the current working directory, are global unless specifically defined with the function which then makes them local within the function.
Code:
typeset FILENAME=/etc/hosts
Function ()
{
FILENAME=/etc/resolv.conf
echo $FILENAME
}
echo $FILENAME
Function
echo $FILENAME
would produce
/etc/hosts
/etc/resolv.conf
/etc/resolv.conf
However,
Code:
typeset FILENAME=/etc/hosts
Function ()
{
typeset FILENAME
FILENAME=/etc/resolv.conf
echo $FILENAME
}
echo $FILENAME
Function
echo $FILENAME
would produce
/etc/hosts
/etc/resolv.conf
/etc/hosts
A quick and dirty way to make variables local to the function INCLUDING the working directory is to execute the function in its own shell by placing parenthesis within the function brackets. The function will get its own copy of the environment when called and any changes, including the directory, to that environment will not be reflected in the calling script which is its own function.
Code:
typeset FILENAME=/etc/hosts
Function ()
{ (
FILENAME=/resolv.conf
echo $FILENAME
) }
echo $FILENAME
Function
echo $FILENAME
would produce
/etc/hosts
/etc/resolv.conf
/etc/hosts