Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2004
    Posts
    16

    Unanswered: How to exit program correctly?

    I have a small problem here as illustrated below. When the main program (A) call the sub program (B), B program will perform some processing. Inside program B, there is a function to perform some task C. If the conditional check is failed in function C, I will exit with exit 1 and the program will resume with the next command after the function is called. My intention is to return immediately to program A. How can I achieve that?

    In program A
    {Main
    /home/user/program_B
    }


    In program B
    process_C
    {
    if (condition check)
    exit 1
    }

    Main
    #call process_C
    process_C

    some other processings
    <note that it will still continue with above command even process_C return with exit 1>

    How can it straight jump to program A once it fails in process_C? Appreciete you help...

  2. #2
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    If I understand what you are saying, exiting WILL pass control back to your calling shell (or program).

    Code:
    #-----  prog1  ------
    echo "Calling prog2 from prog1..."
    prog2
    echo "Back in prog1"
    
    #-----  prog2  ------
    
    function prog2Func
    {
     echo "In prog2Func"
     exit 1
    }
    
    echo "In prog2"
    prog2Func
    echo "You will not see this text!"
    If you wanted simply to exit the function but to remain in prog2, use return.

    Damian

  3. #3
    Join Date
    Feb 2004
    Posts
    16
    Damian,

    It doesn't really work. Exiting 1 in progr2Func will return to the prog2 and not prog1 which was intended. Is there other alternatives?

  4. #4
    Join Date
    Jun 2002
    Location
    UK
    Posts
    525
    Have you tried with the example code I posted? What output do you get?

  5. #5
    Join Date
    Feb 2004
    Posts
    16
    Ya, it is working. Thanks a lot..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •