function fxn
{
echo "target in build_ndm is $target"
}
function main
{
target=${server_type}
echo "target in main is $target"
fxn
}
In the above the value of target is not being propagated to the function while ideally it should be since typeset variables are visible in sub functions
The issue is that "$target" is assigned some unspecified value "$server_type", which may contain either an empty value or might be undefined or maybe contains characters with a special meaning to the shell so that they become interpreted because the variable "$server_type" is used without proper quoting.
In addition, you seem to have no firm grasp of the variable scope in shell languages. The "typeset" keyword just declares a variable. You need to "export" them to make them available in subshells.
To top it off the concept of using a variable from the calling function is flawed in itself: if you want to pass some value to a subfunction use some explicit mechanism, like passing a parameter:
function sub1
{
target="$1"
print - "target in sub1() is: $target"
return 0
}
function caller
{
target="some-value"
print - "target in caller() is: $target"
sub1 "$target"
return 0
}
Finally, you have neither supplied complete code (because the snippet you presented seems to be only a small incomplete part) and you haven't shown any output the complete script has produced. How are we supposed to find out what went wrong with such incomplete data? We're admins, not psychics.
The original thread content ( as the original poster was rude enough to edit and remove...) :
function fxn
{
echo "target in build_ndm is $target"
}
function main
{
target=${server_type}
echo "target in main is $target"
fxn
}
In the above the value of target is not being propagated to the function while ideally it should be since typeset variables are visible in sub functions