473,325 Members | 2,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

befuddled by os.exec functions


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
ka*@purdue.edu

Jul 18 '05 #1
2 2725
Quoth Avi Kak <ka*@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, do**@drizzle.com
Jul 18 '05 #2

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" <do**@drizzle.com>
wrote:
Quoth Avi Kak <ka*@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, do**@drizzle.com


Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Greg Chapman | last post by:
I am at my wit's end trying to get information out of Streamline.net's support dept about my problem. They reply quickly enough, but seem to try and give out the least possible amount of info each...
0
by: John Allison | last post by:
Hi everybody I'm very new to python so please forgive any idiotic mistakes: I'm writing a MUD-like text game in python and I want to support dynamic ingame scripting to deal with user commands. I...
45
by: It's me | last post by:
I am new to the Python language. How do I do something like this: I know that a = 3 y = "a" print eval(y)
1
by: Crutcher | last post by:
I've been playing with dictionary subtypes for custom environments, and I encountered a strange interaction between exec, dictionary subtypes, and global variables. I've attached a test program,...
4
by: Michael | last post by:
Hi, I'm having difficulty finding any previous discussion on this -- I keep finding people either having problems calling os.exec(lepev), or with using python's exec statement. Neither of...
2
by: xml0x1a | last post by:
How do I use exec? Python 2.4.3 ---- from math import * G = 1 def d(): L = 1 exec "def f(x): return L + log(G) " in globals(), locals() f(1)
6
by: vasudevram | last post by:
Hi group, Question: Do eval() and exec not accept a function definition? (like 'def foo: pass) ? I wrote a function to generate other functions using something like eval("def foo: ....") but...
31
by: eliben | last post by:
Hello, In a Python program I'm writing I need to dynamically generate functions and store them in a dict. eval() can't work for me because a function definition is a statement and not an...
11
by: Nadeem | last post by:
Hello all, I'm trying to write a function that will dynamically generate other functions via exec. I then want to be able to import the file (module) containing this function and use it in other...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.