4

I have written an octave script file (.m)

If anyone could point me out on how to run octave scripts on unix shell that would be really helpful. I do not want to execute the script by invoking octave program.

I am new to unix and octave.

Thanks in advance

6
  • 1
    How are you planning to run an octave script without running octave? Hint: you can't. But, you can call octave just to run your script: octave -q script.m should work with "quiet" mode, no extra garbage dumped by octave. Commented Feb 27, 2016 at 11:42
  • @AndrasDeak the same way you invoke scripts from any other language (maybe except Matlab and R). You don't need to actually specify octave on the command line if you do it properly. Commented Feb 27, 2016 at 12:11
  • Well, @carandraug, the shebang will only tell the shell to invoke octave for you. It's just less transparent, but it's still octave calling running the script. Commented Feb 27, 2016 at 12:18
  • @AndrasDeak considering the language and the question, I think this is exactly what the OP meant. Not having to specify it on the command line. Just like you don't have to specify on the command line bash, python, perl, or ruby when calling stuff from /usr/bin written on those languages. Commented Feb 27, 2016 at 13:04
  • @carandraug I guess you're right. To be fair, I always invoke my scripts explicitly:) Commented Feb 27, 2016 at 14:35

1 Answer 1

8

Yes, of course you can write an Octave program. Like so:

$ cat octave_program 
#!/usr/bin/env octave
## Never forget your licence at the top of the files.
1;

function [rv] = main (argv)
  disp ("hello world");
  rv = 0;
  return;
endfunction

main (argv);

$ chmod a+x octave_program # add executable permissions
$ ./octave_program 
hello world

There's a couple of things important for an Octave program:

  1. the first statement cannot be a function declaration. In all my programs, the first statements are loading of necessary packages. If you don't have packages, it is common to use 1;

  2. a she-bang line. That's the first line of your program which tells you how to run your program. If you know where Octave will be installed, you can use #!/usr/bin/octave but using #!/usr/bin/env octave will be more portable and flexible.

  3. your program needs executable permissions

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

Comments

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.