Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have a doubt about the execution pattern of a unix script.

For example a script containing lines:

command B
function C()
{
Myscript   // Myscript is a script 
command  D
}
command E

and suppose if Myscript takes a long time to execute (say 5 minutes) then is the next command (command D) going to execute or wait for Myscript to complete.

share|improve this question

2 Answers 2

up vote 16 down vote accepted

In the case of your particular script, neither Myscript nor command D will ever be executed. You've defined a function C, but you aren't calling it.

The order of execution is:

  1. command B runs to completion.
  2. command E runs to completion.

If you were to call C, it would run Myscript to completion, then run command D to completion.

Everything you invoke in a shell script runs in the order it's seen, and runs to completion. If you need to run something in parallel with something else, you should use job control (check out how the ampersand & character is used to put jobs in the background). A quick example:

A
B

Runs A until it's done, then runs B until it's done. The script finishes.

A &
B

Runs A in the background, then runs B in the foreground. The shell script ends when B has completed. A may still be running.

A &
B &

Runs A in the background, then (without waiting for A to complete), runs B in the background. The script ends immediately.

A &
B &
wait

Runs A in the background, then (without waiting for A to complete), runs B in the background. The script then waits for both A and B to complete before exiting.

share|improve this answer
    
it was a idiotic mistake not to call function sorry...+1 for the explanation.thanks –  munish Mar 11 '12 at 19:37

There is an exception which will be detailed later, but those commands are executed by the same shell (even if Myscript opens a new shell, it and command D will possess exactly the same "mother" shell that runs them). Because *NIX shells don't support parallelization, Myscript has to stop its run (with any exit codes) to let the control got by command D.

The exception is the case where you are detaching Myscript, with a NOHUP signal, or by writing a & behind Myscript. This will put the script to the background.

Study the following code snippet:

openssl enc -e bf -in verybigfile -out outputfile -k thisismykey &; # this is a long operation because of the size of the very big file
echo 'hi!' # this will be written during the encrypting operation

I hope this is almost self-explanatory now.


The answer of Alexios contains a great point: you don't call the function, so it will never run.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.