bash like expansion

B

bill

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).
 
M

Mike Meyer

bill said:
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
 
B

bill

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!!
 
B

bill

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top