Python - how to execute shell commands with pipe?

I have a case to want to execute the following shell command in Python and get the output,

echo This_is_a_testing | grep -c test

I could use this python code to execute the above shell command in python,

>>> import subprocess >>> subprocess.check_output("echo This_is_a_testing | grep -c test", shell=True) '1\n'

However, as I do not want to use the "shell=True" option, I tried the following python code,

>>> import subprocess >>> p1 = subprocess.Popen(["echo", "This_is_a_testing"], stdout=subprocess.PIPE) >>> p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout) >>> p1.stdout.close() >>> p2.communicate() (None, None)

I wonder why the output is "None" as I have referred to the descriptions in the webpage :

Had I missed some points in my code ? Any suggestion / idea ? Thanks in advance.

-------------Problems Reply------------

From the manual:

to get anything other than None in the result tuple, you need to give stdout=PIPE and/or stderr=PIPE

p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout, stdout=subprocess.PIPE)

Please look here:

>>> import subprocess
>>> p1 = subprocess.Popen(["echo", "This_is_a_testing"], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout)
>>> 1
>>> p2.communicate()
(None, None)

here you get 1 as output after you write p2 = subprocess.Popen(["grep", "-c", "test"], stdin=p1.stdout), Do not ignore this output in the context of your question.

Is this what you want:

>>> p1 = subprocess.Popen(["echo", "This_is_a_testing"], stdout=subprocess.PIPE)
>>> p2 = subprocess.Popen(["grep", "test"], stdin=p1.stdout, stdout=subprocess.PIPE)
>>> p2.communicate()
('This_is_a_testing\n', None)

Category:python Views:0 Time:2012-02-22

Related post

  • executing shell command with | (pipe) using NSTask 2011-07-22

    I'm trying to execute this comamnd ps -ef | grep test using NSTask but I can't get the | grep test to be included in the NSTask: This is what I'm using currently to get the output of ps -ef into a string then I need to somehow get the pid of the proc

  • python: os.system does not execute shell command 2012-03-02

    I need to execute shell command in python program (I have ubuntu). More specifically I want to create graph using graphviz in python script. My code is os.system("dot -Tpng -o graph.png") It does not work, but if I just type "dot -Tpng grap

  • Execute shell command from within MySQL client? 2009-05-30

    In oracle database, command "host" can be used to run bash command from database command window. Is there a equivalent command as "host" in mySql? --------------Solutions------------- You can use the system command. system command, \! command Execute

  • Executing shell commands using PHP, e.g. shell_exec() etc., on a remote host? 2010-09-19

    Is it possible to execute shell commands on a remote computer (not localhost)? For instance things like $output = shell_exec("unzip"); You can assume that you have the login credentials of a user account on the remote machine, as well as

  • CTS_ERROR >>> Failed to execute shell command am instrument 2010-11-29

    Using the Android 2.2, API-8, SDK-r7 along with CTS-2.2_r4 suite. Updated the SDK_ROOT environment variable with SDK_r7 tools in “android-cts/tools/startcts” script and the “SDK_ROOT/tools” also included in PATH environment variable. Ran the “android

  • Executing shell command from ruby console returning Permission Denied Error? 2011-03-28

    Getting permission denied error while executing shell command from ruby console. And the same shell command is working from shell. From Shell.. [email protected]:~$ "`grep '^datadir=' /etc/mysql/my.cnf | cut -f 2 -d '='`/db_backups" bash: /db_b

  • How to execute shell command in C? 2011-05-12

    I am having problems executing the shell commands in C. I want to execute a shell command in C then capture the output of shell command and process the output further. I used following code to perform the functionality. But, the issue is when the she

  • How to execute shell commands in android? 2011-07-31

    I am trying to execute shell command through my code for adding entry in Iptables. The following is my piece of code private void createShellCommand(String uidValue_param, String interface_param) { // TODO Auto-generated method stub StringBuilder cmd

  • Execute shell command in c++ 2011-08-29

    I have a question regarding executing shell commands in c++. I'm building an application in winforms, vs 2008. My application has a button, when clicked should decode a binary file to a .csv file. I can decode files by first going to the right direct

  • php executing shell command gives random output in only in heavy use mode 2012-03-28

    Please help me with this issue that I am facing on production server. I launched an application that saves images in a directory only after checking that it does not already exist. To check if it already exists I used the following command find $file

  • How to store the result of an executed shell command in a variable in python? 2011-12-28

    I need to store the result of a shell command that I executed in a variable. But i couldn get that. I tried like: call=os.system("cat syscall_list.txt | grep f89e7000 | awk '{print $2}'") print call But it prints the result in terminal and prints the

  • python execute shell command and continue without waiting and check if running before executing 2012-09-26

    I need to execute two other python scripts from another one. commands look like this: # python # python This will happen in a loop that will sleep for 1 minute then re-run. Before executing the commands to start the other scripts I ne

  • Executing shell command from MySQL 2008-11-14

    I know what I'm looking for is probably a security hole, but since I managed to do it in Oracle and SQL Server, I'll give it a shot: I'm looking for a way to execute a shell command from a SQL script on MySQL. It is possible to create and use a new s

  • vim: execute shell command without filtering 2009-08-06

    I want to select a block of text (eg. V%) and use the text as input to a shell command (eg. wc or pbcopy) - but I DON'T want to alter the current buffer - I just want to see the output of the command (if any) the continue editting without any changes

  • Executing shell command and reading its output on a C program 2011-03-12

    I am trying to create a function which takes a shell command as an argument , uses fork to spawn a new process which executes the command. I also want to redirect the standard output of the command so the caller of the function can read it using a FI

  • Python - How to call bash commands with pipe? 2011-09-06

    I can run this normally on the command line in Linux: $ tar c my_dir | md5sum But when I try to call it with Python I get an error: >>> subprocess.Popen(['tar','-c','my_dir','|','md5sum'],shell=True) <subprocess.Popen object at 0x26c0550

  • Groovy executing shell commands 2008-10-01

    Groovy adds the execute method to String to make executing shells fairly easy; println "ls".execute().text but if an error happens, then there is no resulting output. Is there an easy way to get both the standard error and standard out? (other than c

  • PHP exec() will not execute shell command when executed via browser 2009-02-11

    I have a certain PHP script that calls exec() to execute a command to convert a PDF to JPG. This command works fine in bash. To preempt your initial troubleshooting guesses, note the following: safe_mode = Off Permission on the directory containing t

  • How to execute shell command in Javascript 2009-12-10

    I want to write a javascript function which will execute the system shell commands (ls for example) and return the value. How do I achieve this? --------------Solutions------------- What if the client you are executing this javascript is running Wind

Copyright (C), All Rights Reserved.

processed in 0.107 (s). 11 q(s)