turn off special characters in a string

W

wong_powah

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

davidfilmer

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.
 
T

Tad McClellan

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?
 
M

Mirco Wahab

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.
 
J

Josef Moellers

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.
 
U

usenet

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

Mario D'Alessio

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
 

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
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top