473,385 Members | 2,274 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,385 software developers and data experts.

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.getstatusoutput('. 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.getstatusoutput('export 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 4971
In article <11*********************@g14g2000cwa.googlegroups. com>,
ro***********@yahoo.com wrote:
....
I call it from python like that:
-- begin ---
import commands
commands.getstatusoutput('. 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.washington.edu
Jul 19 '05 #2
In article <do************************@gnus01.u.washington.ed u>,
Donn Cave <do**@u.washington.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**************************************@python.o rg>,
Paul McNett <p@ulmcnett.com> wrote:
Jul 19 '05 #6
In article <ob************@lairds.us>, cl****@lairds.us (Cameron Laird)
wrote:
In article <do************************@gnus01.u.washington.ed u>,
Donn Cave <do**@u.washington.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*POSIX/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.washington.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************************@gnus01.u.washington.ed u>,
Donn Cave <do**@u.washington.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******@thorstenkampe.de> wrote:
* Cameron Laird (2005-06-02 18:08 +0100)
In article <do************************@gnus01.u.washington.ed u>,
Donn Cave <do**@u.washington.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.washington.edu
Jul 19 '05 #10
In article <11*********************@z14g2000cwz.googlegroups. com>,
ro***********@yahoo.com wrote:
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...


Right, that's true - more generally, no process can modify
another's environment.

Donn Cave, do**@u.washington.edu
Jul 19 '05 #11
ro***********@yahoo.com wrote:
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 thought about this a few days ago. Can't you copy it like this:

import os

env_rows = os.popen('. some.script > /dev/null; env).readlines()
for row in env_rows:
var, val = row.split('=',1)
os.environ[var]=val
Jul 19 '05 #12

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

Similar topics

1
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...
1
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...
22
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:...
5
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...
7
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...
5
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
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...
3
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...
4
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...
9
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
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...
0
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,...
0
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...

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.