Handling parameters in Shell Functions | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Handling parameters in Shell Functions

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-09-2010
bharath.gct bharath.gct is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 22 June 2010, 9:51 AM EDT
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
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!!


Code:
 
#! /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
Sponsored Links
    #2  
Old 03-09-2010
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 15 April 2014, 9:23 PM EDT
Location: NM
Posts: 10,056
Thanks: 250
Thanked 756 Times in 711 Posts
which shell? ksh or bash or bourne
Sponsored Links
    #3  
Old 03-09-2010
bharath.gct bharath.gct is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 22 June 2010, 9:51 AM EDT
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
ksh mostly
    #4  
Old 03-09-2010
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 16 April 2014, 6:02 AM EDT
Location: in der Schweiz
Posts: 8,322
Thanks: 312
Thanked 1,013 Times in 886 Posts
(and on AIX?)

You should look at using eval...

i.e.

Code:
$ 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

Sponsored Links
    #5  
Old 03-10-2010
bharath.gct bharath.gct is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 22 June 2010, 9:51 AM EDT
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
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!
Sponsored Links
    #6  
Old 03-10-2010
Scott's Avatar
Scott Scott is online now Forum Staff  
Administrator
 
Join Date: Jun 2009
Last Activity: 16 April 2014, 6:02 AM EDT
Location: in der Schweiz
Posts: 8,322
Thanks: 312
Thanked 1,013 Times in 886 Posts
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.


Code:
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


Last edited by Scott; 03-10-2010 at 02:59 AM.. Reason: ls
Sponsored Links
    #7  
Old 03-11-2010
bharath.gct bharath.gct is offline
Registered User
 
Join Date: Mar 2010
Last Activity: 22 June 2010, 9:51 AM EDT
Posts: 31
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks Scott. This works like charm! 1 question as part of my understanding. The line
Code:
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:

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:

Code:
 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!


Code:
  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

Last edited by bharath.gct; 03-11-2010 at 11:33 PM..
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Handling Parameters in Perl bharath.gct Shell Programming and Scripting 3 03-09-2010 12:13 AM
Get the List of functions with modified parameters Sivaswami Shell Programming and Scripting 1 01-12-2009 08:23 AM
Calling shell functions from another shell script jisha Shell Programming and Scripting 6 04-05-2008 04:29 PM
functions in c shell?? ballazrus Shell Programming and Scripting 1 02-16-2006 04:14 AM
passing command line parameters to functions - sh vino Shell Programming and Scripting 5 03-05-2005 04:51 PM



All times are GMT -4. The time now is 06:12 AM.