Calling Bash Command From Python

Wildman best_lay at yahoo.com
Mon Oct 31 10:14:06 EDT 2016


On Mon, 31 Oct 2016 15:31:27 +1100, Chris Angelico wrote:

> On Mon, Oct 31, 2016 at 3:19 PM, Wildman via Python-list
> <python-list at python.org> wrote:
>> 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
> 
> Are you able to run that command without a password? sudo might be
> attempting to open the console, failing, and aborting the command. Is
> anything being printed to stderr?
> 
> ChrisA

No.  The program is run in a terminal and sudo asks for
a password.

-- 
<Wildman> GNU/Linux user #557453
The cow died so I don't need your bull!



More information about the Python-list mailing list