Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Machines that have broken down will work perfectly when the repairman arrives.


devel / comp.unix.shell / Re: How do you pass a variable from one function into another function?

SubjectAuthor
* How do you pass a variable from one function into another function?Chris Roberts
+- Re: How do you pass a variable from one function into anotherKaz Kylheku
+- Re: How do you pass a variable from one function into another function?Kenny McCormack
`- Re: How do you pass a variable from one function into anotherLew Pitcher

1
How do you pass a variable from one function into another function?

<51e84159-f764-4eba-b807-6154dc88a6b6n@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3953&group=comp.unix.shell#3953

  copy link   Newsgroups: comp.unix.shell
X-Received: by 2002:ac8:5045:: with SMTP id h5mr4777153qtm.178.1623956869449; Thu, 17 Jun 2021 12:07:49 -0700 (PDT)
X-Received: by 2002:a05:620a:214e:: with SMTP id m14mr4128795qkm.496.1623956869213; Thu, 17 Jun 2021 12:07:49 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.uzoreto.com!tr1.eu1.usenetexpress.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.unix.shell
Date: Thu, 17 Jun 2021 12:07:49 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=4.79.195.162; posting-account=tSqayAoAAADEbq2bVxu4ODgY7tmW5dUx
NNTP-Posting-Host: 4.79.195.162
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <51e84159-f764-4eba-b807-6154dc88a6b6n@googlegroups.com>
Subject: How do you pass a variable from one function into another function?
From: thecjg...@gmail.com (Chris Roberts)
Injection-Date: Thu, 17 Jun 2021 19:07:49 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 25
 by: Chris Roberts - Thu, 17 Jun 2021 19:07 UTC

Perhaps someone can assist?
How do you pass a variable from one function into another function?
and/or make it a "global" variable from within a function.
Not sure how "c" got assigned a value of "0"
I want it to say 200.
My actual script has to calculate the variable within a function then send that to various other functions that may have to send variables to yet other functions.

MyUbuntu$ cat myscript
#!/usr/bin/bash
this(){
( a="hi"
b=3
c=200
) }
that(){
( echo "c in that called from this: $((this ${c}))"
) }
that

#OUTPUT:
MyUbuntu$ myscript
c in that called from this: 0

Re: How do you pass a variable from one function into another function?

<20210617123303.475@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3955&group=comp.unix.shell#3955

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 563-365-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: How do you pass a variable from one function into another
function?
Date: Thu, 17 Jun 2021 19:41:29 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 78
Message-ID: <20210617123303.475@kylheku.com>
References: <51e84159-f764-4eba-b807-6154dc88a6b6n@googlegroups.com>
Injection-Date: Thu, 17 Jun 2021 19:41:29 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="f629c8e6b16ac9acc3678bc5a54d887b";
logging-data="13322"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/khnqrbZboxIjQaMP6o1gzWK5iwyRW4Ak="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:srhmOHXsXWdhzEeDYg0s8cNEBnk=
 by: Kaz Kylheku - Thu, 17 Jun 2021 19:41 UTC

On 2021-06-17, Chris Roberts <thecjguy1@gmail.com> wrote:
> Perhaps someone can assist?
> How do you pass a variable from one function into another function?

Bash implements dynamic scoping. When you declare a file "local" inside
a function, this is a dynamic binding which is visible to functions
which are then called from that function. The dynamic binding goes away
when the binding function terminates. This reveals any earlier binding
which that variable had.

foo=42

print_foo()
{
echo "foo is $foo"
}

override_foo()
{
local foo=73
print_foo
}

print_foo
override_foo
print_foo

Output:

foo is 42
foo is 73
foo is 42

After override_foo terminates, the 42 value is restored.

> #!/usr/bin/bash
> this(){
> (
> a="hi"
> b=3
> c=200
> )
> }
> that(){
> (
> echo "c in that called from this: $((this ${c}))"
> )
> }
> that

But "that" is not being called from "this"; your message is misleading.

Anyway, let us continue. Dynamic binding means that the most recent
local binding of a variable is also visible to the eval function.

In a function if we do this:

local foo=42
local foo_name="foo"

eval echo '$'$foo_name

it will print 42. You see where this is headed? We can have a computed
variable name, represented as a string. We can stick a dollar sign on
that string and pass it to eval, to get the dynamic value of that
variable.

So if you put all this together, you have a way to literally "pass a
variable" to another function.

You can bind a variable like foo, and then pass the name "foo"
to another function. That other function can do the eval trick
and obtain the value of foo. De facto, you have passed a variable
using a "call by name" parameter passing strategy.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: How do you pass a variable from one function into another function?

<sag9if$2avqt$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3956&group=comp.unix.shell#3956

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: How do you pass a variable from one function into another function?
Date: Thu, 17 Jun 2021 19:59:11 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <sag9if$2avqt$1@news.xmission.com>
References: <51e84159-f764-4eba-b807-6154dc88a6b6n@googlegroups.com>
Injection-Date: Thu, 17 Jun 2021 19:59:11 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2457437"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Thu, 17 Jun 2021 19:59 UTC

In article <51e84159-f764-4eba-b807-6154dc88a6b6n@googlegroups.com>,
Chris Roberts <thecjguy1@gmail.com> wrote:
>Perhaps someone can assist?
>How do you pass a variable from one function into another function?
>and/or make it a "global" variable from within a function.
>Not sure how "c" got assigned a value of "0"
>I want it to say 200.
>My actual script has to calculate the variable within a function then send
>that to various other functions that may have to send variables to yet
>other functions.

Lose the parens (That is, the ( and ))

() makes things run in a subshell, which means changes do not propagate
back.

Observe:

$ unset foo
$ (foo=42)
$ echo $foo

$

Obviously, if we left out the () on the assignment, the final echo would
show the value 42.

--
1) The only professionals who refer to their customers as "users" are
computer guys and drug dealers.
2) The only professionals who refer to their customers as "clients" are
lawyers and prostitutes.

Re: How do you pass a variable from one function into another function?

<sagckj$nqs$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=3957&group=comp.unix.shell#3957

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: lew.pitc...@digitalfreehold.ca (Lew Pitcher)
Newsgroups: comp.unix.shell
Subject: Re: How do you pass a variable from one function into another
function?
Date: Thu, 17 Jun 2021 20:51:31 -0000 (UTC)
Organization: The Pitcher Digital Freehold
Lines: 171
Message-ID: <sagckj$nqs$1@dont-email.me>
References: <51e84159-f764-4eba-b807-6154dc88a6b6n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 17 Jun 2021 20:51:31 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="179a99f2d1a917a88926a7d5837bf9de";
logging-data="24412"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/MwOG+4KkwVngtbrudWXy+lSvf+kHh94Q="
User-Agent: Pan/0.139 (Sexual Chocolate; GIT bf56508
git://git.gnome.org/pan2)
Cancel-Lock: sha1:NJt6QbVs364ZFaopunSWoGLGKnM=
 by: Lew Pitcher - Thu, 17 Jun 2021 20:51 UTC

On Thu, 17 Jun 2021 12:07:49 -0700, Chris Roberts wrote:

> Perhaps someone can assist?
> How do you pass a variable from one function into another function?
> and/or make it a "global" variable from within a function.
> Not sure how "c" got assigned a value of "0"
> I want it to say 200.
> My actual script has to calculate the variable within a function then send that to various other functions that may have to send variables to yet other functions.
>
> MyUbuntu$ cat myscript
> #!/usr/bin/bash
> this(){
> (
> a="hi"
> b=3
> c=200
> )
> }
> that(){
> (
> echo "c in that called from this: $((this ${c}))"
> )
> }
> that
>
> #OUTPUT:
> MyUbuntu$ myscript
> c in that called from this: 0

Some hints to remember:

1) ordinary variables (not "local" variables) are shared
between the function and it's caller
Thus
function A(){
b=7;
}
b=5;
A
echo $b
will show that the the A function updated the mainline variable b

2) local variables exist only within the function that they are declared in
Thus
function Q() {
local r;
r=5;
echo "r is set to $r"
}
r=10;
Q
echo $r
will show that, while the Q function used the variable r, it did not update
the /mainline/ variable r

3) functions can accept arguments, and the values for those arguments
can come from the caller
Thus
function B(){
b=$1;
echo "Setting b to $1"
}
b=5;
B 100
echo $b
will show that the B function updated the mainline variable b with
a value provided by the call to B

4) functions can return values in their stdout
Thus
function C(){
echo 1000;
}
b=$(C)
echo $b
will show that the C function provided the value stored in the
mainline variable b

5) lists of commands executed between left and right parenthesis
do NOT share an environment with commands outside of the parenthesis
Thus
b=8;
(b=100;echo "Set b=$b";)
echo "b = $b"
will show that the value of the variable b /outside/ of the parenthesis
did not change even though the value of the variable b /inside/ of the
parenthesis did

6) lists of commands executed between left and right brace brackets share
an environment with commands outside of the brace brackets
Thus
b=8;
{ b=100;echo "Set b=$b"; }
echo "b = $b"
will show that the value of the variable b /outside/ of the brace brackets
changed to the value of the variable b /inside/ of the brace brackets

7) there are two forms of "command substitution":
$(command)
and
`command`
In either case, the shell replaces the command substitution with the stdout
of the executed command
Thus,
a=$(date)
assigns to the variable a the value that is the stdout of the date command.

8) In arithmetic expansion, the shell evaluates an arithmetic expression and substitutes
it's value for the expression
Thus
a=$((1 + 4))
assigns to the variable a the value that is the arithmetic result of 1 + 4

Now, on to your script

> this(){
> (
> a="hi"
> b=3
> c=200
> )
> }

Because you contain the assignments within parenthesis, hint #5 comes in to
play, and the assignments are, effectively, local to the parenthetical block.
This means that, as is, the assignments in your function this() will not
leak out of the function.

Had you coded the function as
this() {
a="hi"
b=3
c=200
}
then hint #1 would come into effect and the values set in a, b, and c by the
function this() would be shared with the caller's versions of a, b, and c.

> that(){
> (
> echo "c in that called from this: $((this ${c}))"
> )
> }

Since your this() function wrapped it's assignments in parenthesis, it cannot
change the value of variable c in the that() function.
Note that your echo uses an arithmetic expansion (hint #8), which expects
arguments consisting of names of variables, numeric constants, or arithmetic
operators. You give it arguments: the variable "this", and the value of an
unset variable ${c}. The ${c} evaluates to a null string, and is ignored.
The "this" is evaluated as an unset variable, and taken to have a value of 0.
Your that() function never invokes your this() function. You probably meant
to use a command substitution. If you had, your that() function would look
like

> that(){
> (
> echo "c in that called from this: $(this ${c})"
> )
> }

Note also that, in this form, the command substitution is provided the
value of variable c set (or, in this case, not) by the that() function.
Nowhere in your this() function do you look at the arguments provided
to the this() function, so even if c were set here, this() wouldn't
have actually seen it.

HTH
--
Lew Pitcher
"In Skills, We Trust"

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor