Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #30368
| Return-Path | <kushal.kumaran@gmail.com> |
|---|---|
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.000 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'syntax': 0.03; '"""': 0.05; 'emulate': 0.07; 'method,': 0.07; 'python': 0.09; 'argument:': 0.09; 'command.': 0.09; 'received:mail- vb0-f46.google.com': 0.09; 'res': 0.09; 'sep': 0.09; 'subject:command': 0.09; 'target,': 0.09; 'cc:addr:python-list': 0.10; 'errors)': 0.16; 'fine.': 0.16; 'subject:run': 0.16; 'wrote:': 0.17; 'shell': 0.18; 'input': 0.18; 'received:209.85.212.46': 0.18; 'sender:addr:gmail.com': 0.18; 'skip:p 30': 0.20; 'all,': 0.21; 'either.': 0.22; 'cc:2**0': 0.23; 'this:': 0.23; 'cc:no real name:2**0': 0.24; 'command': 0.24; 'tried': 0.25; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'wonder': 0.27; 'message-id:@mail.gmail.com': 0.27; "doesn't": 0.28; 'received:209.85.212': 0.28; 'subject:like': 0.29; 'fri,': 0.30; 'figure': 0.30; 'code': 0.31; 'hi,': 0.33; 'received:google.com': 0.34; 'false': 0.35; 'pm,': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'child': 0.36; 'skip:p 20': 0.36; 'communicate': 0.37; 'received:209': 0.37; 'subject:: ': 0.38; 'skip:o 20': 0.38; 'header:Received:5': 0.40; 'provide': 0.62; 'behavior': 0.64; 'skip:\xe5 10': 0.65; 'subject:....': 0.65; '8bit%:94': 0.71; 'why?': 0.84 |
| DKIM-Signature | v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:from:date :x-google-sender-auth:message-id:subject:to:cc:content-type :content-transfer-encoding; bh=PWhusXKBjMbQxZtnXj08tYG/dEDkqhGrFdm9d4pA/ak=; b=ry+4kylVRcRdUJPEeSnprYhM6k6rmi5HdPqfd+s3Sqb0r0GtFxl+Qowp7TEzsRPb/v fPbtgV8IfjTUSbiD0tYu63SSR4tOlgpY68WNVwnMRfK1aw5OtrM1JGWi5FHoye6B0GMw WFHuAD9dlOiu9Id2Hj4+8Fa3o995/Gtag3ny9rVU4wmLC0G5cxRYMTr8CX2/QydVGkmu I14M4YtPa4davAax1zNgdAK2QQwrv/QOAMyuPPJGTuB1sBI42qLrVOaeUgV+Mv0dWLiN t8MGvGoPhj5c77s1LYYEbC//FGGykEddgqlUljwoQ0v5oErgb9zc8LzeJRkD3f5UDAiT Rnqg== |
| MIME-Version | 1.0 |
| Sender | kushal.kumaran@gmail.com |
| In-Reply-To | <506555B6.5040406@eisoo.com> |
| References | <506555B6.5040406@eisoo.com> |
| From | Kushal Kumaran <kushal.kumaran+python@gmail.com> |
| Date | Fri, 28 Sep 2012 13:46:10 +0530 |
| X-Google-Sender-Auth | MTQpVJvaRpkg8khJ3K8HG4j31JQ |
| Subject | Re: how to run shell command like "<<EOT .... EOT" |
| To | 叶佑群 <ye.youqun@eisoo.com> |
| Content-Type | text/plain; charset=UTF-8 |
| Content-Transfer-Encoding | quoted-printable |
| Cc | python-list@python.org |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1545.1348820601.27098.python-list@python.org> (permalink) |
| Lines | 67 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1348820601 news.xs4all.nl 6939 [2001:888:2000:d::a6]:54675 |
| X-Complaints-To | abuse@xs4all.nl |
| Path | csiph.com!usenet.pasdenom.info!news.franciliens.net!feed.ac-versailles.fr!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
| Xref | csiph.com comp.lang.python:30368 |
Show key headers only | View raw
On Fri, Sep 28, 2012 at 1:15 PM, 叶佑群 <ye.youqun@eisoo.com> 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)
> and pexpect code is:
>
> child = pexpect.spawn ("sfdisk -uM /dev/sdb <<EOT")
> child.sendline (....)
> child.sendline (....)
> child.sendline (....)
>
> and os.popen like this:
>
> os.popen ("sfdisk -uM /dev/sdb <<EOT\n,1000,83\n,,83\nEOT\n")
>
> I tried "\r\n", and it doesn't work either.
>
--
regards,
kushal
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: how to run shell command like "<<EOT .... EOT" Kushal Kumaran <kushal.kumaran+python@gmail.com> - 2012-09-28 13:46 +0530
csiph-web