befuddled by os.exec functions

Discussion in 'Python' started by Avi Kak, Jul 23, 2004.

  1. Avi Kak

    Avi Kak Guest

    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
    Avi Kak, Jul 23, 2004
    #1
    1. Advertising

  2. Avi Kak

    Donn Cave Guest

    Quoth Avi Kak <>:

    | 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 Cave, Jul 23, 2004
    #2
    1. Advertising

  3. Avi Kak

    Avi Kak Guest

    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" <>
    wrote:

    >Quoth Avi Kak <>:
    >
    >| 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,
    Avi Kak, Jul 23, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hal Vaughan
    Replies:
    11
    Views:
    1,105
    Gordon Beaton
    May 22, 2006
  2. tedsuzman
    Replies:
    2
    Views:
    7,076
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  3. Ted
    Replies:
    1
    Views:
    460
    Duncan Booth
    Jul 22, 2004
  4. Guillermo Riojas
    Replies:
    0
    Views:
    169
    Guillermo Riojas
    Nov 26, 2010
  5. Random Task
    Replies:
    12
    Views:
    644
    Joe Smith
    Dec 4, 2005
Loading...

Share This Page