Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^4: csh, alias, perl & quotes

by Smylers (Pilgrim)
on Jun 17, 2005 at 14:57 UTC ( [id://467749]=note: print w/replies, xml ) Need Help??


in reply to Re^3: csh, alias, perl & quotes
in thread csh, alias, perl & quotes

alias rfw "perl -ne '/\$mail_cmd/ || print;' \!^"

You need quoting at several levels:

  1. When defining the alias any special characters that are supposed to be in the alias need protecting from being acted upon immediately.
  2. The alias needs to end up with enough quotes or escaping in it so that special characters get passed to Perl rather than being interpreted by the shell.
  3. Perl itself can need some things escaping.

Each level may add quoting characters which themselves need protecting at the previous level, so let's take them in reverse. Ignore aliasing and the shell for the moment, you want a Perl program that consists of:

/\$mail_cmd/ || print;

That backslash has to be there so that Perl treats the dollar sign as a literal dollar, rather than trying to interpolate a Perl variable. However the semicolon is unnecessary and the double bars can be replaced by something else, so let's get rid of those — quoting in C-Shell aliases is rather broken, so minimizing the number of characters that need protecting is a good start. This'll work:

print unless /\$mail_cmd/

That needs quoting as a whole for passing to the shell. It contains a dollar sign, so double quotes can't be used, because in the C Shell dollars in double quotes are always acted upon by the shell. So let's use single quotes for this:

perl -wne 'print unless /\$mail_cmd/'

Now we want to set up an alias with that as the value. Spaces in an alias definition don't usually matter: even if the entire definition is parsed as a single parameter now, it's going to get split on spaces when it gets expanded. The only special characters in the above which need protecting are the single quotes, the backslash, and the dollar. So one way of writing the alias is simply to stick a backslash before each of those characters (and before the exclamation mark in the alias's placeholder):

alias rfw perl -wne \'print unless /\\\$mail_cmd/\' \!^

Again because of the dollar sign there's no point in trying to use double quotes. But you could use single quotes to protect everything except for the single quotes. There's only one set of single quotes in the alias expansion, so you could put a new set of single quotes just inside them (to protect everything that's between them) and just put backslashes before each of the outer single quotes (to protect them):

alias rfw perl -wne \''print unless /\$mail_cmd/'\' \!^

Smylers

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://467749]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-04-25 08:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found