Reputation: 9794
I am trying to run the following command using subprocess module:
/usr/local/MATLAB/R2013b/bin/matlab -r "func_call('output', '/path/to/location/')"
Note that Matlab requires function calls to be in double quotes. The above command works find from bash command line. Here is what I have done in python
func = "func_call('output', '/path/to/loc/')"
cmd = "/usr/local/MATLAB/R2013b/bin/matlab "
options = "-r \"%s\"" % func
run_cmd = cmd + options
proc = subprocess.Popen(run_cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc.communicate()
I get following error:
('', '/usr/local/MATLAB/R2013b/bin/matlab: eval: line 1738: syntax error near unexpected token `)\'\n/usr/local/MATLAB/R2013b/bin/matlab: eval: line 1738: `exec "/usr/local/MATLAB/R2013b/bin/gla64/MATLAB" -r ""\'"func_call(\'"\'output\'," \'/path/to/loc/\')"\'\n/usr/local/MATLAB/R2013b/bin/matlab: line 1738: warning: syntax errors in . or eval will cause future versions of the shell to abort as Posix requires\n')
I am assuming that it is the quotes that are messing the execution. Am I right? How can I fix this?
Upvotes: 1
Views: 1080
Reputation: 25813
I suspect this is because of the split
you have, that splits on all spaces including the spaces between quotes. Try this:
func = "func_call('output', '/path/to/loc/')"
cmd = "/usr/local/MATLAB/R2013b/bin/matlab"
run_cmd = [cmd, "-r", func]
proc = subprocess.Popen(run_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc.communicate()
Notice that I didn't put another set of quotes around func. Those quotes in a shell, ie bash, tell the shell that everything between them is a single argument, those quotes get parsed by the shell and not actually part of the argument. In python every string, after the first which is the command, is a single argument so you don't need to do anything special.
Upvotes: 2