turn off special characters in a string

Discussion in 'Perl Misc' started by wong_powah@yahoo.ca, Sep 18, 2007.

  1. Guest

    My string has special characters '@' and '$'.
    How to escape them (turn them off)?
    e.g.
    $newpw is entered as "1q@W3e$R";

    chop($newpw = <STDIN>);
    system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");

    $newpw becomes "1q@W3e" (the "$R" is chopped).
    , Sep 18, 2007
    #1
    1. Advertising

  2. Guest

    On Sep 18, 2:22 pm, wrote:
    > My string has special characters '@' and '$'.
    > How to escape them (turn them off)?
    > e.g.
    > $newpw is entered as "1q@W3e$R";
    >
    > chop($newpw = <STDIN>);
    > system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
    >
    > $newpw becomes "1q@W3e" (the "$R" is chopped).


    perldoc -f quotemeta

    FWIW, You probably want to use backticks instead of system(), and you
    should check your return code.


    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
    , Sep 18, 2007
    #2
    1. Advertising

  3. <> wrote:


    > chop($newpw = <STDIN>);



    You should use chomp() to remove newlines nowadays.

    Using chop() to remove newlines was how it was done 10 years ago.

    Where are you learning your Perl from?


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad McClellan, Sep 19, 2007
    #3
  4. Mirco Wahab Guest

    wrote:
    > My string has special characters '@' and '$'.
    > How to escape them (turn them off)?
    > $newpw is entered as "1q@W3e$R";
    > chop($newpw = <STDIN>);
    > system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
    > $newpw becomes "1q@W3e" (the "$R" is chopped).


    This would normally not happen. I don't know
    what you did else, but maybe your "quoting interpolation"
    will happen only in your "test example", see:

    ...
    my $command = 'echo'; # change this to 'changepw'

    my $oldpw = '1q@W3e$Q' . "\n"; # this is how input would come from
    my $newpw = '1q@W3e$R' . "\n"; # outside (note the '' quote chars!)

    chomp $oldpw; # don't use chop(), always use chomp()
    chomp $newpw; # if possible (as Tad already said)

    # use the qq{ .. } operator to evade quote masking errors
    system qq{ $command -newpw "$newpw" -oldpw "$oldpw" };
    ...

    If your program is larger, put a

    use strict;
    use warnings;
    ...

    on top of it and Perl will tell you where
    unwanted interpolation might happen.

    Regards

    M.
    Mirco Wahab, Sep 19, 2007
    #4
  5. wrote:
    > My string has special characters '@' and '$'.
    > How to escape them (turn them off)?
    > e.g.
    > $newpw is entered as "1q@W3e$R";
    >
    > chop($newpw = <STDIN>);
    > system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
    >
    > $newpw becomes "1q@W3e" (the "$R" is chopped).
    >


    No, it's not, at least not by perl.
    However, your shell will most likely do this. Try to use single quotes
    instead of the double quotes:

    system("changepw -newpw '$newpw' -oldpw '$oldpw'");

    Note that you don't need a LF at the end of a system() string.

    --
    These are my personal views and not those of Fujitsu Siemens Computers!
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize (T. Pratchett)
    Company Details: http://www.fujitsu-siemens.com/imprint.html
    Josef Moellers, Sep 19, 2007
    #5
  6. Guest

    On Sep 19, 12:10 am, Josef Moellers <josef.moell...@fujitsu-
    siemens.com> wrote:
    > Try to use single quotes instead of the double quotes:
    > system("changepw -newpw '$newpw' -oldpw '$oldpw'");


    Of course, if the password contains a single-quote, this fails. And
    if a user is malicious and sets their password to something like this:

    x'; rm -rf /;

    then your script just deleted your whole system (assuming it runs as
    root, which it probably does, since it is changing passwords). Guess
    what? You're fired!


    --
    The best way to get a good answer is to ask a good question.
    David Filmer (http://DavidFilmer.com)
    , Sep 19, 2007
    #6
  7. <> wrote in message
    news:...
    > My string has special characters '@' and '$'.
    > How to escape them (turn them off)?
    > e.g.
    > $newpw is entered as "1q@W3e$R";
    >
    > chop($newpw = <STDIN>);
    > system("changepw -newpw \"$newpw\" -oldpw \"$oldpw\" \n\");
    >
    > $newpw becomes "1q@W3e" (the "$R" is chopped).



    To avoid escaping quotes, your system call could be (note that
    you don't need the \n at the end):

    system( qq( changepw -newpw "$newpw" -oldpw "$oldpw" ));

    From Perl docs on "system" function: "If there is only one scalar argument,
    the argument is checked for shell metacharacters, and if there are any, the
    entire argument is passed to the system's command shell for parsing." So,
    the command line you have is being passed to your shell for processing. Your
    shell is probably interpreting the $... as variables, effectively stripping
    them. To avoid this, pass multiple args to "system":

    system( 'changepw', '-newpw', $newpw, '-oldpw', $oldpw );

    Perl will now call changepw directly rather than using the shell.

    Mario
    Mario D'Alessio, Sep 19, 2007
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stefan Mueller
    Replies:
    3
    Views:
    32,952
    Stefan Mueller
    Jul 23, 2006
  2. robert112
    Replies:
    1
    Views:
    422
    Juan T. Llibre
    Apr 26, 2007
  3. Replies:
    2
    Views:
    1,070
    Ingo Menger
    May 31, 2007
  4. rvino
    Replies:
    0
    Views:
    4,635
    rvino
    Aug 14, 2007
  5. majna
    Replies:
    4
    Views:
    638
    Thomas 'PointedEars' Lahn
    Sep 19, 2007
Loading...

Share This Page