LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-13-2011, 01:55 PM   #1
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Rep: Reputation: 24
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
 
Old 06-13-2011, 02:11 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
First you have to pass the entire array as a single string using double quotes:
Code:
function_name "$(echo ${q[@]})"
then inside the function you have to rebuild the array using command substitution:
Code:
function_name() {
  x=( $(echo "$1") )
  echo ${#x[@]}
}
Hope this helps.
 
1 members found this post helpful.
Old 06-13-2011, 03:22 PM   #3
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by colucix View Post
First you have to pass the entire array as a single string using double quotes:
Code:
function_name "$(echo ${q[@]})"
then inside the function you have to rebuild the array using command substitution:
Code:
function_name() {
  x=( $(echo "$1") )
  echo ${#x[@]}
}
Hope this helps.
Thank you, yes. And it also works like this: in the script body
Code:
q="${mm[@]}"
showindex "$q" "$notes" "^EndOf.*$"
and in the function
Code:
x=($1)
 
Old 06-13-2011, 04:31 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
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.

Last edited by tuxdev; 06-13-2011 at 04:36 PM.
 
Old 06-14-2011, 06:32 AM   #5
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,649
Blog Entries: 4

Rep: Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934Reputation: 3934
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.
 
1 members found this post helpful.
Old 06-14-2011, 09:11 PM   #6
porphyry5
Member
 
Registered: Jul 2010
Location: oregon usa
Distribution: Slackware 14.1, Arch, Lubuntu 18.04 OpenSUSE Leap 15.x
Posts: 518

Original Poster
Rep: Reputation: 24
Quote:
Originally Posted by sundialsvcs View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I pass a char* array as a function paramter in c++? chinho Programming 3 01-28-2011 12:40 PM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
in C, how do I pass a pointer of an array of structures to a function? randomsel Programming 2 02-04-2008 06:23 PM
How to pass array to a function in Java so that it returns changed? kornerr Programming 1 09-19-2006 12:16 PM
Pass Parameters to php from bash iman00b Linux - Newbie 2 09-09-2004 05:30 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:59 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration