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).

Assignment 2 and Clarifications.
 
Options:
RSS cloud
Jump to:

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

1 of 390 articles shown, currently no other people reading this forum.
photo
From: Lee dB.
Date: Mon 25th May, 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

Related articles

Returning Multiple Values: string manipulation vs global variables vs alternatives. (all 6) RSS
├─ THIS   Mon 25th May, 12:09pm, Lee dB.
├─ reply 1   Tue 26th May, 7:53am, Christopher M.
├─ reply 2   Tue 26th May, 1:49pm, Lee dB.  O.P.
├─ reply 3   Tue 26th May, 5:44pm, Christopher M.
├─ reply 4   Mon 1st Jun, 9:23am, Lee dB.  O.P.
└─ reply 5   Mon 1st Jun, 11:15am, Christopher M.
This Page


Program written by: [email protected]
Feedback welcome
Last modified:  8:27am May 24 2020