0

I am trying to pass a string as System Argument variable to gnuplot from python. I have done this several times before, but surprisingly it does not work this time. I used this Topic How to pass command line argument to gnuplot?, but I did not work

import subprocess
ii=2
while ii<5:
    if (ii==2):
            name='rectangular'
            a="gnuplot -e 'name="+name+ "' graph3.gp"
    if (ii==3):
            name='trapezoidal'
    if (ii==4):
            name='simpson'

    a="gnuplot -e 'name="+str(simpson)+ "' graph3.gp"
    subprocess.call(a, shell='true')
    ii=ii+1

I always get the same error message:

line 0: undefined variable: rectangular

line 0: undefined variable: trapezoidal

line 0: undefined variable: simpson
Community
  • 1
  • 1
anonymous
  • 409
  • 1
  • 4
  • 14
  • Using shell=True can be a security [hazard](https://docs.python.org/2/library/subprocess.html#frequently-used-arguments) – Praveen Dec 16 '16 at 18:23
  • why? how can this cause the Problem that I have with my Code? Wht would you do instead? – anonymous Dec 16 '16 at 19:03

2 Answers2

0

Couple things:

  1. call takes a list of args
  2. shell takes a boolean (True not 'true')
  3. you may have a type in trying to cast "simpson" as a string instead of name?

maybe something like:

subprocess.call(a.split(), shell=True) # or
subprocess.call(["gnuplot", "-e", "'name={}".format(str(name)), "graph3.gp"], shell=True)
Kelvin
  • 1,357
  • 2
  • 11
  • 22
  • None of your props worked. Trying it with "Simpson" instead of name, gave me the same error. Your 2nd prop lead to this error: Traceback (most recent call last): File "Plots.py", line 11, in subprocess.call(["gnuplot", "-e", "'name={}'".format(str(name))], "graph3.gp", shell=True) File "/usr/lib64/python2.7/subprocess.py", line 522, in call return Popen(*popenargs, **kwargs).wait() File "/usr/lib64/python2.7/subprocess.py", line 658, in __init__ raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer – anonymous Dec 16 '16 at 18:58
  • I would print out the string you are passing, and then try to run it manually in the shell and see if it works. I was just giving you a few suggestions. – Kelvin Dec 16 '16 at 19:41
0

Ok, I figured out how it can be done. Everything has to be passed as a string:

import subprocess
ii=2
while ii<5:
    if (ii==2):
             name='name="rectangular"'
    if (ii==3):
            name='name="trapezoidal"'
    if (ii==4):
            name='name="simpson"'
    a="gnuplot -e {0} graph3.gp".format(name)
    subprocess.call(a.split(), shell=False)
    ii=ii+1

~

anonymous
  • 409
  • 1
  • 4
  • 14