1

I'm trying to get output of

dmesg | grep -i 'bios' 

I've tried this:

dmsg = subprocess.check_output("dmesg | grep -i 'bios'").decode('utf-8') 

, but I'm getting an error.

How can I obtain a full sdtoutput message?

2
  • 1
    What error are you receiving? You typically must give the command and parameters as elements in a list, not as a single string. See the documentation Commented Aug 24, 2015 at 20:45
  • 1
    like this it will only work with shell=True Commented Aug 24, 2015 at 20:59

2 Answers 2

4
import subprocess p1 = subprocess.Popen(['dmesg'], stdout=subprocess.PIPE) p2 = subprocess.Popen(['grep', '-i', 'bios'], stdin=p1.stdout, stdout=subprocess.PIPE) p1.stdout.close() out, err = p2.communicate() print(out) 
Sign up to request clarification or add additional context in comments.

Comments

2

You can pipe using Popen passing the stdout of one one process to the stdin of another but you also parse the output of dmesg with python instead of running two processes:

import subprocess dmsg = subprocess.check_output("dmesg",universal_newlines=True).splitlines() print([line for line in dmsg if "bios" in line.lower()]) 

If you were trying to pipe using check_output you would need to use shell=True:

import subprocess dmsg = subprocess.check_output("dmesg | grep -i 'bios'",shell=True).decode('utf-8') 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.