Loading ...
Sorry, an error occurred while loading the content.

Need some assistance

Expand Messages
  • perl_steve_novice
    I m trying to parse out the second column of a ps -ef command. My split seems to be getting choked up on leading white space at the beginning of the command
    Message 1 of 8 , Sep 13, 2005
    • 0 Attachment
      I'm trying to parse out the second column of a ps -ef command. My
      split seems to be getting choked up on leading white space at the
      beginning of the command but I'm not sure. Could someone help?

      # ps -ef | grep username | grep bash | grep -v grep
      username 25648 25646 0 05:08:05 pts/1 0:01 -bash
      #

      $proc = `ps -ef | grep -v grep | grep $username | grep $shell`;

      foreach $var ($proc) {

      ($var) = (split /' '/)[2];
      print "$var\n";
      }
    • Jeff 'japhy' Pinyan
      ... You probably just want $var = (split )[2]; or even just $var = (split)[2]; You have split / / which means split on a single quote followed by a
      Message 2 of 8 , Sep 13, 2005
      • 0 Attachment
        On Sep 13, perl_steve_novice said:

        > I'm trying to parse out the second column of a ps -ef command. My
        > split seems to be getting choked up on leading white space at the
        > beginning of the command but I'm not sure. Could someone help?

        > ($var) = (split /' '/)[2];

        You probably just want

        $var = (split ' ')[2];

        or even just

        $var = (split)[2];

        You have "split /' '/" which means "split on a single quote followed by a
        space followed by a single quote" which is not at ALL what you want.

        --
        Jeff "japhy" Pinyan % How can we ever be the sold short or
        RPI Acacia Brother #734 % the cheated, we who for every service
        http://www.perlmonks.org/ % have long ago been overpaid?
        http://princeton.pm.org/ % -- Meister Eckhart
      • perl_steve_novice
        Thanks Jeff for the quick response. I tried using $var = (split)[2]; AND $var = (split )[2]; And here is the results for both tries Use of uninitialized
        Message 3 of 8 , Sep 13, 2005
        • 0 Attachment
          Thanks Jeff for the quick response. I tried using

          $var = (split)[2]; AND

          $var = (split ' ')[2];

          And here is the results for both tries


          Use of uninitialized value in split at ./check_ccache_procs.pl line
          35, <STDIN> line 2.

          --- In perl-beginner@yahoogroups.com, Jeff 'japhy' Pinyan
          <japhy@p...> wrote:
          > On Sep 13, perl_steve_novice said:
          >
          > > I'm trying to parse out the second column of a ps -ef command. My
          > > split seems to be getting choked up on leading white space at the
          > > beginning of the command but I'm not sure. Could someone help?
          >
          > > ($var) = (split /' '/)[2];
          >
          > You probably just want
          >
          > $var = (split ' ')[2];
          >
          > or even just
          >
          > $var = (split)[2];
          >
          > You have "split /' '/" which means "split on a single quote
          followed by a
          > space followed by a single quote" which is not at ALL what you
          want.
          >
          > --
          > Jeff "japhy" Pinyan % How can we ever be the sold short or
          > RPI Acacia Brother #734 % the cheated, we who for every service
          > http://www.perlmonks.org/ % have long ago been overpaid?
          > http://princeton.pm.org/ % -- Meister Eckhart
        • Jeff 'japhy' Pinyan
          ... That means $_ (which is what split() is splitting) is uninitialized. You d have to show more code for us to know why, though. -- Jeff japhy Pinyan
          Message 4 of 8 , Sep 13, 2005
          • 0 Attachment
            On Sep 13, perl_steve_novice said:

            > Thanks Jeff for the quick response. I tried using
            >
            > $var = (split)[2]; AND
            >
            > $var = (split ' ')[2];
            >
            > And here is the results for both tries
            >
            > Use of uninitialized value in split at ./check_ccache_procs.pl line
            > 35, <STDIN> line 2.

            That means $_ (which is what split() is splitting) is uninitialized.
            You'd have to show more code for us to know why, though.

            --
            Jeff "japhy" Pinyan % How can we ever be the sold short or
            RPI Acacia Brother #734 % the cheated, we who for every service
            http://www.perlmonks.org/ % have long ago been overpaid?
            http://princeton.pm.org/ % -- Meister Eckhart
          • Jeff 'japhy' Pinyan
            ... Oh, I see the problem. I hadn t paid close enough attention to your original code: $proc = `ps -ef | grep -v grep | grep $username | grep $shell`; foreach
            Message 5 of 8 , Sep 13, 2005
            • 0 Attachment
              On Sep 13, Jeff 'japhy' Pinyan said:

              > On Sep 13, perl_steve_novice said:
              >
              >> Thanks Jeff for the quick response. I tried using
              >>
              >> $var = (split)[2]; AND
              >>
              >> $var = (split ' ')[2];
              >>
              >> And here is the results for both tries
              >>
              >> Use of uninitialized value in split at ./check_ccache_procs.pl line
              >> 35, <STDIN> line 2.
              >
              > That means $_ (which is what split() is splitting) is uninitialized.
              > You'd have to show more code for us to know why, though.

              Oh, I see the problem. I hadn't paid close enough attention to your
              original code:

              $proc = `ps -ef | grep -v grep | grep $username | grep $shell`;

              foreach $var ($proc) {
              ($var) = (split /' '/)[2];
              print "$var\n";
              }

              Try this instead:

              @proc = `ps -ef | ...`; # @proc instead of $proc

              That gives you each line of output as its own element in the @proc array.
              Then we loop over its elements:

              for $var (@proc) {
              $value = (split ' ', $var)[2];
              print "$value\n";
              }

              You could also loop over @proc with $_, which would make my suggested code
              work:

              for (@proc) {
              $var = (split)[2];
              print "$var\n";
              }

              --
              Jeff "japhy" Pinyan % How can we ever be the sold short or
              RPI Acacia Brother #734 % the cheated, we who for every service
              http://www.perlmonks.org/ % have long ago been overpaid?
              http://princeton.pm.org/ % -- Meister Eckhart
            • perl_steve_novice
              Here is my entire script. #!/usr/local/bin/perl -w # # # chomp (my $grep = /usr/bin/grep ); chomp (my $DATE = `date`); chomp (my $HOST = `uname -n`); system
              Message 6 of 8 , Sep 13, 2005
              • 0 Attachment
                Here is my entire script.


                #!/usr/local/bin/perl -w
                #
                #
                #


                chomp (my $grep = "/usr/bin/grep");
                chomp (my $DATE = `date`);
                chomp (my $HOST = `uname -n`);

                system ('clear');
                print "Please enter the userid you wish to check: ";
                chomp ($usrname = <STDIN>);
                &err_user;
                print "\n";
                system ('nismatch', $usrname, 'passwd.org_dir');
                print "\n";
                print "What is the users shell? ";
                chomp ($shell = <STDIN>);
                &err_shell;
                print "\n";
                chomp (my $procid = `ps -ef | grep $usrname | grep $shell |
                grep -v grep`);
                print "Timestamp: $DATE\n";
                print "Active shell sessions for $usrname on $HOST\n";
                print "===================================================\n";
                print "$procid\n";
                print "\n";


                foreach $var ($procid) {

                ($var) = (split ' ')[2];
                $ccache = `/router/bin/ccache-check $var | egrep 'Errors were
                found:'`;
                print "$var was checked for ccache errors: $ccache\n";
                }



                sub err_user

                {

                while ($usrname =~ /\d/) {
                system('clear');
                print "Please enter a username that only contains LETTERS: ";
                chomp ($usrname = <STDIN>);
                }
                }

                sub err_shell

                {
                while () {

                if ($shell =~ /\bbash\b/) {
                last;
                }
                elsif ($shell =~ /\bksh\b/) {
                last;
                }
                elsif ($shell =~ /\bcsh\b/) {
                last;
                }
                elsif ($shell =~ /\btcsh\b/) {
                last;
                }
                elsif ($shell =~ /\bsh\b/) {
                last;
                }
                else {
                system('clear');
                print "\n";
                print " ***** PROBLEM - you choose an incorrect
                shell *****\n";
                print "\n";
                print "Please choose from one of the following shells
                (bash, ksh, csh, tcsh or sh): ";
                chomp ($shell = <STDIN>);
                print "\n";
                sleep 2;
                }
                }
                }
              • Huaer XC
                you should change $proc to @proc: #$proc = `ps -ef | grep -v grep | grep $username | grep $shell`; @proc = ...... foreach (@proc) { #here you use $_ instead
                Message 7 of 8 , Sep 13, 2005
                • 0 Attachment
                  you should change $proc to @proc:

                  #$proc = `ps -ef | grep -v grep | grep $username | grep $shell`;

                  @proc = ......

                  foreach (@proc) { #here you use $_ instead of $var,
                  #use @proc instead of $proc
                  $var = (split)[2];
                  print "$var\n";
                  }
                  ----------
                • perl_steve_novice
                  Thanks Jeff and Huaer XC. I used an array and was able to parse out the data like I wanted.
                  Message 8 of 8 , Sep 13, 2005
                  • 0 Attachment
                    Thanks Jeff and Huaer XC. I used an array and was able to parse out
                    the data like I wanted.
                  Your message has been successfully submitted and would be delivered to recipients shortly.