473,666 Members | 2,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

calling ksh script from python

hi all,

I'm trying to call ksh script from python.
I have a file (toto.env) with a scirpt looking like:

-- begin ---
#!/bin/ksh
#
export TOTO_ENV=/home/toto
-- end ---

I call it from python like that:
-- begin ---
import commands
commands.getsta tusoutput('. toto.env')
-- end ---

but it always return an error saying:
sh: TOTO_ENV=/home/home: is not an identifier

doesn anyone know why ?
no matter what I use (popen,system.. .) it's always the same error...
if I directly try to do
commands.getsta tusoutput('expo rt TOTO_ENV=/home/toto')
it's the same error...
and what about this sh primpt in the error message, can't I use ksh
script ?

thanks for any help

ronan

Jul 19 '05 #1
11 5028
In article <11************ *********@g14g2 000cwa.googlegr oups.com>,
ro***********@y ahoo.com wrote:
....
I call it from python like that:
-- begin ---
import commands
commands.getsta tusoutput('. toto.env')
-- end ---

but it always return an error saying:
sh: TOTO_ENV=/home/home: is not an identifier

doesn anyone know why ?
....

and what about this sh primpt in the error message, can't I use ksh
script ?


Yes and no. You can expect system(), popen() et al. to
invoke the standard UNIX shell, "sh", so you must supply
an expression that will be correctly executed by that shell.
That expression can execute a program written in another
language - for example, 'ksh toto.env' or just './toto.env'
if you have made it executable and it starts with #!/bin/ksh.
That's really the best way to invoke a script.

Meanwhile, it might be worthwhile to reconsider the use
of ksh here, if you have any choice in the matter. Ksh
is fine for interactive use, but has some unfortunate
flaws as a programming shell, and due to proprietary issues
one commonly encounters an alternative implementation that's
even worse. On most modern platforms, sh will have a pretty
good programming feature set, and will be more reliable
(especially if it isn't just ksh by another name.)

Donn Cave, do**@u.washingt on.edu
Jul 19 '05 #2
In article <do************ ************@gn us01.u.washingt on.edu>,
Donn Cave <do**@u.washing ton.edu> wrote:
Jul 19 '05 #3
Cameron Laird wrote:
Infidel. While I sure feel that way about csh(1), it
surprises me you'd criticize ksh(1) so. 'Fact, 'mong
all the *sh-s, I *recommend* ksh for programming. May-
be the two of us see things differently.


I keep wondering how difficult it would be to make a Python shell that
exposes all of Python but also includes some builtin commands such as
cd, mkdir, etc., that are just names bound to os.chdir, os.mkdir..., and
is smart enough to take a given command from the user and try to do a
os.system() on it based on the path. IOW, I'd love to have all of Python
available as my unix shell, while still doing shell-type stuff such as
traversing directories, launching applications, etc.

There's likely a project that does this already that I'm just unaware of.

--
Paul McNett
http://paulmcnett.com

Jul 19 '05 #4
Paul McNett wrote:
Cameron Laird wrote:
Infidel. While I sure feel that way about csh(1), it
surprises me you'd criticize ksh(1) so. 'Fact, 'mong
all the *sh-s, I *recommend* ksh for programming. May-
be the two of us see things differently.


I keep wondering how difficult it would be to make a Python shell that
exposes all of Python but also includes some builtin commands such as
cd, mkdir, etc., that are just names bound to os.chdir, os.mkdir..., and
is smart enough to take a given command from the user and try to do a
os.system() on it based on the path. IOW, I'd love to have all of Python
available as my unix shell, while still doing shell-type stuff such as
traversing directories, launching applications, etc.

There's likely a project that does this already that I'm just unaware of.


yes, there is:

planck[~]> ipython -p pysh
Welcome to pysh, a set of extensions to IPython for shell usage.
help(pysh) -> help on the installed shell extensions and syntax.

Python 2.3.4 (#1, Feb 2 2005, 12:11:53)
Type "copyright" , "credits" or "license" for more information.

IPython 0.6.15 -- An enhanced Interactive Python.
? -> Introduction to IPython's features.
%magic -> Information about IPython's 'magic' % functions.
help -> Python's own help system.
object? -> Details about 'object'. ?object also works, ?? prints more.

IPython profile: pysh
fperez@planck[~]|1> cd test
fperez@planck[~/test]|2> ls
argv.py* cf.py div.py exit.py* lcomp.py sanner.py* times.sh*
ast.tcl* data error.py* foo.old pplot2.py* scopes.py* t.py
avg.py* die.py err.py foo.py pplot.py* splot.py vars.py
bar.py div.c err.pyc inspectbug.py pylabug.py strings.py visex.py
bar.pyc div.f exit2.py* inter.py ramptest.py* tgp.py xplot.py*
fperez@planck[~/test]|3> $$a=ls e*py
fperez@planck[~/test]|4> a
<4> ['error.py', 'err.py', 'exit2.py', 'exit.py']
fperez@planck[~/test]|5> for f in a:
|.> if len(f)>6:
|.> wc -l $f
|.>
64 error.py
2 exit2.py
10 exit.py
fperez@planck[~/test]|6> Exit
Cheers,

f

Jul 19 '05 #5
In article <ma************ *************** ***********@pyt hon.org>,
Paul McNett <p@ulmcnett.com > wrote:
Jul 19 '05 #6
In article <ob************ @lairds.us>, cl****@lairds.u s (Cameron Laird)
wrote:
In article <do************ ************@gn us01.u.washingt on.edu>,
Donn Cave <do**@u.washing ton.edu> wrote:
.
.
.
Meanwhile, it might be worthwhile to reconsider the use
of ksh here, if you have any choice in the matter. Ksh
is fine for interactive use, but has some unfortunate
flaws as a programming shell, and due to proprietary issues
one commonly encounters an alternative implementation that's
even worse. On most modern platforms, sh will have a pretty
good programming feature set, and will be more reliable
(especially if it isn't just ksh by another name.)

.
.
.
Infidel. While I sure feel that way about csh(1), it
surprises me you'd criticize ksh(1) so. 'Fact, 'mong
all the *sh-s, I *recommend* ksh for programming. May-
be the two of us see things differently.


----* Portability
"ksh" means
* -* ksh88 * *(AIX 5.2, ...)
* -* ksh93 * *(MacOS X 10.4, ...)
* -* pdksh * *(Redhat Linux, NetBSD 2.0, ...)
* -* (nothing) (Older BSDs, ...)
* Plus, I have seen differences that evidently come from
* build options that can be exercised by the platform vendor.
* For example, the effect of ksh -p varies, and "echo" seems
* to mimic the behavior of sh on its host platform.

----* Reliability
Interactive ksh users depend on ENV=$HOME/.kshrc for
per-shell transient customization, like aliases, but unlike
bash or even csh, this file isn't loaded for an interactive
shell startup, but any shell startup.* So if I write a program
in ksh, and you run it, my script is exposed to your aliases.

----* Feature over-saturation
The Bourne shell is pretty fair for UNIX programming, all
things considered, but it's a miserable programming language
in a more general context, I mean you can hardly do worse
(except, as you mention, csh.)* The Korn shell does nothing
to really improve the language, yet it adds myriad features
as if catering to some kind of full time ksh programmer.* And
that's ksh88, I understand 93 takes it to a whole new level.

Modern shells - ash, bash, etc. - implement*a*POS IX/XPG4
shell specification that has plenty of these*features. * That's
the obvious target for shell scripts written today. Programs
that can't get by with that should probably be written in some
other language - Python comes to mind, for example, though I
usually try awk first for simplicity of deployment.

** *Donn Cave,*do**@u.wa shington.edu
Jul 19 '05 #7
thanks for your input...
well I just find out that modifying environment through ksh call is not
possible (can't get the new evironment back to python). I think the
best thing to do is to translate all my ksh to pure python... I thought
that I could re-use some stufff, but I guest I'm going to translate
everything...

Jul 19 '05 #8
* Cameron Laird (2005-06-02 18:08 +0100)
In article <do************ ************@gn us01.u.washingt on.edu>,
Donn Cave <do**@u.washing ton.edu> wrote:
Meanwhile, it might be worthwhile to reconsider the use
of ksh here, if you have any choice in the matter. Ksh
is fine for interactive use, but has some unfortunate
flaws as a programming shell, and due to proprietary issues
one commonly encounters an alternative implementation that's
even worse. On most modern platforms, sh will have a pretty
good programming feature set, and will be more reliable
(especially if it isn't just ksh by another name.)

.
Infidel. While I sure feel that way about csh(1), it
surprises me you'd criticize ksh(1) so. 'Fact, 'mong
all the *sh-s, I *recommend* ksh for programming. May-
be the two of us see things differently.


http://groups-beta.google.com/group/...578e8d95137a3c
Jul 19 '05 #9
In article <8l************ *************** **@40tude.net>,
Thorsten Kampe <th******@thors tenkampe.de> wrote:
* Cameron Laird (2005-06-02 18:08 +0100)
In article <do************ ************@gn us01.u.washingt on.edu>,
Donn Cave <do**@u.washing ton.edu> wrote:
Meanwhile, it might be worthwhile to reconsider the use
of ksh here, if you have any choice in the matter. Ksh
is fine for interactive use, but has some unfortunate
flaws as a programming shell, and due to proprietary issues
one commonly encounters an alternative implementation that's
even worse. On most modern platforms, sh will have a pretty
good programming feature set, and will be more reliable
(especially if it isn't just ksh by another name.)

.
Infidel. While I sure feel that way about csh(1), it
surprises me you'd criticize ksh(1) so. 'Fact, 'mong
all the *sh-s, I *recommend* ksh for programming. May-
be the two of us see things differently.


http://groups-beta.google.com/group/...578e8d95137a3c


Well, that certainly must just about say it all.

It's nice to see that the author knows rc and es, which
are indeed a couple of very much better designed shells.

I am not sure I agree with him so much on shell programming
in general, but it depends on what his point really may be.
For sure, it's good to be aware of those things, at any rate.

Donn Cave, do**@u.washingt on.edu
Jul 19 '05 #10

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

Similar topics

1
2216
by: Raja | last post by:
I have a java class thats trying to call something in python. This works fine when i do a jythonc on the python script. When i call another python script from the __init__ of my called python script, things go bad.i get this message AttributeError: class 'configuration' has no attribute 'configuration' when calling the constructor. This is my python script thats called by the java class testconfig.py =============
1
1874
by: nik | last post by:
hi, my C++ app has embedded the interpreter and calls a python script everytime it creates a certain data structure. This allows the user to put the structure into a database or files or whatever. For example; // Py_Initialize(); has been called earlier m_module = PyImport_AddModule("myModule"); // for example, this string is my data structure
22
6306
by: Brad Tilley | last post by:
Is it possible to write a file open, then read program in C and then call the C program from a Python script like this: for root, files, dirs in os.walk(path) for f in files: try: EXECUTE_C_PROGRAM If possible, how much faster would this be over a pure Python solution?
5
2150
by: Pekka Niiranen | last post by:
Hi there, I have two scripts. The first "main.py" sets some variables and then imports another called "gen.py". The idea is to provide "main.py" that defines some paths, variables etc. without using Windows environment variables. Various other "hackers" will make additional Python scripts (subroutines) like "gen.py" that utilize variables set by the "main.py" and which "main.py" calls. I can do this with "subprocess" module by setting...
7
12999
by: Pankaj | last post by:
The module which i am creating is like Part A: 1. It does some processing by using python code. 2. The result of this python code execution is written to a text file. ] Part B: 1. I read a text file which is outputted by above python script in a C++ program
5
2566
by: Shuaib | last post by:
Hi! I have a python script which returns an Integer value. How do I call this script from a C programe, and use the result returned? Thanks for your time.
2
7388
by: M Abbas | last post by:
Hello Folks, This is what i am required to do. Call an executable from my python script, and when the executable is fininshed running, i should continue with my python script. I have tried "os.exec()" but it calls the executable and never returns to the calling python script. I tried "os.fork" it will start an independent process, since logic of my program depends on the results of executable.
3
2072
by: Anthony Smith | last post by:
In my php page I am calling a Python cgi. The problem is that originally the Python script was being called directly and it could access the environment variables that were being set. Now since the php script is being called first it has access to the environment variables, but the Python script does nt. How can I forward the variables or call the Python script in such a way where it can access those variables? These are the type of...
4
6186
by: Quill_Patricia | last post by:
I have a Python script which is used to load data into a database. Up to now this script has been run by customers from the Windows command prompt using "python edg_loader.pyc". Any error messages generated are written to a log file. A project team working in the same company as me here would like to use this loading utility. They write UI applications for Windows using Java. They were able to launch the Python script from within Java by...
9
15525
by: Catherine Moroney | last post by:
I have one script (Match1) that calls a Fortran executable as a sub-process, and I want to write another script (Match4) that spawns off several instances of Match1 in parallel and then waits until they all finish running. The only way I can think of doing this is to call it as a sub-process, rather than directly. I'm able to get Match1 working correctly in isolation, using the subprocess.Popen command, but calling an instance of Match1...
0
8443
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
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8781
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
8639
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...
1
6192
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
5663
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
4366
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2769
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.