473,395 Members | 1,629 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,395 software developers and data experts.

bash like expansion

Consider the following:

import os, commands
os.environ['QWE']="string with foo"
a = '$QWE ${QWE/foo/baz}'
b = commands.getoutput('echo ' + a)
This does what I want, which is to expand
a according to the standard bash expansion rules
(so b now references "string with foo string with baz"),
but it doesn't feel right.

Is there a more pythonic way to interpret strings according
to shell rules for word expansion? Relying on commands
feels like a kludge, and shlex is way too much work for this
(not to mention that it doesn't really address the issue).

Jul 19 '05 #1
4 2449
Something in this vein?
baseString = "string with %s"
[ baseString % ss for ss in ('foo', 'baz') ] ['string with foo', 'string with baz'] b = " ".join([ baseString % ss for ss in ('foo', 'baz') ])
b 'string with foo string with baz'


-- Paul

Jul 19 '05 #2
"bill" <bi**********@gmail.com> writes:
Consider the following:

import os, commands
os.environ['QWE']="string with foo"
a = '$QWE ${QWE/foo/baz}'
b = commands.getoutput('echo ' + a)
This does what I want, which is to expand
a according to the standard bash expansion rules
(so b now references "string with foo string with baz"),
but it doesn't feel right.

Is there a more pythonic way to interpret strings according
to shell rules for word expansion? Relying on commands
feels like a kludge, and shlex is way too much work for this
(not to mention that it doesn't really address the issue).


Well, if you really want shell rules, I think you're out of
luck. Espcially since those rules tend to vary depending on what shell
you're running. I don't think that even shlex will do what you want.
If you just want the simple cases you used in your example, you might
check out the Template module in 2.4.

<mike
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 19 '05 #3
Quick glance at the reference manual, and I think that pipes.Template
may do exactly what I want. Is that what you're referring to?

I realized when I woke up that I have another slight irritant:
I need to be able to intelligently parse a command line. ie
I need to correctly parse each of the following strings:

"echo foo"
"/bin/echo 'the cat'"
"$(which echo) foo"

and generate the list:
["/bin/echo", arg] where arg is either "foo" or "the cat"

I believe that Template will help me with this as well. I'll look into
it today, after the dogs are walked...(note, shlex becomes annoying
here, as it will correctly tokenize "the cat" for me, but it splits
/bin/echo up into 4 tokens, which I now have to parse)

My current solution is to use os.system(a), but I'd much rather parse a
and use an exec function.

I'm not too concerned about portability accross different shells, as
long as it works in (ba)sh. Also, I'm constrained to python 2.2, so I
have no doubt that there is a "shInterpret" module in 2.3 or 2.4!!

Jul 19 '05 #4
Hmmm, the following session causes me some concern:
print a $(which sh) ${HOME/b/k} 'the dog' print b /bin/sh /home/kill the dog shlex.split(a) ['$(which', 'sh)', '${HOME/b/k}', 'the dog'] shlex.split(b)

['/bin/sh', '/home/kill', 'the', 'dog']

I started with a, which contains the 3 cases that I'm worried about:
expanding $(), variable expansion, and a token containing white space.
b was generated using commands.getoutput('echo ' +a). So b has most of
the right stuff, but it loses the parens and 'the dog' is split by
shlex. But shlex breaks up $(which sh). Is that correct behavior from
shlex? It feels like shlex should return '$(which sh)' as a single
token.

Jul 19 '05 #5

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

Similar topics

3
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but...
6
by: William Park | last post by:
(crossposted to comp.lang.python, because this may be of interest to them.) Python has try-block, within which you can raise exception. Once it's raised, execution breaks out of the try-block...
0
by: William Park | last post by:
1. Here is shell version of Python filter() for array. Essentially, you apply a command on each array element, and extract only those elements which it returns success (0). This is specialized...
11
by: Magnus Jonneryd | last post by:
Hi, I'm planning on writing a program that interactively is fed input via a shell (bash). I also want to be able to write a shell script that executes various commands related to my program. In...
1
by: Robert Phan | last post by:
I was modified the expansions.xml to learn about expansion snippet. Now I got error when trying to invoke the expansion. Error is "No expansion template were found...". Do I needed to reinstall...
16
by: John Salerno | last post by:
Hi all. I just installed Ubuntu and I'm learning how to use the bash shell. Aside from the normal commands you can use, I was wondering if it's possible to use Python from the terminal instead of...
6
by: Ishpeck | last post by:
I'm using Python to automate testing software for my company. I wanted the computers in my testing lab to automatically fetch the latest version of the python scripts from a CVS repository and...
4
by: melmack3 | last post by:
Hello My PHP script executes many bash/cmd commands. Functions like "exec()" or "system()" cause that new bash/cmd session is started, the command is executed and the session is closed....
6
by: Frantisek Malina | last post by:
What is the best way to do the regular bash commands in native python? - create directory - create file - make a symlink - copy a file to another directory - move a file - set permissions ...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.