Handling parameters in Shell Functions

Hi,

Please help me with the below situation where I have to handle the parameters passed to a function in a unique way.

Below is the code, which I am trying to execute. I basically want to pass the parameter to a function, where I am trying to get user input into array(s). I want to name the array(s) based on the parameter. Please help me do it!!

 
#! /bin/sh
 
#GET_TABLE FUNCTION
get_table()
{
  i=0;
 
  set -A $1_file_array NULL;
  set -A $1_db_array NULL;
  set -A $1_table_array NULL;
 
  echo " Please enter tables that are affected in '<DBName>.<Tablename(s)>' format      :"
 
  while read file
  do
    if [ ${#file} -gt 0 ]; then
      $1_file_array[$i]=$file
      $1_db_array[$i]=${file%.*}
      $1_table_array[$i]=${file#*.}
      i=`expr $i + 1`
    fi
  done
  $1_count=${#$1_file_array[@]}
  return $1_count
}
 
# CALL FUNCTION GET_TABLE WITH 'SRC' AS PARM
get_table src
 
# DISPLAY THE LOADED ARRAY
j=0;
src_count=$?
while [ $j -lt src_count ]
do
# BELOW ARE THE ARRAY NAMES I WISH TO SEE
    echo "File: ${src_file_array[$j]}"
    echo "DB: ${src_db_array[$j]}"
    echo "Table: ${src_table_array[$j]}"
    j=`expr $j + 1`
done

Please let me know for any questions.

Thanks
Bharath

which shell? ksh or bash or bourne

ksh mostly

(and on AIX?)

You should look at using eval...

i.e.

$ cat script
set -A onearray_1 elem1 elem2 elem3
set -A anotherarray_1 another1 another2 another3

PrintMe() {
  eval echo \${${1}_1[2]}
}

PrintMe onearray
PrintMe anotherarray

$ ./script
elem3
another3

Thanks Scott!

But what if I want to have the Arrays defined and initialised within PrintMe() itself?

My requirement is to call the function with a prefix and the function will do the following:

  1. Declare, Define and Initialize 3 arrays with names
    <prefix>_file_array
    <prefix>_db_array
    <prefix>_table_array

  2. Fetch user input into the file array and fill the other 2 arrays with file array values.

  3. Define a variable namely <prefix>_count and calculate the count of array (all 3 arrays have the same count) into the variable.

  4. Return the <prefix>_count variable to the calling part.

Let me know for any questions.

thanks again in advance!!!

---------- Post updated 03-10-10 at 12:38 AM ---------- Previous update was 03-09-10 at 08:03 PM ----------

Can someone please help me with this? Needing this requirement be implemented soon. Appreciate any help on it. Thanks!

Here's how zou can dynamically create the arrays.

From some of your other code (like where does "file" come from) it's not so clear, but perhaps this will get you started.

CreateArray() {
  i=0
  ls | while read line; do
    if [ ... ]; then 
      eval ${1}_file_array[\${#${1}_file_array[@]}]="$line"
      eval ${1}_db_array[\${#${1}_db_array[@]}]="${line%.*}"
      eval ${1}_table_array[\${#${1}_table_array[@]}]="${line#*.}"
      i=$((i + 1))
    fi
  done
  eval ${1}_count=$i
  return $i
}

CreateArray src

j=0
while [ $j -lt ${#src_file_array[@]} ]; do
  ...
  j=$((j + 1))
done

Thanks Scott. This works like charm! 1 question as part of my understanding. The line

eval ${1}_file_array[\${#${1}_file_array[@]}]="$line"

will also the initialize the array?

Updating the post with the results. I just ran the code below and I am getting faulty values as the array is not initialized/set to NULL.

Code:

CreateArray()
{

  echo "\n Please enter tables that are affected in '<DBName>.<Tablename(s)>' format      :\n"

  i=0
  while read line; do
    if [ ${#line} -gt 0 ]; then
      eval ${1}_file_array[\${#${1}_file_array[@]}]="$line"
      eval ${1}_db_array[\${#${1}_db_array[@]}]="${line%.*}"
      eval ${1}_table_array[\${#${1}_table_array[@]}]="${line#*.}"
      i=$((i + 1))
    fi
  done
  eval ${1}_count=$i
  return $i
}

CreateArray src
j=0;
while [ $j -lt src_count ]
do
    echo " ${src_table_array[$j]}_unload.scrpt"
    j=`expr $j + 1`
done


CreateArray src

j=0;
while [ $j -lt src_count ]
do
    echo " ${src_table_array[$j]}_unload.scrpt"
    j=`expr $j + 1`
done

Output:

 Please enter tables that are affected in '<DBName>.<Tablename(s)>' format      :

DB1.TABLE1
DB2.TABLE2
DB3.TABLE3
DB4.TABLE4
DB5.TABLE5
 TABLE1_unload.scrpt
 TABLE2_unload.scrpt
 TABLE3_unload.scrpt
 TABLE4_unload.scrpt
 TABLE5_unload.scrpt

 Please enter tables that are affected in '<DBName>.<Tablename(s)>' format      :

DB3.TABLE3
DB4.TABLE4
 TABLE1_unload.scrpt
 TABLE2_unload.scrpt

I tried adding the below lines, to the first of the function, to initialize the array but that did not work too!

  set -A eval ${1}_file_array NULL;
  set -A eval ${1}_db_array NULL;
  set -A eval ${1}_table_array NULL;

Let me know if I am missing something

Hi.

You don't need to initialise an array in shell before you use it.

eval ${1}_file_array[\${#${1}_file_array[@]}]="$line"

This will set the element of the current count in the array to the value of $line

ie.

$ echo ${#src[@]}
0

$ src[${#src[@]}]=hello

$ echo ${#src[@]}
1

$ echo ${src[0]}
hello

$ src[${#src[@]}]=world

$ echo ${#src[@]}
2

$ echo ${src[1]}
world

There's no way out of your loop. I've added two lines below:

CreateArray()
{

  echo "\n Please enter tables that are affected in '<DBName>.<Tablename(s)>' format      :\n"

  i=0
  while read line; do
    if [ ${#line} -gt 0 ]; then
      eval ${1}_file_array[\${#${1}_file_array[@]}]="$line"
      eval ${1}_db_array[\${#${1}_db_array[@]}]="${line%.*}"
      eval ${1}_table_array[\${#${1}_table_array[@]}]="${line#*.}"
      i=$((i + 1))
    else       # added this
      break    # added this
    fi
  done
  eval ${1}_count=$i
  return $i
}

If I type in:

DB1.TABLE1
DB2.TABLE2
DB3.TABLE3
DB4.TABLE4
DB5.TABLE5

I get the output:

 TABLE1_unload.scrpt
 TABLE2_unload.scrpt
 TABLE3_unload.scrpt
 TABLE4_unload.scrpt
 TABLE5_unload.scrpt

This is what I'd expect to see. If it's wrong then please post exactly what you typed in, and exactly what output your expect.