Передача аргументов по ссылке для функций в оболочке bash

2005-07-19 at 01:45 | Posted in uncategorized | Leave a comment

Есть несколько путей для этого. Стоит заметить что здесь ссылка на переменную — это не указатель (адрес), а символьное имя переменной, что довольно необычно. Что делает встроенная команда eval — будет понятно из листинга.

$ vi test
> function setupComponent() {
>    # DEBUG
>    echo "setting up current component..."
>
>    # C: componentRef = component (?)
>    # bash: $1="generator"
>    # bash: \$$1="$generator"
>    # bash: componentRef="$generator"
>    local componentRef=\$$1
>    echo "componentRef is $componentRef"
>
>    # C: tmp = *component (?)
>    # bash: eval "tmp=\$$1" is "tmp=$generator" is "tmp=/scms/generation/file_generator.sh"
>    # bash2: "eval tmp=\$$1" or "tmp=${!1}
>    tmp=${!1}
>    echo "tmp is $tmp"
>    echo "\${!1} is ${!1}"
>
>    # C: comp = *componentRef (?)
>    # bash: eval "comp=$generator" is "comp=scms/generation/file_generator.sh"
>    eval comp=$componentRef
>
>    # bash: $comp="scms/generation/file_generator.sh"
>    echo "$componentRef is $comp"
>
>    # C: *component = "blabla" (?)
>    # bash: eval "$1=blabla" is "generator=blabla"
>    eval $1=blabla
> }
>
> generator="scms/generation/file_generator.sh"
> echo "before: $generator"
> setupComponent generator
> echo "after: $generator"
>
> :wq
$ chmod 755 test
$ ./test
before: scms/generation/file_generator.sh
setting up current component...
componentRef is $generator
tmp is scms/generation/file_generator.sh
${!1} is scms/generation/file_generator.sh
$generator is scms/generation/file_generator.sh
after: blabla

Leave a Comment »

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.
Entries and comments feeds.

%d bloggers like this: