Faculty of Engineering and Mathematical Sciences 
Not logged in (login)

help4407


This forum is provided to promote discussion amongst students enrolled in Open Source Tools and Scripting (CITS4407).
 
Options:
RSS cloud
Jump to:

Returning Multiple Values: string manipulation vs global variables vs alternatives.

6 of 390 articles shown, currently no other people reading this forum.
photo
From: Lee dB.
Date: Mon 25th May 2020, 12:09pm
Actions: 
        Login-to-reply

 

Hi everyone,

I'm looking at how to better structure the shell scripts for my project, and one thing I've 
found challenging is how to best pass multiple values between functions.

One way I've used is to concatenate the multiple values together, echo them to stdout, then 
deconstruct them back into variables in the receiving function. This seems expensive, 
especially since this might require multiple forks. It's also not great for code reuse as 
it's hard to put a function together that can do this multiple times (because how would a 
helper function to deconstruct a string for passing multiple parameters itself return 
multiple parameters to the calling function?).

So that leaves one of the other options being global variables, which are traditionally 
considered to Be a Bad Thing. But I'm not sure what other alternatives there are whilst 
still making good use of functions for code modularity.

Other languages that don't allow multiple return values usually at least allow returning a 
pointer, or having parameters passed in by reference, so there are other ways of tackling 
this.

Is there another mechanism I've missed for returning multiple values from functions that's 
easy on the eyes, maintainable and efficient? What are the prevailing thoughts on global 
variables in shell scripting achieving this, versus string processing?

Thanks,
Lee

Returning Multiple Values: string manipulation vs global variables vs alternatives.

photo
From: Christopher M.
Date: Tue 26th May 2020, 7:53am
Actions: 
        Login-to-reply

 

"Lee de Byl" <10*0*8*[email protected]*u*e*t*u*a*e*u*a*> wrote:

> Is there another mechanism I've missed for returning multiple values from functions that's 
> easy on the eyes, maintainable and efficient? What are the prevailing thoughts on global 
> variables in shell scripting achieving this, versus string processing?

Not a well supported feature (in bash).

You could have a function set multiple return values in different environment variables - similar to way 
functions receive parameters as $1, $2, ...
you could have them return values through R1, R2, ...

Returning Multiple Values: string manipulation vs global variables vs alternatives.

photo
From: Lee dB.  O.P.
Date: Tue 26th May 2020, 1:49pm
Actions: 
        Login-to-reply

 

Thanks Chris, that's the approach I've ended up taking. It just seemed wasteful concatenating variables into strings, 
then deconstructing them again in the receiver, particularly where forks are involved.

Returning Multiple Values: string manipulation vs global variables vs alternatives.

photo
From: Christopher M.
Date: Tue 26th May 2020, 5:44pm
Actions: 
        Login-to-reply

 

"Lee de Byl" <10*0*8*[email protected]*u*e*t*u*a*e*u*a*> wrote:

> Thanks Chris, that's the approach I've ended up taking. It just seemed wasteful concatenating variables into strings, 
> then deconstructing them again in the receiver, particularly where forks are involved.

Sounds good.
By 'forks involved', do you mean running the external processes, spawned by bash?

Returning Multiple Values: string manipulation vs global variables vs alternatives.

photo
From: Lee dB.  O.P.
Date: Mon 1st Jun 2020, 9:23am
Actions: 
        Login-to-reply

 

Hi Chris,

Yes, I was referring to external processes, as well as the creation of subshells. Is my understanding correct that both of these 
Involve forks (at least on Unix & Linux)?

Thanks!
Lee

Returning Multiple Values: string manipulation vs global variables vs alternatives.

photo
From: Christopher M.
Date: Mon 1st Jun 2020, 11:15am
Actions: 
        Login-to-reply

 

"Lee de Byl" <10*0*8*[email protected]*u*e*t*u*a*e*u*a*> wrote:

> Yes, I was referring to external processes, as well as the creation of subshells. Is my understanding correct that both of these 
> Involve forks (at least on Unix & Linux)?

Yes, usually external processes involve forking to get a new child process.
In some cases, the shell can determine if a subshell really needs a new process, and so there may not be a fork in all cases.
This Page


Program written by: [email protected]
Feedback welcome
Last modified: 11:27am Sep 21 2020