Passing arguments to octave function from within bash script

12,450

The single quotes are preventing the shell from substituting the variable:

octave --silent --eval "myOctaveFunc(\"$line\")"

If octave lets you use single quotes to quote strings, it will look a little cleaner (inside double quotes, single quotes have no special meaning):

octave --silent --eval "myOctaveFunc('$line')"

Also, from vim, make sure you save the file in unix format so each line does not end with a carriage return character: :set ff=unix

Share:
12,450

Related videos on Youtube

Sriram
Author by

Sriram

Interested in learning all that is out there!!

Updated on June 04, 2022

Comments

  • Sriram
    Sriram almost 2 years

    I wrote a function in Octave that takes in a line read from a file (one line at a time) as input argument. I use a bash script to read a line at a time from the file and then pass that as argument to the octave function from within the script.

    My bash script looks like so:

    #!/bin/bash  
    
    while read line
    do
      octave --silent --eval 'myOctaveFunc("${line}")'
    done < "inFileName"
    

    When I execute above script, octave throws errors like:

    error: called from:
    error:   /usr/share/octave/3.2.3/m/miscellaneous/fullfile.m at line 43, column 11
    error: evaluating argument list element number 2
    error: evaluating argument list element number 1
    error:   /usr/libexec/octave/packages/gsl-1.0.8/i386-redhat-linux-gnu-api-v37/PKG_ADD at line 47, column 1
    error: addpath: expecting all args to be character strings
    error: addpath: expecting all args to be character strings
    error: addpath: expecting all args to be character strings
    error: addpath: expecting all args to be character strings
    

    and so on..

    I have been able to run the octave script myOctaveFunc.m with input arguments such as helloWorld from the command line. The problem arises when I try to run it from within the bash script.

    My questions are:
    1. How do I get the octave function running from within the bash script?
    2. I am using gvim to edit the bash script. When I type in the line to invoke the octave script, I see that the ${line} is colored differently as compared to normal circumstances. Is that because of the '' used to invoke the octave function? If so, should I be worried about it?

    • Bruno De Fraine
      Bruno De Fraine over 12 years
      How do you invoke your octave script outside of the bash script? What do you see when you put "echo octave --silent ..." in the bash script instead of "octave --silent" ?
  • Sriram
    Sriram over 12 years
    Also, is the :set ff=unix option needed? I am working on a linux machine and do take care to do a dos2unix each time I come back from Windows.
  • glenn jackman
    glenn jackman over 12 years
    You hadn't told us anything about your environment, so no, not needed.
  • pwan
    pwan about 7 years
    It returns me warning: function 'dbEval' defined within script file '/home/max/octave-dollar/scripts/dbEval.m' when I run octave --silent --eval "dbEval(\"hello-world\")"