0

I have multiple octave scripts, that I need to execute in order. The 2nd script is dependent upon the first script, so it must wait for the first script to complete. I'd also like to pass in 2 arguments from the command line. The following script, though, does not wait for the first script before executing the second. How can I correct this?

EXP_ID = $1;    
NUM_FEATURES = $2;

cd fisher;
octave computeFisherScore-AG.m $EXP_ID;
cd ..;
octave predictability-AG.m $EXP_ID $NUM_FEATURES;
4
  • Do you run those scripts separately? Commented Jul 23, 2014 at 17:48
  • 1
    why are you writing a bash script that calls Octave scripts in sequence? Why not just a single Octave script that runs both? Also, why don't you actually make an Octave script by using #! bin/octave as shebang line, and giving it execute permissions? Commented Jul 23, 2014 at 18:43
  • I agree with @carandraug, there is no need whatsoever to run two Octave scripts separately or rely on Shell scripting. Refactor your code or create another Octave script that calls the other two in sequence. Commented Jul 24, 2014 at 18:13
  • I understand what you're saying. In this case, for reasons outside of the purview of this question, they need to be separate scripts, and are often run independently. Commented Jul 24, 2014 at 20:03

2 Answers 2

1

Try:

EXP_ID = $1;    
NUM_FEATURES = $2;

cd fisher;
octave computeFisherScore-AG.m $EXP_ID;
wait
cd ..;
octave predictability-AG.m $EXP_ID $NUM_FEATURES;
wait

Check out http://www.lehman.cuny.edu/cgi-bin/man-cgi?wait+3

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I need to play around with this to figure out if that solves it!
1

Perhaps your octave script runs in a background. You can use this workaround:

waitpid() {
    while kill -s 0 "$1" >/dev/null 2>&1; do
        sleep 1
    done
}

cd fisher;
octave computeFisherScore-AG.m $EXP_ID;
waitpid "$!"
cd ..;
octave predictability-AG.m $EXP_ID $NUM_FEATURES;

May I also suggest that you quote your arguments properly to prevent unexpected word splitting and pathname expansion:

cd fisher
octave computeFisherScore-AG.m "$EXP_ID"
waitpid "$!"
cd ..
octave predictability-AG.m $EXP_ID "$NUM_FEATURES"

Semicolons may also not be necessary.

3 Comments

Thank you! I need to play around with this, as well, to figure out if that solves it! The string splitting was definitely also an issue
Thank you for the quote suggestion. That was definitely a piece of it.
@Adam_G Ok. I'm still curious why your original script would not work for octave. It can't run itself on the background by itself. But wait works with it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.