[SOLVED] bash how pass array as one of several parameters to a function
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
bash how pass array as one of several parameters to a function
Never mind, its the quotes that screwed it up, without them it works. I've really got to get this quoting problem sorted out.
How can I pass an entire array as just one of several parameters to a function?
If I pass the array I get just the first element. If I store the array as a variable, e.g. by
q="${mm[@]}"
and then pass "$q" I get the entire content, but it will not reconstitute as an array, i.e. if in the function
x=("$1")
it puts the entire content in x[0]
Last edited by porphyry5; 06-13-2011 at 02:08 PM.
Reason: Found the problem
The problem with this is that it's *extremely* unsafe. You must ensure that IFS is set to a character that you absolutely with 100% certainty know will not be used in the data itself. "echo" is most certainly the wrong tool to collapse an array this way since it only delimits with spaces.
The whole point of quoting is so that you don't end up merging and splitting your data in weird ways.
I recommend that you restructure your bash function so that all parameters after the Nth are the array. You can't pass in two arrays this way, though. If you *really* need to pass two arrays in, then probably the best way to go about it is to pass the arrays in by name and use (*shudder*) eval to dereference them.
And I suggest that you quit futzing around with Bash in this way and use a real scripting language of your choice!
No, I'm quite serious. I'm not being condescending, or cynical, or making any fun at your expense.
Yes, you might be able to prove that you can drive a hundred miles on a square stone wheel, but at the end of the day you will have "proved a point," but "accomplished nothing." And that is not what digital computers are for. When you have literally half-a-dozen powerful language tools at your fingertips on any Linux system, there is zero value in anything less than choosing the right tool for a particular job. I will refrain from suggesting which one, or why...
Last edited by sundialsvcs; 06-14-2011 at 06:35 AM.
And I suggest that you quit futzing around with Bash in this way and use a real scripting language of your choice!
No, I'm quite serious. I'm not being condescending, or cynical, or making any fun at your expense.
Yes, you might be able to prove that you can drive a hundred miles on a square stone wheel, but at the end of the day you will have "proved a point," but "accomplished nothing." And that is not what digital computers are for. When you have literally half-a-dozen powerful language tools at your fingertips on any Linux system, there is zero value in anything less than choosing the right tool for a particular job. I will refrain from suggesting which one, or why...
And I suggest, if you can't keep to the subject under discussion, that you make no post at all. But as you did, I'll respond to it.
Your post may or may not be "condescending", though I rather suspect that it is. It certainly can't be described as "cynical". Appropriate adjectives that come to mind include presumptuous and egotistical.
Further down, I note that you lack the courage of your conviction. Rather than forthrightly declare "your language preference that shall remain nameless", you coyly conceal that declaration behind the very word that denies making such declaration. That makes you look dishonest.
In the past year I have started 2 threads asking about scripting languages available with linux, and describing the kinds of projects for which I want to use them. These threads are http://www.linuxquestions.org/questi...macros-823126/ and http://www.linuxquestions.org/questi...n-bash-874827/. In the second thread, I specifically ask if "...nameless" was an appropriate tool for the purposes I had in mind. Where were you then?
Since then I have researched the matter further and found several rather obscure CL apps that make me very optimistic that I can achieve my objectives through bash, indeed the very script I'm writing will prove that point yes or no. Bash fits very well with my taste for minimalism, and distaste for bloatware. If the result is no, I may well consider "...nameless" along with others.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.