0

I am using Python and wants to run the "editUtility" as shown below.

echo "Some data" | /opt/editUtility --append="configuration" --user=userid 1483485

Where 1483485 is some random number and passed as parameter too.

What I am doing is calling the "editUtility" via Python "subprocess" and passing the param as shown below.

proc = subprocess.Popen(['/opt/editUtility', '--append=configuration'],stdout=subprocess.PIPE)
            lsOutput=""
            while True:
              line = proc.stdout.readline()
              lsOutput += line.decode()
              if not line:
                break
            print(lsOutput)

My question is: How to pass all of the params mentioned above and how to fit the 'echo "some data"' along with pipe sign with subprocess invocation?

funie200
  • 3,688
  • 5
  • 21
  • 34
Ammad
  • 4,031
  • 12
  • 39
  • 62
  • 1
    Does `editUtility` read all of stdin before it writes anything to stdout, or does it interleave reads and writes? – Charles Duffy Jan 22 '20 at 00:22
  • 1
    To be clear, in most cases the Right Thing is to use `stdin=subprocess.PIPE` and call `proc.communicate('Some data')`. Certainly the *easy* thing. – Charles Duffy Jan 22 '20 at 00:23
  • ...if you need to be able to read output incrementally, and don't have a guarantee that `editUtility` does all reads before any writes, then see the "How to use subprocess command with pipes" duplicate among those linked. – Charles Duffy Jan 22 '20 at 00:28

1 Answers1

1

so if you just want to input a string and then read the output of the process until the end Popen.communicate can be used:

cmd = [
    '/opt/editUtility',
    '--append=configuration',
    '--user=userid',
    '1483485'
]

proc = subprocess.Popen(
    cmd,
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

(stdoutData, stderrData) = proc.communicate('Some data')
IronMan
  • 1,854
  • 10
  • 7
  • Note that in general, Q&A entries that are duplicative of existing content should be closed, not answered; see the "Answer Well-Asked Questions" section of [How to Answer](https://stackoverflow.com/help/how-to-answer), particularly the bullet point regarding questions that "have already been asked and answered many times before". – Charles Duffy Jan 22 '20 at 00:24
  • Process is not picking up the value '1483485'. How can we pass it? – Ammad Jan 22 '20 at 00:59
  • 1
    @Ammad It would depend on the syntax of the arguments - it would make more sense for the user ID to be passed in via `--user=1483485`, but check the man page and try on the command line to confirm. – IronMan Jan 22 '20 at 01:04
  • This is the only param that is being passed with out --variablename=value. Thats why it is not picking up. – Ammad Jan 22 '20 at 01:06