Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > befuddled by os.exec functions

Reply
Thread Tools

befuddled by os.exec functions

 
 
Avi Kak
Guest
Posts: n/a
 
      07-23-2004

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
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
 
 
 
Donn Cave
Guest
Posts: n/a
 
      07-23-2004
Quoth Avi Kak <(E-Mail Removed)>:

| 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 Removed)
 
Reply With Quote
 
 
 
 
Avi Kak
Guest
Posts: n/a
 
      07-23-2004



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



On Fri, 23 Jul 2004 04:58:46 -0000, "Donn Cave" <(E-Mail Removed)>
wrote:

>Quoth Avi Kak <(E-Mail Removed)>:
>
>| 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 Removed)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Befuddled jlab13 Wireless Networking 4 06-19-2007 08:10 PM
Shared functions vs Non-Shared Functions tshad ASP .Net 11 05-27-2005 05:53 PM
still befuddled by port forwarding Steve Richter Cisco 8 05-13-2005 07:47 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
Exportable class functions as stand alone functions to .DLL or .SO Timothy Wong C++ 3 05-20-2004 01:44 PM



Advertisments