Re: Regexes on the command line



On Thu, 26 Feb 2009, Tom McGlynn wrote:

On Feb 26, 4:40 pm, Tom McGlynn <t...@xxxxxxxxxxxxxxxxxxxxxx> wrote:
On Feb 26, 2:52 pm, Tom Anderson <t...@xxxxxxxxxxxxxxx> wrote:
On Thu, 26 Feb 2009, Tom McGlynn wrote:

where match is a little shell script

#!/bin/bash
java -cp ... somepackage.Match $*

When the java command inside the script executes it reanalyzes the command string and since the single quotes are gone it misinterprets the special characters. There are a number of ways to address this. You can use aliases rather than scripts, but that's a bit limiting. You could use Perl and do a multi-argument system call (I presume there are equivalents in Python, Ruby and elsewhere) which doesn't re- analyze the arguments. What I've tended to do is to analyze the input arguments and regenerate the command line in the script requoting any special characters.

java Match '$*' doesn't do it for you? Or better yet, IMHO, '$@'.

Thanks... That's not quite right but I think you just misremembered
it.
It should be
java Match "$@"
Once you pointed me to the proper variable I was able to see it in the
bash documentation.

I should probably have phrased this a little more cautiously but I was in a hurry. The other Tom clearly is more familiar than I with this, but at last on my machine '$*' and '$@' are not expanded by bash. Possibly they are in some of the other shells.

Argh! No, you're quite right, and i wasn't thinking straight. I had the idea that they'd both be expanded, but with different amounts of expansion of shell variables in them, but of course neither of them do any expansion of anything inside the string. '' just quotes, and "" expands the variable into a quoted string.

The funny thing is that i've spent the last week or two doing almost nothing but shell, so you'd have thought i'd be on top of this. Maybe i've gone snowblind instead.

"$*" expands as a single argument, so it doesn't quite do what I typically want. But "$@" is very very nice!

"$@" is a fine invention. I'm not really sure what the use of "$*" is!

tom

--
eggflip, brandy, bits of Tia Maria, Beecham's powder, aspirin,
Benedictine, Alka-Seltzer, black currant juice, a touch of mustard and
"other things"
.



Relevant Pages

  • Re: Creation of DSN
    ... Build a text string from the input data. ... rather execute the string that was just built. ... script file has been processed. ... > I'm using directly a Command, ...
    (microsoft.public.vb.database.ado)
  • Re: Gimp invocation from Java
    ... The script in question is a small batch file that simply invokes GIMP to execute a pre-defined Scheme program that watermarks and converts an image file. ... private static void convert (String files, String targetDir) {int retV = 0; ... Java environments to no avail). ...
    (comp.lang.java.help)
  • Re: Regexp vs string match
    ... power of the regexp command on my example. ... On my simple example, the "string matching"-based script ... set username ...
    (comp.lang.tcl)
  • Re: Regexes on the command line
    ... When the java command inside the script executes it reanalyzes the ... any expansion of anything inside the string. ...
    (comp.lang.java.programmer)
  • Re: export question
    ... -- export JAVA ... the script doesn't spawn a new shell so the export command is executed ... If you execute the shell script in your current ...
    (Debian-User)