Handling parameters in Shell Functions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Handling parameters in Shell Functions
# 1  
Old 03-09-2010
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
# 2  
Old 03-09-2010
which shell? ksh or bash or bourne
# 3  
Old 03-09-2010
ksh mostly
# 4  
Old 03-09-2010
(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

# 5  
Old 03-10-2010
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!
# 6  
Old 03-10-2010
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 03:59 AM.. Reason: ls
# 7  
Old 03-12-2010
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-12-2010 at 12:33 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Python passing multiple parameters to functions

Hi, I am a beginner in python programming. In my python script have a main function which calls several other functions. The main function gets its input by reading lines from a input text file. I call the main function for every line in input text file through a loop. def main(line): var1... (6 Replies)
Discussion started by: ctrld
6 Replies

2. Shell Programming and Scripting

Pass parameters to a function and running functions in parallel

Hi , I have a script which is using a text file as I/P. I want a code where it reads n lines from this file and pass the parameters to a function and now this script should run in such a way where a function can be called in parallel with different parameters. Please find below my script, it... (1 Reply)
Discussion started by: Ravindra Swan
1 Replies

3. Shell Programming and Scripting

How to execute functions or initiate functions as command line parameters for below requirement?

I have 7 functions those need to be executed as command line inputs, I tried with below code it’s not executing function. If I run the ./script 2 then fun2 should execute , how to initiate that function I tried case and if else also, how to initiate function from command line if then... (8 Replies)
Discussion started by: saku
8 Replies

4. Shell Programming and Scripting

Handling Parameters in Perl

Hi All, I'm pretty new to the forum and also to UNIX. I have a requirement for which I need some help. I have a script (example.script) where I get user inputs using the read command. I would need to pass the read-fetched input to a perl command (explained below) in my script. The part which... (3 Replies)
Discussion started by: bharath.gct
3 Replies

5. Shell Programming and Scripting

Get the List of functions with modified parameters

Hi I have 2 files a.c and a.bak where I changed long to int using awk script. I want to get the list of functions whose parameters got modified for eg: fun ( long a, long b ) might be changed to fun ( int a, int b ) (1 Reply)
Discussion started by: Sivaswami
1 Replies

6. Shell Programming and Scripting

Calling shell functions from another shell script

Hi, I have a query .. i have 2 scripts say 1.sh and 2.sh 1.sh contains many functions written using shell scripts. 2.sh is a script which needs to call the functions definded in 1.sh function calls are with arguments. Can some one tell me how to call the functions from 2.sh? Thanks in... (6 Replies)
Discussion started by: jisha
6 Replies

7. Shell Programming and Scripting

Functions in C-Shell

Hi All, I have a very long code called myfunction -> "if ..... else if .... else if ..end if " And i have several other codes which need to call the "myfunction" code. How can C-shell call a function "B]myfunction" ? Can any body give me an example ?? (1 Reply)
Discussion started by: Raynon
1 Replies

8. Shell Programming and Scripting

functions in c shell??

i've been told that c shell does not support functions/subroutines is that true? if not can somebody give me the basic syntax for creating a function. it would very much appreciated! thanks in advance (1 Reply)
Discussion started by: ballazrus
1 Replies

9. Shell Programming and Scripting

Shell script functions

Simple shell script : date test_fn() { echo "function within test shell script " } on the shell prompt I run > . test Then I invoke the function on the command line as below : test_fn() It echos the line function within test shell script and works as expected. ... (5 Replies)
Discussion started by: r_subrahmanian
5 Replies

10. Shell Programming and Scripting

passing command line parameters to functions - sh

All, I have a sh script of the following tune: function a () { #functionality.. } function b () { #functionnlity.. } function check () { # this function checks for env and if all fine call build } function usage () { #sh usage details } function build () { #calls either a or b or... (5 Replies)
Discussion started by: vino
5 Replies
Login or Register to Ask a Question