Script – Function Call


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script – Function Call
# 1  
Old 06-29-2011
Script – Function Call

Hello,

I have Individual function in my shell script ,

Code:
Function1
{
            Master activities 
}
 
Function2
{
            Sub activities 1
}
 
Function3
{
            Sub activities 2
}
…

I wanted to maintain the function , since later I might be using passing some parameter …etc, which would be generic in the program
My requirement is , based on the success or failure of the master activity , I need to perform Sub activity 1 , then based on success / failure , I need to do Sub activity 2 …etc

Do I need to declare a return value for each function ?
Like

Code:
Function1
{
            Master activities 
            Return master_flag
}
 
Function2
{
            Sub activities 1
            Return sub1_flag 
}
…

So , in my program
Code:
Function1
If master_flag!=0 then 
Exit ( so that I don’t proceed further ) 
Function2
If Return sub1_flag !=0 then 
Exit ( so that I don’t proceed further )

OR, can I get the success failure from the default return value after each function call ?

Code:
Function1
If [ $? != 0 ] then         
            Exit ( so that I don’t proceed further ) 
Function2
If [ $? != 0 ] then         
            Exit ( so that I don’t proceed further ) 
Function3…..

Can someone please me out with this code , or If there is a better way of doing this ?

Thanks In Advance,

-

Last edited by pludi; 06-29-2011 at 12:36 PM.. Reason: Removed excessive formatting, added code tags
# 2  
Old 06-29-2011
how about something like...
Code:
func1 && func2 && func3

This chain automatically takes care of function return code and takes appropriate action...either call the next function or simply error out
# 3  
Old 06-29-2011
For situations where I want to print a particular message on error, I borrow an idea from perl:

Code:
function die
{
        echo "$*" >&2
        exit 1
}

function something
{
# success
        return 0
}

function otherthing
{
# success
        return 0
}

function sleed
{
# failure
        return 1
}


something || die "something didn't work"
otherthing || die "otherthing didn't work"
sleed || die "sleed didn't work"

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Call function as different user within the script

Hello For HP-UX, ksh shell, is it possible to define functions and call them by another user ? For example <function_name> ( ) { command1 command2 } su - <user> -c <function_name> Or the only option is defining the user in the function itself as follows - <function_name> ( )... (2 Replies)
Discussion started by: atanubanerji
2 Replies

2. Shell Programming and Scripting

Segmentation fault in function call, shell script

I am getting Segmentation fault at below function call in my script: get_x() { sqlplus -s / <<end | grep KEEP | sed 's/KEEP//;s///g' select 'KEEP' ,table_name from all_synonyms where upper(synonym_name)= '$1'; exit end x=$(get_x $1) echo " SQL OUTPUT IS :: $x" } I am getting output of... (1 Reply)
Discussion started by: IB_88
1 Replies

3. Shell Programming and Scripting

Call function from another script

Hey, i got this 2 file. When i try to pick option 1, which is test1, it says ./test: test1: not found. Any idea on how i can fix it? #!/bin/sh QUIT=0 `dirname $0`/testfile while ; do testmenu read option case $option in 1) test1 ;; 2) test2 ;; 3) echo... (2 Replies)
Discussion started by: Nick1097
2 Replies

4. Shell Programming and Scripting

Call shell script function from awk script

hi everyone i am trying to do this bash> cat abc.sh deepak() { echo Deepak } deepak bash>./abc.sh Deepak so it is giving me write simply i created a func and it worked now i modified it like this way bash> cat abc.sh (2 Replies)
Discussion started by: aishsimplesweet
2 Replies

5. Shell Programming and Scripting

remotely call function from local script

The following code doesn't work properly which means it doesn't displays remote output. #!/bin/ksh #################### Function macAddressFinder ######################## macAddressFinder() { `ifconfig -a > ipInterfaces` `cat ipInterfaces` }... (2 Replies)
Discussion started by: presul
2 Replies

6. Shell Programming and Scripting

Shell Script to call another function

Here is the following code : 1. # gcc -c test firstprog.c the above command will generate a executable file called "test " in which ever directory it is run. Assuming It will also return a value. 2. In the below SCRIPT . test is a file generated by compiling a c program... (3 Replies)
Discussion started by: Vabiosis
3 Replies

7. Shell Programming and Scripting

error when call function in bash script

Dear all, Could you please advice as I when call function i found the following error " refills: command not found" note that refills is function name. following also the function and how i call it function refills { echo "formatting refills and telepin" >> $log awk -F,... (20 Replies)
Discussion started by: ahmed.gad
20 Replies

8. Shell Programming and Scripting

how can i call a function in shell script

i have a function written in one shell script and i want to call that function in another shell script and use the value returned by that script. can any one suggest me how can i do that? regards, Rajesh.P (4 Replies)
Discussion started by: rajesh.P
4 Replies

9. Shell Programming and Scripting

i want to call a oracle function in my shell script

i want to call a oracle function in my shell script (4 Replies)
Discussion started by: dineshr85
4 Replies
Login or Register to Ask a Question