Calling Bash Command From Python

Wildman best_lay at yahoo.com
Mon Oct 31 00:19:36 EDT 2016


Python 2.7.9 on Linux

Here is a bash command that I want to run from a python
program:  sudo grep "^user\:" /etc/shadow

If I enter the command directly into a terminal it works
perfectly.  If I run it from a python program it returns an
empty string.  Below is the code I am using.  Suggestions
appreciated.

cmdlist = ["sudo", "grep", '"^$USER\:"', "/etc/shadow"]
p = subprocess.Popen(cmdlist,
                     stdout=subprocess.PIPE,
                     stderr=subprocess.PIPE)
shadow, err = p.communicate()
print shadow

-- 
<Wildman> GNU/Linux user #557453



More information about the Python-list mailing list