Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: how to run shell command like "<<EOT .... EOT"

Thread Tools

Re: how to run shell command like "<<EOT .... EOT"

Kushal Kumaran
Posts: n/a
On Sat, Sep 29, 2012 at 6:18 AM, 叶佑群 <(E-Mail Removed)> wrote:
> 于 2012-9-28 16:16, Kushal Kumaran 写道:
>> On Fri, Sep 28, 2012 at 1:15 PM, 叶佑群<(E-Mail Removed)> wrote:
>>> Hi, all,
>>> I have the shell command like this:
>>> sfdisk -uM /dev/sdb<< EOT
>>> ,1000,83
>>> ,,83
>>> EOT
>>> I have tried subprocess.Popen, pexpect.spawn and os.popen, but none
>>> of
>>> these works, but when I type this shell command in shell, it is works
>>> fine.
>>> I wonder how to emulate this type of behavior in python , and if someone
>>> can
>>> figure out the reason why?
>>> The sample code of subprocess.Popen is:
>>> command = ["sfdisk", "-uM", target, "<<EOT", "\r\n",
>>> ",", 1000, ",", "83", "\r\n",
>>> ",", ",", "83", "\r\n", "EOT", "\r\n"]
>>> pobj = subprocess.Popen (command, bufsize=1, \
>>> stderr=subprocess.PIPE, stdout=subprocess.PIPE)
>>> res = pobj.stderr.readline ()
>>> if res is not None and pobj.returncode != 0:
>>> observer.ShowProgress (u"对设备 %s 分区失败!" % target)
>>> return False

>> The "<<EOT" syntax (called a here-document) just provides input to the
>> command. If you use the communicate method, you can provide input as
>> an argument:
>> command = ["sfdisk", "-uM", target ]
>> instructions = """
>> ,1000,83
>> ,,83
>> """
>> pobj = subprocess.Popen(command, stdin=subprocess.PIPE,
>> stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>> (output, errors) = pobj.communicate(instructions)

> I tried this, but it is still not work.

What do you mean by "not work"?

- If you get an exception, copy the entire traceback into an email

- If you do not get an exception, print out the value of the "errors"
variable to see why the command failed. You can also check
pobj.returncode for the exit status of the subprocess.

A possibility is that you have to replace "sfdisk" with the full path
to the binary, if it cannot be located on the PATH. So you will
replace it with "/usr/sbin/sfdisk", or "/sbin/sfdisk", or wherever the
file actually is.

<from your other email>

> If I want to read the output line by line and not put all output to memory buffer in one
> time, how to write the code?

You can read line by line by calling pobj.stdout.readline() and
pobj.stderr.readline(). You can send input to the process by calling
pobj.stdin.write(). If you manage this interaction "by hand", you
should not call communicate(). Also, you should be aware of the
problem mentioned in the subprocess documentation:

"Use communicate() rather than .stdin.write, or to avoid deadlocks due to any of the other OS pipe
buffers filling up and blocking the child process."

Is there any reason why you need to read line-by-line? You could use
communicate(), and then call stdout.splitlines() to get a list of
lines, if that's all you need.

Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Re: how to run shell command like "<<EOT .... EOT" Dennis Lee Bieber Python 0 10-08-2012 04:47 AM
how to run os.execv() to run command pslq dbname < gen.command Sonu Python 2 08-04-2007 11:25 PM
Run Unix shell command $ parse command line arguments in python Python 4 04-23-2005 04:42 AM
can I run unix shell command in the ModelSim shell? VHDL 2 02-18-2005 10:04 PM