Solved

Sending an array from Function 1 to Function 2

Posted on 2009-03-31
7
719 Views
Last Modified: 2013-12-26
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.
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

0
Comment
Question by:davidiwharper
  • 3
7 Comments
 

Author Comment

by:davidiwharper
Comment Utility
Increasing points value
0
 

Accepted Solution

by:
davidiwharper earned 0 total points
Comment Utility
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.
0
 

Author Comment

by:davidiwharper
Comment Utility
There is now a (self-generated) solution for this issue. Please cancel the deletion request if appropriate.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now