variable with empty space

Discussion in 'Perl Misc' started by yo, Aug 22, 2006.

  1. yo

    yo Guest

    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 ","\n");

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


    TIA, P
    yo, Aug 22, 2006
    #1
    1. Advertising

  2. yo

    Guest

    yo wrote:
    [a multipoted question]

    But the bot is presently turned off....

    --
    David Filmer (http://DavidFilmer.com)
    , Aug 22, 2006
    #2
    1. Advertising

  3. On Tue, 22 Aug 2006 14:20:41 -0400, wrote:
    > 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

    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
    Peter J. Holzer, Aug 22, 2006
    #3
  4. yo

    yo Guest

    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

    On Tue, 22 Aug 2006 20:48:44 +0200, "Peter J. Holzer"
    <> wrote:

    >On Tue, 22 Aug 2006 14:20:41 -0400, wrote:
    >> 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
    yo, Aug 22, 2006
    #4
  5. yo wrote:
    > 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 ","\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
    ';


    >
    > die("1 lenght variable");


    die("1 length variable");


    > }
    > else # Now, everything has failed
    > {



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Aug 22, 2006
    #5
  6. <> wrote:
    > yo wrote:
    > [a multipoted question]
    >
    > But the bot is presently turned off....



    I like "this bot" a lot better.

    :)


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Aug 23, 2006
    #6
  7. yo wrote:
    > 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?

    >>On Tue, 22 Aug 2006 14:20:41 -0400, wrote:


    >>why didn't you try the obvious
    >>
    >> if ($username eq " !")


    >>One space anywhere in the username?
    >>
    >>That would be (for example)
    >>
    >> if ($username =~ / /)
    >>


    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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Aug 23, 2006
    #7
  8. yo

    Ben Morrow Guest

    Quoth Josef Moellers <>:
    >
    > 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

    --
    Every twenty-four hours about 34k children die from the effects of poverty.
    Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
    that image, that ghastly, grey-billowing, double-barrelled fall, repeated
    twelve times every day. Full of children. [Iain Banks]
    Ben Morrow, Aug 23, 2006
    #8
  9. Ben Morrow wrote:
    > Quoth Josef Moellers <>:
    >
    >> 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. :)


    ... 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
    --
    Josef Möllers (Pinguinpfleger bei FSC)
    If failure had no penalty success would not be a prize
    -- T. Pratchett
    Josef Moellers, Aug 24, 2006
    #9
  10. yo

    Ben Morrow Guest

    Quoth Josef Moellers <>:
    > Ben Morrow wrote:
    > > Quoth Josef Moellers <>:
    > >
    > >> 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. :)

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


    ....but they nest in quoting constructs.

    m{ \s{1,10} }x;

    does what you think it does.

    Ben

    --
    Joy and Woe are woven fine,
    A Clothing for the Soul divine William Blake
    Under every grief and pine 'Auguries of Innocence'
    Runs a joy with silken twine.
    Ben Morrow, Aug 24, 2006
    #10
    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. Jas Shultz
    Replies:
    0
    Views:
    935
    Jas Shultz
    Dec 3, 2003
  2. Shuo Xiang

    Stack space, global space, heap space

    Shuo Xiang, Jul 9, 2003, in forum: C Programming
    Replies:
    10
    Views:
    2,855
    Bryan Bullard
    Jul 11, 2003
  3. Christian Seberino
    Replies:
    21
    Views:
    1,620
    Stephen Horne
    Oct 27, 2003
  4. Ian Bicking
    Replies:
    2
    Views:
    975
    Steve Lamb
    Oct 23, 2003
  5. Ian Bicking
    Replies:
    2
    Views:
    702
    Michael Hudson
    Oct 24, 2003
Loading...

Share This Page