469,631 Members | 1,134 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

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 2305
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by John Bowling | last post: by
6 posts views Thread by William Park | last post: by
reply views Thread by William Park | last post: by
11 posts views Thread by Magnus Jonneryd | last post: by
1 post views Thread by Robert Phan | last post: by
16 posts views Thread by John Salerno | last post: by
6 posts views Thread by Ishpeck | last post: by
6 posts views Thread by Frantisek Malina | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.