befuddled by os.exec functions

Avi Kak kak at purdue.edu
Fri Jul 23 10:12:09 EDT 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" <donn at drizzle.com>
wrote:

>Quoth Avi Kak <kak at purdue.edu>:
>
>|  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, donn at drizzle.com




More information about the Python-list mailing list