Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Sending an array from Function 1 to Function 2

Posted on 2009-03-31
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
	# Create a string containing "dbs[*]"
	local ccReadArray="$2[*]"
	# assign loc_array value to $dbs[*]} using indirect variable reference
	# Resetting IFS to default
	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!";
		elif [ $? -eq 1 -a $ccReturn -ne 1 ]; then
	return $ccReturn;
} # configCheck

Open in new window

Question by:davidiwharper
  • 3

Author Comment

ID: 24046175
Increasing points value

Accepted Solution

davidiwharper earned 0 total points
ID: 24114027
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

This works as expected.

Author Comment

ID: 24114030
There is now a (self-generated) solution for this issue. Please cancel the deletion request if appropriate.

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
cron job says it ran, no results 25 121
Problem to start Neon 20 120
Need a shell script to zip the logfiles which are older than the current date 8 52
parse a file and get data out 11 76
This Windows batch file is useful for organizing image files from a digital camera or other source, but can have many other uses.  It simply renames the file(s) to match their create date.  For example, if you took a picture today at 1:40pm and the …
Utilizing an array to gracefully append to a list of EmailAddresses
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

840 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