befuddled by os.exec functions

A

Avi Kak

Hello:

I'd be most grateful if someone could answer
the following questions about the exec functions
in the os module.

1) How does one get one of the os.exec functions
in Python to execute a shell script that
includes some sort of a control structure in
the shell script itself?

For example, I can do the following in Perl

$ENV{ACK_MSG} = "You said: ";
exec('while a=a; do read MYINPUT; echo $ACK_MSG $MYINPUT; done');

How can one use one of the os.exec functions
in Python to do the same? All of the os.exec
functions require a pathname for the first
argument, followed by well-defined arguments.
But the above example does not break down
into pathname and argument components.

2) In the following example, I am mystified as
to why the first element of the list in the
second argument has to be ignored. If it is
going to be ignored anyway, why does it need
to be supplied at all? The following call
does the same regardless of what one has in the
first element of the second-arg list.

os.execvp( 'ls', ['ls', '-al'] )

Thanks.

Avi Kak
(e-mail address removed)
 
D

Donn Cave

Quoth Avi Kak <[email protected]>:

| 1) How does one get one of the os.exec functions
| in Python to execute a shell script that
| includes some sort of a control structure in
| the shell script itself?
|
| For example, I can do the following in Perl
|
| $ENV{ACK_MSG} = "You said: ";
| exec('while a=a; do read MYINPUT; echo $ACK_MSG $MYINPUT; done');
|
| How can one use one of the os.exec functions
| in Python to do the same? All of the os.exec
| functions require a pathname for the first
| argument, followed by well-defined arguments.
| But the above example does not break down
| into pathname and argument components.

As you probably know, the os (posix) module also provides a
system() function that does what you describe. While that's
actually implemented by calling a C library function, this
would be about the same:

def system(cmd):
pid = os.fork()
if pid:
...
else:
...
os.execve('/bin/sh', ['sh', '-c', cmd], os.environ)

That pathname and arguments are implicit in your example.
(Well, I don't know what your example actually does, since
I haven't used Perl for many years.)


| 2) In the following example, I am mystified as
| to why the first element of the list in the
| second argument has to be ignored. If it is
| going to be ignored anyway, why does it need
| to be supplied at all? The following call
| does the same regardless of what one has in the
| first element of the second-arg list.
|
| os.execvp( 'ls', ['ls', '-al'] )

It's up to the application - some applications look at this
value, sys.argv[0] in Python, others don't. "ls" may actually
use it for a "usage" message - try
os.execvp('ls', ['xx', '--yikes'])

and then there are various situations where argv[0] is used
in some more significant way. So it's useful to be able to
provide a value for argv[0] separately from the execution path.

Donn Cave, (e-mail address removed)
 
A

Avi Kak

Thanks very much, Donn, for posting your reply.
It was the syntax you used for the call to os.execve
that provided the solution I was looking for.

I was aware of os.system, but I wanted to use one of
os.exec functions because I do not want to create a
new child process.

Thanks again.

Avi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top