[kw-pm] Diffing files with '$' in the name

Nick Dumas nick.dumas at gmail.com
Wed Mar 16 16:02:33 PDT 2011


When I do:
-----
#!/usr/bin/perl
use strict;
use warnings;

my $f1 = '$file@@foo';
my $f2 = '$file@@bar';

my $res = `diff $f1 $f2`;
print $res, "\n";
-----

I get a perfectly normal result from diff.
I'm running under WinXP however.

Does your shell require you to escape those characters?
If so you'll also have to escape the escapes when putting them into
the string literals.

 - Nick

> Message: 1
> Date: Wed, 16 Mar 2011 06:41:38 -0400 (EDT)
> From: "Robert P. J. Day" <rpjday at crashcourse.ca>
> To: KW Perl list <kw-pm at mail.pm.org>
> Subject: [kw-pm] trying to diff two files which contain '$' in their
>        names
> Message-ID:
>        <alpine.DEB.2.00.1103160637020.3832 at localhost6.localdomain6>
> Content-Type: TEXT/PLAIN; charset=US-ASCII
>
>
>  a colleague presents me with the following (simple?) perl problem.
> first, here's a variation of the program that works:
>
> #!/usr/bin/perl
>
> $f1 = 'file1';
> $f2 = 'file2';
>
> $res = `diff $f1 $f2`;
> print $res, "\n";
>
>  piece of cake -- there are two variables that contain the names of
> perfectly respectable files, those files are diff'ed and the result of
> the diff is saved and printed.
>
>  the problem is that the filenames sometimes contain a literal dollar
> sign as part of the name (don't ask).  so a simplified demo of the
> problem would be:
>
> #!/usr/bin/perl
>
> $f1 = '$file1';
> $f2 = '$file2';
>
> $res = `diff $f1 $f2`;
> print $res, "\n";
>
> which gives me:
>
> diff: missing operand after `diff'
> diff: Try `diff --help' for more information.
>
>  i've tried to backslash escape various things but no success.  what
> bit of magic am i missing?
>
> rday
>
>
> ------------------------------
>
> Message: 2
> Date: Wed, 16 Mar 2011 07:29:49 -0400 (EDT)
> From: "Robert P. J. Day" <rpjday at crashcourse.ca>
> To: KW Perl list <kw-pm at mail.pm.org>
> Subject: Re: [kw-pm] trying to diff two files which contain '$' in
>        their names
> Message-ID:
>        <alpine.DEB.2.00.1103160726550.4265 at localhost6.localdomain6>
> Content-Type: TEXT/PLAIN; charset=US-ASCII
>
>
>  as a followup to my earlier post, here's what appears to be a
> solution to what i was after:
>
> $pre1 = '$file';
> $post1 = ' 1';
> $pre2 = '$file';
> $post2 = ' 2';
>
> $res = `diff \'${pre1}\'@@\'${post1}\' \'${pre2}\'@@\'${post2}\'`;
> print $res, "\n";
>
>  it was clarified for me that the two files to be diff'ed had names
> of the format:
>
>  1) some arbitrary string
>  2) two literal '@' characters
>  3) another arbitrary string
>
> and all of those strings could conceivably have embedded whitespace or
> literal dollar signs.  argh.  who the $%#^&^&% thought that was a sane
> idea?
>
>  in any event, you can see how i whipped up a test program that
> simulated that, and protected everything inside the backticks with
> escaped single quotes, which appears to work.
>
>  thoughts?
>
> rday
>
> --
>
> ========================================================================
> Robert P. J. Day                               Waterloo, Ontario, CANADA
>                        http://crashcourse.ca
>
> Twitter:                                       http://twitter.com/rpjday
> LinkedIn:                               http://ca.linkedin.com/in/rpjday
> ========================================================================
>
>
> ------------------------------
>
> _______________________________________________
> kw-pm mailing list
> kw-pm at pm.org
> http://mail.pm.org/mailman/listinfo/kw-pm
>
> End of kw-pm Digest, Vol 92, Issue 5
> ************************************
>


More information about the kw-pm mailing list