473,832 Members | 2,287 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2750
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.co m
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.c om>
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.co m


Jul 18 '05 #3

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

Similar topics

2
5297
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 time. The transcript so far is reproduced for your amusement below. To summarise: I've put up a Sudoku-solving program called Sudoku.exe. I want to call it in a php script to solve a puzzle and output the solution. It works fine with...
0
1287
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 have a database table containing command strings and python code. When a player types a command, the program looks up the table for the command and executes the corresponding python code. My plan was initally to build a library of useful...
45
2695
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
2709
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, but first I'd like to give some background. Python uses dictionary objects as symbol tables in it's execution contexts. Since these objects used to be basic types, which were not subclassable, some of the interpreter code accessed them using...
4
2377
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 which I mean here. Just for a moment, let's just take one definition for one of the
2
3114
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
3637
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 it gave a syntax error ("Invalid syntax") with caret pointing to the 'd' of the def keyword.
31
2473
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 expression, so I'm using exec. At the moment I came up with the following to make it work: def build_func(args): code """def foo(...)..."""
11
2587
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 modules, but for some reason it only works using the "import <mod>" syntax, and not "from <modimport *" syntax... i.e. in the latter case, the function is dynamically generated, but not accessible from the importing module. Any ideas on what I can...
0
9794
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10780
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10497
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10212
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9319
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7753
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6951
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5623
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
3077
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.