Calling two function


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Calling two function
# 1  
Old 03-27-2013
call same function twice with different parameter

Hi,

I need to run start_load function for two tables.
Step 1: if HMAX_TBL_ID and GMAX_TBLI_D are same for tab_name1 then echo message "all table ids are processed"
Step 2: go back and call start_load for tab_name2 and check if table id are same for table 2 too.

Please let me know how to implement this logic.

Code:
get_table_id 
if [ ${HMAX_TBL_ID} = ${GMAX_TBLI_D} ]
 then   
   echo -e "All table ids are processed"
else
   echo "Do some processing"  
fi

##define start_load function##
start_load()
#some processing
get_table_id
#some more processing
############ Main section#################

if ${option} = 'ALL' then

start_load ${tab_name1} 
start_load ${tab_name2}

fi

thanks
sandy

Last edited by sandy162; 03-27-2013 at 06:30 PM.. Reason: update code
# 2  
Old 03-27-2013
I guess you need a loop and exit/return. Is start_Load() a function declaration? Define functions up top then comment the start of
############## MAIN ##############
like in PASCAL !

One good way to write loops is write what you want to do and when about to repeat, go back and wrap the repeating section so far in an unconditional loop: while : ; do ... done You can break, return or exit to get out! If you find yourself break-ing at the end or start, make it a loop condition in place of ':'. This applies to all languages !

One wonders who does error checking, and what to do about errors.

Is there supposed to be filtering if not 'ALL' ?
# 3  
Old 03-27-2013
thanks for reply, yes start_load() is a function definition, I am calling "get_table_id" function inside the "start_load" function.
There are error checks and other conditions , but thoe pieces are owkring so I did not put them here.

Problem : there are several conditions for this script, I having problem in test case when
"All table ids are processed"
Step 1: when I call "start_load tab_name1" , it should go to "get_table_id" function and print "All table ids are processed"
Step 2 : come back and run "start_lod tab_name2", it should go to "get_table_id" function and print "All table ids are processed"
Step 3: exit
# 4  
Old 03-27-2013
Lets step back from where for a moment and describe how it starts (first two loops unrolled) and how it ends (when can you tell it is done and is there anything else to do).
This User Gave Thanks to DGPickett For This Post:
# 5  
Old 03-27-2013
Thanks for reply
I pass table names to migrate_table function and in case when table_ids are matching
I just want to print the message "All tableid are processed" and run migrate_table function for second table and if table_id are matchin then exit and print "All table ids are processed"

Code:
get_tbl_id_list()
{
        TABLE_NAME=$1
        LOADER_ID=$2

        LAST_PROCESSED_TABLEID=`nzsql -q -A -t -c "select max(last_processed_tableid) from tab_pqr where tbl_name='${TABLE_NAME}' and status_flag='S'"

        if [ $? -ne 0 ]
        then
                echo -e "Error While getting the last processed tableid"
                exit 1;
        fi

        HMAX_TBL_ID=`psql -U ${PG_USER} -h ${PG_HOST} -d ${PG_DB} -p ${PG_PASS} -A -t -c "select stp_ctr_tbl_id('ARIB', 'GET')"`

        if [ $? -ne 0 ]
        then
                echo -e " Error while getting the maximun table id processed in Hadoop "
        fi

        if [ ${HMAX_TBL_ID} = ${LAST_PROCESSED_TABLEID} ]
         then   
          echo -e "All table ids are processed"
        else
        psql -U ${PG_USER} -h ${PG_HOST} -d ${PG_DB} -p ${PG_PASS} -t -c "select distinct tbl_id from tbl_ctr_hdr where tbl_id between ${LAST_PROCESSED_TABLEID}+1 and ${HMAX_TBL_ID}" | sed '/^$/d' > ${TBL_ID_FILE}

           if [ $? -ne 0 ]
            then
                 echo -e " Error while getting the list of Table ids"
           fi
        fi
}

migrate_tables
{
TAB_NAME=$1
loader_id=BD

 echo -e " loader ids that are processing ... 
 
 get_tbl_id_list ${TAB_NAME} ${loader_id}
 
 ##some more processing##
 }
 
#########################Calling function to load data in table#####################
tab_name=$1 
#some checks and other processing
if [ ${tab_name} = 'ALL' ] 
	then
	   echo -e " Loading tab_abc table have started"
	     migrate_tables 'tab_abc'
       
	   if [ $? -ne 0 ]
		 then
			echo -e "Error While processing 'tab_abc' using ALL option"
			exit 1;
		fi
	   
	    echo -e "Loading tab_xyz has started"
	       migrate_tables 'tab_xyz'
 
		if [ $? -ne 0 ]
		 then
			echo -e "Error While processing tab_xyz using ALL option "
			exit 1;

		fi
fi


Last edited by sandy162; 03-28-2013 at 11:23 AM.. Reason: edit code
# 6  
Old 03-28-2013
OK, first routine get_tbl_id_list:
  1. you use table to get last table_id,
  2. then constants to get hmax table_id (does other processing change this result, not constant for the whole run and should be outside the loops?)
  3. if = then say all done and return
  4. else make a table id file.
Not sure why it needs to be a sub, since it looks like you call it once. Not sure who calls it when. $LOADER_ID never used, or hidden parameter to something?

Second, migrate_tables
  1. echos a message
  2. calls routine #1 with $1 and constant (which will be ignored?)
  3. other stuff
Hardly worth having a function.

Third, main code:
  1. $1 has to be ALL to do any of the referenced code.
  2. echo a message
  3. call #2 with constant tab_abc (low table?)
  4. call #2 with constant tab_xyz (high table?) which will overwrite product of step 3 unless all are done. Maybe that is always true, which would be somewhat contrived.
Not sure what it does or lacks. Normally, you want to loop through a process from a known starting point until a predictable end point. Nothing here loops. I am not sure what is modeled or processed. Who modifies tab_pqr? Who reads the file? Does stp_ctr_tbl_id('ARIB', 'GET') output change as tables are modified in this process? We only call it twice. Maybe we should take the table ids out of that file and migrate them?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help to Modify File Name in each function before calling another function.

I have a script which does gunzip, zip and untar. Input to the script is file name and file directory (where file is located) I am reading the input parameters as follows: FILENAME=$1 FILEDIR=$2 I have created 3 functions that are as follows: 1) gunzip file 2) unzip file... (2 Replies)
Discussion started by: pinnacle
2 Replies

2. Shell Programming and Scripting

Function is calling only once

In my prog if i enter the input for the 1st time it is executing correctly, but for the second time entire script is not executing it just exiting my code is #!/bin/sh checkpo() { echo "Checking the entered PO to create output text file "; IFS=$'\n' set -f var=0 for i in $(cat... (3 Replies)
Discussion started by: Padmanabhan
3 Replies

3. UNIX for Dummies Questions & Answers

Getting a error while calling a function

Hi Friends, While calling a function in below scipt func_serv_logs () { find . -type f \( \( -name 'WLS*' -o -name 'access*' \) -a ! -name '*.gz' -a ! -newer ${REFERENCE} \) -print | while read FILENAME do echo "hi" done } func_serv_logs I am getting error... (4 Replies)
Discussion started by: Jcpratap
4 Replies

4. Shell Programming and Scripting

SHELL SCRIPT Function Calling Another Function Please Help...

This is my function which is creating three variables based on counter & writing these variable to database by calling another function writeRecord but only one record is getting wrote in DB.... Please advise ASAP...:confused: function InsertFtg { FTGSTR="" echo "Saurabh is GREAT $#" let... (2 Replies)
Discussion started by: omkar.sonawane
2 Replies

5. Shell Programming and Scripting

Calling Function in KSH

I have a script with 2 functions 1) show_menu 2) create Ths show_menu function works fine....... Sort of.... When I select option 2 of the menu the code does a few commands and then calls another function called create. It's at this point that I get "create: not found"..... However,... (2 Replies)
Discussion started by: hxman
2 Replies

6. UNIX for Dummies Questions & Answers

Calling a function through a variable

Hey folks, I'm pretty new to unix programming. I was trying to get something to work but it's not doing what I expected. #!/bin/ksh . ./functions.sh STRING=function_1 FUNCTION="$STRING" RETURN=eval $FUNCTION echo "value of $FUNCTION function is: $RETURN" All i'm... (5 Replies)
Discussion started by: Irrational
5 Replies

7. Shell Programming and Scripting

Return a value from called function to the calling function

I have two scripts. script1.sh looks -------------------------------- #!/bin/bash display() { echo "Welcome to Unix" } display ----------------------------- Script2.sh #!/bin/bash sh script1.sh //simply calling script1.sh ------------------------------ (1 Reply)
Discussion started by: mvictorvijayan
1 Replies

8. UNIX for Dummies Questions & Answers

Calling a function

I have created a file generic.func and it has lots of functions. One of the functions is this: Check_backup_size() { dsmc q b $BACKUP_DIR/"*.Z" | awk '{print $1}'|sed 's///g' > outputfile X=`awk '{sum += $1} END {print sum }' outputfile'` echo "$X" ls -ltr $BACKUP_DIR/"*.Z" | awk... (5 Replies)
Discussion started by: ashika
5 Replies

9. Shell Programming and Scripting

Calling other file function

Hi, I am pretty new to unix. Lets say i have a program(run_program) that will call another file function(functiona, in same directory): hence, inside that run_program. i will just call "functiona xx xx" to refer and use that function. this run ok until i run this program from another folder.... (3 Replies)
Discussion started by: maldini
3 Replies

10. Programming

c++ calling main() function

i just finished a project for a c++ class that i wrote at home on my computer, compiled with gcc. when i brought the code into school it would not compile, it would complain that cannot call main() function. at school we use ancient borland c++ from 1995. anyway my program has 20 different... (3 Replies)
Discussion started by: norsk hedensk
3 Replies
Login or Register to Ask a Question