Let me know if there is a way to pass array to a funtion in ksh script.
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
isPresent 12 <array>
if [ $? -ne 0 ]
then
echo "12 not present":
else
echo "12 present";
fi
## 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
## 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
You could use a nameref (ksh93):
#!/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:
12 is present
In bash or ksh93:
array()
{
eval "array=( \"\${${1}[@]}\" )"
printf "%s\n" "${array[@]}"
}
q=( a b c d e f g )
array q