variable with empty space

Y

yo

Hi, 1st off I'm not a perl programmer and just know a few things that
I have picked up here and there.

I'm having trouble trying to figure out how to handle a value that's
being passed to my perl script. The value is, " !", six spaces
and and a ! mark. I am having a difficult time making the script stop
executing the rest of the perl code when it sees this valued being
passed to it.

Ideally if the value being passed to it has a space, all spaces or
just on space I would like the script to stop also.



I tried the following

if ($username eq '')
if ($username eq ' ')
if ($username)
if ($username eq " ")
if ($username eq '' !")
if ($username eq '' \ !")

I do however want to be able to have this perl script run all the way
through if it does not see the value above or any blank space.

Heres part of the script that matters.


#!/usr/bin/perl

open(INFILE, "@ARGV[0]") ||
die("Can't open @ARGV[0] for processing");
$username = <INFILE>;
close(INFILE);

@rm_user = ("/usr/sbin/userdel $username","\n");

##### check empty dirs

if ($username eq ' ')
{
print "\n";

die("null variable");
}

elsif ($username eq "mail")
{
print "\n";

die(" ");
}

elsif ($username eq "root")
{
print "\n";

die(" ");
}


elsif (length($username) == 1) # If above fails, try this
{
print "The string has one character\n";

@rm_user_outmail_me = ("cat /dev/null | /bin/mail -s
one.lenght (e-mail address removed)","\n");

die("1 lenght variable");
}
else # Now, everything has failed
{


TIA, P
 
P

Peter J. Holzer

I'm having trouble trying to figure out how to handle a value that's
being passed to my perl script. The value is, " !", six spaces
and and a ! mark. I am having a difficult time making the script stop
executing the rest of the perl code when it sees this valued being
passed to it.

I tried the following

if ($username eq '')
if ($username eq ' ')
if ($username)
if ($username eq " ")
if ($username eq '' !")
if ($username eq '' \ !")

why didn't you try the obvious

if ($username eq " !")

?
Ideally if the value being passed to it has a space, all spaces or
just on space I would like the script to stop also.

One space anywhere in the username?

That would be (for example)

if ($username =~ / /)
Heres part of the script that matters.


#!/usr/bin/perl

open(INFILE, "@ARGV[0]") ||
die("Can't open @ARGV[0] for processing");
$username = <INFILE>;

You almost certainly want a
chomp($username);
here, otherwise $username will contain a trailing newline.

Also, you only read the first line of the file. Is this what you want?
close(INFILE);

hp
 
Y

yo

hmmm i tried that althought it didnt work, im seeing the ! come
through, at least its showing up on the logs and i can't seem to match
that string.

However i changed your example below to this if ($username =~ /!/)
and that worked.

thanks

I'm having trouble trying to figure out how to handle a value that's
being passed to my perl script. The value is, " !", six spaces
and and a ! mark. I am having a difficult time making the script stop
executing the rest of the perl code when it sees this valued being
passed to it.

I tried the following

if ($username eq '')
if ($username eq ' ')
if ($username)
if ($username eq " ")
if ($username eq '' !")
if ($username eq '' \ !")

why didn't you try the obvious

if ($username eq " !")

?
Ideally if the value being passed to it has a space, all spaces or
just on space I would like the script to stop also.

One space anywhere in the username?

That would be (for example)

if ($username =~ / /)
Heres part of the script that matters.


#!/usr/bin/perl

open(INFILE, "@ARGV[0]") ||
die("Can't open @ARGV[0] for processing");
$username = <INFILE>;

You almost certainly want a
chomp($username);
here, otherwise $username will contain a trailing newline.

Also, you only read the first line of the file. Is this what you want?
close(INFILE);

hp
 
J

John W. Krahn

yo said:
Hi, 1st off I'm not a perl programmer and just know a few things that
I have picked up here and there.

I'm having trouble trying to figure out how to handle a value that's
being passed to my perl script. The value is, " !", six spaces
and and a ! mark. I am having a difficult time making the script stop
executing the rest of the perl code when it sees this valued being
passed to it.

Ideally if the value being passed to it has a space, all spaces or
just on space I would like the script to stop also.



I tried the following

if ($username eq '')
if ($username eq ' ')
if ($username)
if ($username eq " ")
if ($username eq '' !")
if ($username eq '' \ !")

You probably want to use a regular expression:

if ( $username =~ /\A *!?\z/ )

I do however want to be able to have this perl script run all the way
through if it does not see the value above or any blank space.

Heres part of the script that matters.


#!/usr/bin/perl

use warnings;
use strict;

You should include these two pragmas and let perl help you find mistakes.

open(INFILE, "@ARGV[0]") ||

You are using an array slice when you want a scalar and you are needlessly
quoting it.

open(INFILE, $ARGV[0]) ||

die("Can't open @ARGV[0] for processing");

Again, you want a scalar not a slice:

die("Can't open $ARGV[0] for processing");

$username = <INFILE>;
close(INFILE);

@rm_user = ("/usr/sbin/userdel $username","\n");

That should either be:

my $rm_user = "/usr/sbin/userdel $username";

Or:

my @rm_user = ( '/usr/sbin/userdel', $username );

You don't need a newline in there in either case.

##### check empty dirs

if ($username eq ' ')
{
print "\n";

die("null variable");
}

elsif ($username eq "mail")
{
print "\n";

die(" ");
}

elsif ($username eq "root")
{
print "\n";

die(" ");
}


elsif (length($username) == 1) # If above fails, try this
{
print "The string has one character\n";

@rm_user_outmail_me = ("cat /dev/null | /bin/mail -s
one.lenght (e-mail address removed)","\n");

Again, you don't need a newline at the end and perl will interpolate @meganet
as an array so you need to either escape the '@' character or use single quotes.

$rm_user_outmail_me = 'cat /dev/null | /bin/mail -s one.length
(e-mail address removed)';

die("1 lenght variable");

die("1 length variable");

}
else # Now, everything has failed
{


John
 
J

Josef Moellers

yo said:
hmmm i tried that althought it didnt work, im seeing the ! come
through, at least its showing up on the logs and i can't seem to match
that string.

You tried ... what?
Please don't top-post and please attribute replies to whatever you are
replying to, as " " has given you two different possible solutions and
now I'm to guess which one you tried ... Or did you indeed try both?

If both solutions failed, maybe you don't have a blank in there but
maybe a tab.

Try
if ($username =~ /\s/)

(ouch, ouch, don't hit me ... yes ... leaning toothpick ...)

if ($username =~ m|\s|)

Josef
 
B

Ben Morrow

Quoth Josef Moellers said:
if ($username =~ /\s/)

(ouch, ouch, don't hit me ... yes ... leaning toothpick ...)

if ($username =~ m|\s|)

Or
if ($username =~ m{\s}) {

, as | is meta in a regex. :)

Ben
 
J

Josef Moellers

Ben said:
Or
if ($username =~ m{\s}) {

, as | is meta in a regex. :)

... and so are '{' and '}':

\s{1,10)
matches 1 to 10 blanks

But you're right: if I use | as a delimiter, I won't be able to specify
alternations.
Never thought of that :-(

Thanks for pointing it out,

Josef
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top