Status: method.
State: suggestion,draft,part,need_check.
Detailed description: One more method for passing arguments to bash function.
Outline: below under 'passing argument as pointer' i mean passing argument by name:
function f() { } declare var='abc' f var # not f "$var"This may be considered as analogy for pointers (in C), i think :-)
1. Passing argument as pointer to function sometimes may require redefinition it as local variable in order to preserve referred data. Though, generally pointer used to provide to function ability to modify caller's environment, in bash it may be used, to skip unnecessary copying of big argument, if we want to redefine all positional parameters to local variables (names 1, 2, 3,.. is bad whenever, and also positional parameters may be used for some tricks, so it's better, when they do not contain useful data). eval " $(declare -p $2 | sed -e's/^[^=]\+=/local -a shortopts=/' ); $(declare -p $3 | sed -e's/^[^=]\+=/local -a longopts=/'); " Above construction is, seemingly, the only way. To correctly redefine pointer to local variable we should honour several contraints: 1. we should not define any local variable before all pointers will be expanded, because otherwise there is a chance, that defined local variable name is match with some pointed to variable's name and we lose access to it. 2. we should preserve all special characters inside pointed data. To handle first constraint, we need `eval`. To handle second, we should properly quote expanded pointed to variable's value: ${[@]} - split elements correctly only during first expand, but in the second (done in `eval`) these boundaries will be lost and bash splits elements by 'metacharacters', not by IFS or anything else (info bash, ch-2). ${[*]} - my be used, perhaps, with IFS=',', and then.. something artificial to set up quoting by brace expansion. But this rarely works, though (i don't know working example). `declare` - seems, only way to obtain correct quoting without many evals and artificial tricks. But its output, perhaps, should be processed by some program to replace variable name to desired one.
Комментариев нет:
Отправить комментарий