Call a shell command from Python

Ben Finney ben+python at benfinney.id.au
Mon Oct 31 21:08:52 EDT 2016


Wildman via Python-list <python-list at python.org> writes:

> On Mon, 31 Oct 2016 15:44:13 +1100, Ben Finney wrote:
>
> > One immediate difference I see is that you specify different
> > arguments to ‘grep’. You have a different pattern for each command.
> > 
> > * The ‘^user\:’ pattern matches “user\:” at the start of a line.
> > 
> > * The ‘^$USER\:’ pattern I think won't match anything, since “$” matches
> >   end-of-line and then you expect further characters *past* the end of
> >   the line. I think that will always fail to match any line.
>
> Yes, the '^' indicates the start of the line and the ':' indicates
> the character where to stop.  The colon has a special meaning so it
> has to be escaped, '\:'.  The dollar sign precedes a variable.  In
> this case it is an environment variable.

The ‘grep’ program you're invoking knows nothing of such variables, and
the ‘$’ sign means to ‘grep’ what I said above.

> > Maybe you are expecting Bash to be involved somehow (and so “$USER”
> > will be substituted by Bash with some other value). That's not what
> > happens.
>
> No, the shell is already running.

I don't know what you mean by this. If you mean that some *other*
instances of the shell ar running: that isn't relevant to how your
Python program invokes a subprocess.

The shell is not involved in the command as you invoke it directly as a
subprocess, without asking for a shell.

> And $USER will be substituted by the name of the user that invoked the
> shell.

It will not, because there is no shell involved: your Python program
invokes ‘sudo’, which invokes ‘grep’. The shell is never involved in
that chain, so its substitutions rules are irrelevant.

-- 
 \     “Try adding “as long as you don't breach the terms of service – |
  `\          according to our sole judgement” to the end of any cloud |
_o__)                      computing pitch.” —Simon Phipps, 2010-12-11 |
Ben Finney




More information about the Python-list mailing list