Question : Sending an array from Function 1 to Function 2

Hi all.

I am trying to send an array from a parent function (not shown) to a child function (see below, configCheck). When I call configCheck from the main script, it works fine. But when I call it from within menuBdcGuide, there is an error.

The log shows that the split has not happened. When configCheck is called directly, the log shows:

Checking database accounts
Checking database configuration

But when it is run from another function, instead what we see is:

Checking database (accounts configuration)

Which means that the code to create the array has not worked correctly.

How can I fix this so I can call configCheck from within my other function? My guess is that the IFS might have to be different. This would be fine, because configCheck is never called directly; it's only ever used from within functions.
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
configCheck ()
{
 
	# checks to see whether a db key exists
	# version 1.0
	#
	# required inputs:
	# ccKey - key to check for
	# ccDbs - databases to check in
	#
	# returns:
	# 0 - key does not exist
	# 1 - key exists
	# 1001 - function error
	#
	# instructions for use:
	# testDbs=( accounts configuration )
	# configCheck "myKey" testDbs
	# if [ $? -eq 1 ]; then
	#	echo "Key exists!"
	# fi
	
	local ccKey=$1
	local ccDbs=;
	local ccReturn=1001
 
	# Setting the shell's Internal Field Separator to null
	# from http://www.unix.com/shell-programming-scripting/61370-bash-ksh-passing-array-function.html
 
	local OLD_IFS=$IFS
	IFS=''
 
	# Create a string containing "dbs[*]"
	local ccReadArray="$2[*]"
 
	# assign loc_array value to $dbs[*]} using indirect variable reference
	ccDbs=(${!ccReadArray})
 
	# Resetting IFS to default
	IFS=$OLD_IFS
 
	writeLog "Checking database for key $ccKey."
 
	for element in $(seq 0 $((${#ccDbs[@]} - 1)));
	do                  #  ${#ccDbs[@]}
			    #+ gives number of elements in the array.
			    #
			    #  Question:
			    #  Why is  seq 0  necessary?
			    #  Try changing it to seq 1.
		writeLog "Checking database ${ccDbs[$element]}."
		runCommand "db ${ccDbs[$element]} show $ccKey" "quiet"
		if [ $? -eq 0 ]; then
			# previous data exists
			writeLog "Configuration key detected in ${ccDbs[$element]} database!";
			ccReturn=1;
		elif [ $? -eq 1 -a $ccReturn -ne 1 ]; then
			ccReturn=0;
		fi
 
	done
 
	return $ccReturn;
 
} # configCheck
Open in New Window Select All

Answer : Sending an array from Function 1 to Function 2

I have a solution, which was not clear in the code given at the beginning.

Creating the array to pass thus:
local myArray=(accounts configuration)

... doesn't work as expected. What DOES work is creating the array as follows:

local myArray[0]=accounts
myArray[1]=configuration

This works as expected.
Random Solutions  
 
programming4us programming4us