LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-20-2014, 12:34 AM   #1
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Rep: Reputation: Disabled
How to return an array in bash


Hello,

As I know this how we pass a array in the function call..

Code:
func1{
declare -A TEST
TEST["apple"]="Doctor"
TEST["mango"]="Summer"
TEST["pine"]="fine"
func2 "$(declare -p TEST)" 
}

func2 {
eval "declare -A LIST="${1#*=}
}


How to return an array to the calling function ?
 
Old 01-20-2014, 02:12 AM   #2
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 80
As i know it the correct way is :

Code:
function funtion_name {}

OR

func1() {
declare -a TEST
#define array values here
func2($var)   # to call a function

}

func2() {
somecode
}
EDIT:
Code:
to return array values:

echo "${array_name[@]}"
 array count:
echo "${#array_name[@]}"

Last edited by SAbhi; 01-20-2014 at 02:21 AM.
 
Old 01-24-2014, 02:54 AM   #3
jags1984
Member
 
Registered: Mar 2013
Posts: 83

Original Poster
Rep: Reputation: Disabled
I dont think this is the correct way...

For Variables in shell script we do this way


Code:
#Function call
func1(){
func1 $var1 $var2  returnValue
ReturnValue=$returnValue
#Some code
---
}


func1(){
Value1=$1
Value2=$2
#Some code

eval "$3=$Value3"
return 0
}



I am looking find out same way for Associative arrays in shell script.

Array can be passed in function as argurment using -p option
Code:
func1{
declare -A TEST
TEST["apple"]="Doctor"
TEST["mango"]="Summer"
TEST["pine"]="fine"
func2 "$(declare -p TEST)" 
}

func2 {
eval "declare -A LIST="${1#*=}
}

But I dont no how to return the associative array in SHELL script.
 
Old 01-24-2014, 04:22 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389Reputation: 2389
@jags1984: Is this what you are looking for:
Code:
#!/bin/bash

function func1() {
  declare -A TEST

  TEST["apple"]="Doctor"
  TEST["mango"]="Summer"
  TEST["pine"]="Fine"

  func2
}

function func2() {
  # copy associative array TEST to LIST
  LISTTMP=$(declare -p TEST)
  eval "declare -A LIST="${LISTTMP#*=}

  echo "show LIST array entries"
  echo "${LIST["apple"]}"
  echo "${LIST["mango"]}"
  echo "${LIST["pine"]}"
}

func1

exit 0
Example run:
Code:
$ ./jags1984
show LIST array entries
Doctor
Summer
Fine
 
1 members found this post helpful.
Old 01-24-2014, 06:51 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,459

Rep: Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852Reputation: 2852
Well I know everyone is coming up with solutions here, but personally I would ask myself am I using the right tool or am I possibly overcomplicating what I actually need to do??
 
Old 01-24-2014, 10:32 AM   #6
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: CentOS
Posts: 3,286

Rep: Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423Reputation: 1423
Ignoring the extreme insecurity of using eval, you should be able to pass the array back the same way you passed it in.
Code:
#!/bin/bash
func1() {
  declare -A TEST
  TEST["apple"]="Doctor"
  TEST["mango"]="Summer"
  TEST["pine"]="Fine"

  RET="$(func2 "$(declare -p TEST)")"
  eval "declare -A NEWLIST=${RET#*=}"
  for X in ${!NEWLIST[*]}; do echo "$X: ${NEWLIST[$X]}"; done
}

func2() {
  eval "local -A LIST="${1#*=}
  LIST["banana"]="Tropical"
  declare -p LIST
}
 
Old 01-26-2014, 12:04 AM   #7
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 80
Quote:
I dont think this is the correct way...
Oh is it!! then you dont need my input.. thanks
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
BASH-Adding array element: Naming issue using array[${#array[*]}]=5 calvarado777 Programming 8 07-26-2013 09:48 PM
[SOLVED] split a string into array in bash return wrong size xeon123 Linux - Newbie 2 03-21-2011 07:16 AM
how to return array from function c++ vbx_wx Programming 3 11-09-2010 04:21 PM
bash: use file as input into array, parse out other variables from array using awk beeblequix Linux - General 2 11-20-2009 10:07 AM
C can 't return a character array Linh Programming 5 06-18-2003 04:58 AM


All times are GMT -5. The time now is 05:30 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration