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
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;
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
}