Seite 1 von 1

'echo -e' + subprocess

Verfasst: Montag 21. März 2016, 11:18
von HeAdLeSs
Hallo zusammen.

Ich möchte ein Shell Kommando mit subprocess ausführen. Dabei möchte ich Zeilenumbrüche im Text haben. Das geht mit 'echo -e'. Nur leider funktioniert das nicht wenn ich es im subprocess mache. Was mache ich da falsch?
import subprocess

text = "Hallo\nhier ein Test\n\nText."
inputcommand = 'echo -e "%s" | gammu-smsd-inject TEXT %s' % (text, number) #speak aloud
p = subprocess.Popen(inputcommand, stdout=subprocess.PIPE, shell=True)
Vielen Dank.
HeAdLeSs

Re: 'echo -e' + subprocess

Verfasst: Montag 21. März 2016, 11:32
von Sirius3
@HeAdLeSs: wenn Du Daten einem Programm übergeben willst, machst Du das über Pipes:

Code: Alles auswählen

import subprocess

text = "Hallo\nhier ein Test\n\nText."
command = ['gammu-smsd-inject', 'TEXT', str(number)]
p = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
p.stdin.write(text)
p.stdin.close()

Re: 'echo -e' + subprocess

Verfasst: Montag 21. März 2016, 20:55
von HeAdLeSs
@Sirius3
Hab es gerade ausprobiert. Funktioniert 1a. Danke.
Habe es aber noch ein wenig abgeändert, da stdin.write wohl Probleme machen kann (http://stackoverflow.com/questions/1635 ... n-argument).

Code: Alles auswählen

    command = ['gammu-smsd-inject', 'TEXT', str(number)]
    p = subprocess.Popen(command, stdout=subprocess.PIPE, stdin=subprocess.PIPE)
    output = p.communicate(input=text)[0]