Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
how to run shell command like "<<EOT .... EOT"
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Kushal Kumaran  
View profile  
 More options Sep 28 2012, 4:23 am
Newsgroups: comp.lang.python
From: Kushal Kumaran <kushal.kumaran+pyt...@gmail.com>
Date: Fri, 28 Sep 2012 13:46:10 +0530
Local: Fri, Sep 28 2012 4:16 am
Subject: Re: how to run shell command like "<<EOT .... EOT"

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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
叶佑群  
View profile  
 More options Sep 28 2012, 8:48 pm
Newsgroups: comp.lang.python
From: 叶佑群 <ye.you...@eisoo.com>
Date: Sat, 29 Sep 2012 08:48:03 +0800
Local: Fri, Sep 28 2012 8:48 pm
Subject: Re: how to run shell command like "<<EOT .... EOT"
于 2012-9-28 16:16, Kushal Kumaran 写道:

I tried this, but it is still not work.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
叶佑群  
View profile  
 More options Sep 28 2012, 8:49 pm
Newsgroups: comp.lang.python
From: 叶佑群 <ye.you...@eisoo.com>
Date: Sat, 29 Sep 2012 08:34:04 +0800
Local: Fri, Sep 28 2012 8:34 pm
Subject: Re: how to run shell command like "<<EOT .... EOT"
于 2012-9-28 16:16, Kushal Kumaran 写道:

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 must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kushal Kumaran  
View profile  
 More options Sep 29 2012, 7:54 am
Newsgroups: comp.lang.python
From: Kushal Kumaran <kushal.kumaran+pyt...@gmail.com>
Date: Sat, 29 Sep 2012 17:23:31 +0530
Local: Sat, Sep 29 2012 7:53 am
Subject: Re: how to run shell command like "<<EOT .... EOT"

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, .stdout.read or
.stderr.read 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.

--
regards,
kushal


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
叶佑群  
View profile  
 More options Oct 7 2012, 9:09 pm
Newsgroups: comp.lang.python
From: 叶佑群 <ye.you...@eisoo.com>
Date: Mon, 08 Oct 2012 08:53:46 +0800
Local: Sun, Oct 7 2012 8:53 pm
Subject: Re: how to run shell command like "<<EOT .... EOT"
于 2012-9-29 19:53, Kushal Kumaran 写道:

Sorry for replying so late, these days are long vocation in china.

If I type command in shell line by line, the command will run as
expected, but when I invoke the command in python, it is always failed.
Which is what I mean "not work".

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

No exception occured.

> - 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.

I solved this problem as below:

fop = os.popen ("sfdisk -uM %s <<EOT\n,%d,83\n,,83\nEOT\n" % (target, fps))

But when I run it with subprocess.Popen (), it doesn't work as os.popen
even use the code as:

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)

stderr.readline () always return the error message means "sfdisk won't accept more than one device parameter except -s or -l is specified", it seems that some parameters were incorrectly treated


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »