1

I am trying to write an Octave script that I can run as an executable.

I am using octave version 3.6.0. I am running the following script downloaded form here:

#!/usr/local/bin/octave -qf

# An example Octave script 

len = input( "What size array do you wish to use for the evaluation: " );

clear a; 
tic(); 
for i=1:len
    a(i) = i; 
endfor 
time1 = toc();

a = [1]; 
tic(); 
for i=2:len 
    a = [a i]; 
endfor
time2 = toc();

a=zeros( len, 1 ); 
tic(); 
for i=1:len 
    a(i) = i; 
endfor
time3 = toc();

printf( "The time taken for method 1 was %.4f seconds\n", time1 );
printf( "The time taken for method 2 was %.4f seconds\n", time2 );
printf( "The time taken for method 3 was %.4f seconds\n", time3 );

However when I run the script on the command line, I get the following error:

'usr/local/bin/octave: invalid option -- '

However, when I type the same command at the command line:

/usr/local/bin/octave -qf

I get the octave command prompt. What am I doing wrong?

Homunculus Reticulli
  • 65,167
  • 81
  • 216
  • 341

3 Answers3

5

I assume you're on some sort of Unix/Linux system. Is the file in "DOS" format, with DOS-style line endings? This could cause problems with how the command is interpreted.

BobS
  • 2,588
  • 1
  • 15
  • 15
  • Since everything else failed, and commands that work fine in another file failed when saved to the 'faulty' file, I suspected that the problem will be to do with the line endings as you suggested (I forgot that I had copied and pasted the data from the web). I used gedit to check and lo and behold, the offending file had Windows line endings :/ I fixed this, and the problem was solved. I will award the 50 points to you since I should have listened to you in the first instance! – Homunculus Reticulli Mar 17 '12 at 17:41
  • Stupid loose of time. Two systems, two different ways of ending lines... Thx! – nightcod3r Oct 26 '15 at 10:47
0

Your shebang line (which, btw, has a space it shouldn't) is calling /usr/local/bin/octave, but the error is coming from /usr/bin/octave. Is that a mistake? If so, you need to copy-and-paste code and errors for things like that. If not, the local version may be a script that calls the binary with an incorrect option when run non-interactively. In particular, it looks like the script (or something, at least) is trying to use a long option (--option), and the binary doesn't support it (so it's interpreting it as a short option).

Kevin
  • 53,822
  • 15
  • 101
  • 132
  • It should be /usr/local/bin. I have only one installation of Octave. I have fixed my code snipet/error message to reflect the bin location. Presence (or absence of spaces in the #! is a red herring AFAIK), however I have removed the space - the error still remains. – Homunculus Reticulli Mar 13 '12 at 14:37
  • @HomunculusReticulli Still, check the file to see whether it's a script. – Kevin Mar 13 '12 at 14:43
  • I checked the file type to make sure (as you suggested). It turns out octave is a symbolic link to octave-3.6.0 which is the executable (in the same folder). I updated my script file however when I run it, I got the following error: `'usr/local/bin/octave-3.6.0: invalid option -- '` – Homunculus Reticulli Mar 13 '12 at 16:01
0

Firstly the posted script runs fine on my system.

I type nano test.sh, I copy it to the file, I change the first line to be #!/usr/bin/octave -qf.

I press Ctrl-O and Ctrl-X.

I then make the script executable using chmod +x test.sh.

I then run the script using ./test.sh or octave -qf test.sh and it works as expected.

Notes: Octave on my system is

$ which octave
/usr/bin/octave 

$ file /usr/bin/octave
/usr/bin/octave: symbolic link to `octave-3.6.1'

$file /usr/bin/octave-3.6.1 
/usr/bin/octave-3.6.1: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically       linked (uses shared libs), for GNU/Linux 2.6.32,  BuildID[sha1]=0x061b0a703928fc22af5ca93ee78346a7f5a0e481, stripped

And on my system /usr/bin and /usr/local/bin are in $PATH

The only way I can generate the error you mention is by changing the first line of the script to #!/usr/bin/octave - - or #!/usr/bin/octave -q -f.

This gives

$ ./test.sh 
/usr/bin/octave: invalid option -- ' '

This means that in the script on your machine the shebang line is incorrect or is being interpreted incorrectly.

Verify that the first line is correct in the script.

Also identify what happens if the line is changed to #!/usr/local/bin/octave -q or to #!/usr/local/bin/octave -f.

For more information on the parsing of shebang line see :

Community
  • 1
  • 1
Appleman1234
  • 15,946
  • 45
  • 67