Passing array to functions in ksh script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing array to functions in ksh script
# 1  
Old 12-22-2009
Passing array to functions in ksh script

Let me know if there is a way to pass array to a funtion in ksh script.


Code:
function isPresent
{
  typeset member
  member=$1
  dbList=$2
  echo '$1:' $1
  echo '$2' $dbList



The array will be at the second position....something like this

Code:
isPresent 12 <array>
if [ $? -ne 0 ]
then
  echo "12 not present":
else
  echo "12 present";
fi


Last edited by pludi; 12-22-2009 at 02:57 AM.. Reason: code tags, please...
# 2  
Old 12-22-2009
Code:
## In this example the array contents are passed as a whole

function isPresent
{ 
	[[ ${2} == *${1}* ]] && return 0 
	return 1
} 

set -A n "ABC 12 DEF" 

isPresent 12 "${n[*]}" 

if [[ $? -ne 0 ]]
then 
	echo "12 not present"
else 
	echo "12 present"
fi

Code:
## In this example the array is accessed directly as the function can see the array

function isPresent
{ 
	[[ ${n[*]} == *${1}* ]] && return 0 
	return 1
} 

set -A n "ABC 12 DEF" 

isPresent 13 

if [[ $? -ne 0 ]]
then 
	echo "12 not present"
else 
	echo "12 present"
fi

# 3  
Old 12-22-2009
You could use a nameref (ksh93):

Code:
#!/bin/ksh
function isPresent
{
  typeset member=$1
  nameref dbList=$2
  [[ ${dbList[member]} != "" ]]
}

letterarray=(a b c d e f g h i j k l m n o p q r s t u v w x y z)

if isPresent 12 letterarray
then
  echo "12 is present"
else
  echo "12 is not present"
fi

Output:
Code:
12 is present

# 4  
Old 12-23-2009

In bash or ksh93:

Code:
array()
{
  eval "array=( \"\${${1}[@]}\" )"

  printf "%s\n" "${array[@]}"
}

q=( a b c d e f g )

array q

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Passing output parameter(Oracle) to variable in ksh Script

Hello, I have researched and tried many way to pass OUT parameter to be stored in variable in KSH Script.Still not success, please help. Here is my Store Procedure. create procedure testout3(v_in varchar2,v_out OUT integer) as begin v_out := 1; end; Here is my testvout.ksh #!/bin/ksh... (1 Reply)
Discussion started by: palita2601
1 Replies

2. Shell Programming and Scripting

ksh While Loop - passing variables to functions

I'm reading a text file using a while loop but when I call a function from within this loop it exits that same iteration … even though there are many more lines in the file to be read. I thought it was something to do with the IFS setting but it appears that a function call (when run... (3 Replies)
Discussion started by: user052009
3 Replies

3. Shell Programming and Scripting

Array in Ksh Shell script

hi team, i have a file, which contains only variable and its value param.ksh --------- export A=123 export B=345 export C=567 export D=OPLI export E=OL89PO From shell script, i am invoking this file and use the value of this variable. Now there are 5 variable in above file. Before i... (1 Reply)
Discussion started by: ace_friends22
1 Replies

4. Shell Programming and Scripting

passing a variables value from the called script to calling script using ksh

How do i get the value of the variable from the called script(script2) to the calling script(script1) in ksh ? I've given portion of the script here to explain the problem. Portion of Script 1 ============= ----- ----- tmp=`a.ksh p1 p2 p3` if then # error processing fi -----... (10 Replies)
Discussion started by: rajarkumar
10 Replies

5. Shell Programming and Scripting

Passing a file handler and an array from Perl to Shell Script

Hi there, I am trying to call a shell script from a Perl script. here is the code: @args = ("sh", "someshellprg.sh", "a file handler", "an array"); system(@args) == 0 or die "system @args failed: $?"; in the shell program, I examine if the arguments exits using: if then echo... (5 Replies)
Discussion started by: pinkgladiator
5 Replies

6. Shell Programming and Scripting

Passing arrays between functions

Hi, I have a function that hold 3 arrayies. I need to pass them to another function as an input, for further use Could you please explain how to do that. Thanks (5 Replies)
Discussion started by: yoavbe
5 Replies

7. Shell Programming and Scripting

[BASH - KSH] Passing array to a function

Passing a array to a function, a basic feature in modern language, seems to be only possible in KSH. Not in BASH. Depite all my efforts I couldn't come to a solution. See the following examples: It works perfectly in KSH: #!/usr/bin/ksh function print_array { # assign array by indirect... (3 Replies)
Discussion started by: ripat
3 Replies

8. Shell Programming and Scripting

Help in passing array of inputs to C program using script?

Hi, I have a file input which has 1000 data inputs of array elements. I would like to pass this to a C program one line at a time as input automatically. Anyone know how I could use "sed" to perform this? Appreciate alot. Thanks. (1 Reply)
Discussion started by: ahjiefreak
1 Replies

9. Shell Programming and Scripting

passing result of query to a varibale in ksh script

Hi, I have a scenario where in i have to extarct max of one column and pass it to a variable. i have tried to export the result as .dat file and read from that file.But my database is mainframe and it is not permitting me to export in .dat file.I have tried using .ixf file but reading from... (2 Replies)
Discussion started by: ammu
2 Replies

10. Shell Programming and Scripting

passing variables to awk from ksh script

I'm trying to write a ksh script that uses awk, but I want to pass variables to awk. For example (not working): if ];then searchstr=$1 lsof -i | awk '{if($9~/SEARCHSTR/) print $2} SEARCHSTR=$searchstr' else echo "usage: $0 <search string>" fi I tried several options. Is it... (3 Replies)
Discussion started by: rein
3 Replies
Login or Register to Ask a Question