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

Re: [PBML] repeated use of grep

Expand Messages
  • Glenn Becker
    John, ... Thanks for the reply. I found myself thinking with this step of the project that since the lines I wanted went name, location, name, location and so
    Message 1 of 11 , Jun 11, 2007
    View Source
    • 0 Attachment
      John,

      > Note that if there's only two pieces of information per package in
      > the PACKAGES.TXT file, you might want to use a hash instead of the
      > two arrays.

      Thanks for the reply. I found myself thinking with this step of the
      project that since the lines I wanted went name, location, name,
      location and so on, that it would be a good candidate for pulling into
      a hash: they're pairs. And then I thought, well, would a more
      complicated structure be more efficient/relevant, since there are many
      files under each location? Something like a hash of array references?
      And that's where I started to get tired and kinda lost.

      So I stepped back and tried to go more slowly, step by step ... and
      got MORE lost. :^D

      Regards,

      G
    • Alan
      ... Hi. [ . . ] ... FWIW there s a (freely available) shell script (which I use) entitled slackupdate that does just this. But, with slackupdate, (there s one
      Message 2 of 11 , Jun 11, 2007
      View Source
      • 0 Attachment
        On Sunday 10 June 2007 18:28, Glenn Becker wrote:
        > Hi, All -

        Hi.
        [ . . ]
        > A little context on what I am trying to do here. I use Slackware
        > Linux, which lacks the upgrading systems (like apt or yum) that other
        > Linux distributions have. So today I decided I would try to write a
        > Perl program to automate the upgrading of packages on a stable
        > Slackware box.

        FWIW there's a (freely available) shell script (which I use) entitled
        slackupdate that does just this.

        But, with slackupdate, (there's one interim, manual step involved) which is
        you run Slack's upgradepkg command on the updates after slackupdate downloads
        the updates. (reasons do exist to the effect that this is a good thing).

        I wrote my own Perl wrapper (enclosed below) for slackupdate which acts as the
        control agent to control slackupdate. I shared this Perl wrapper with the
        author of slackupdate. But I received no reply from the author of
        slackupdate. And, yes, I know that this forks (launches a new or sub shell).

        My intention is to one day rewrite slackupdate in Perl. But I must get
        cracking for my shell script skills aren't advanced enough for me to fully
        understand slackupdate. And my Perl skills may or may not be quite advanced
        enough for me to do all of the things that slackupdate does.

        If the OP desires, OP may email me private about I did a very slight mod to
        slackupdate to make it work with my Perl wrapper. Though it's entirely
        possible that it may be rather easy to see what's needed to do by studying my
        Perl wrapper and by studying both slackupdate's options as well as all of the
        functions that are in slackupdate (I set slackupdate to not check for a new
        slackupdate version since I handle this in the Perl wrapper and do it only
        every so often *when I want to do it*).

        slackupdate itself can be downloaded from the darklinux url which is found at
        almost the end of my Perl wrapper.

        Here's my Perl wrapper:

        #!/usr/bin/perl
        use warnings;
        use strict;

        print "\nChecking . . .\n\n";
        my $txt;
        my $junk;
        my $blaklistd;
        my @stuff = `slackupdate -l 0 -s
        ftp://slackware.mirrors.tds.net/pub/slackware`;
        for ( @stuff ) {
        $blaklistd .= $_ if /Blacklisted/;
        unless ( /Welcome|Initializing|Entering|Checking|Using|Blacklisted|^ / ) {
        # print;
        $txt .= $_;
        }
        }
        shift @stuff;
        shift @stuff;
        $junk = pop @stuff;
        $junk = pop @stuff;
        $junk = pop @stuff;
        # $junk = pop @stuff;
        push @stuff, $blaklistd if $blaklistd;
        print @stuff, "\n";

        my $choice;
        if ($txt) {
        $txt =~ s/\n\n//g;
        print "Update\(s\) listed next were found at above server\n";
        print $txt;
        print <<STUF;

        The default is that we stop here without downloading.
        If you've verified the changelog says no discontinued etc.
        IOW "*only* updates to existing official packages are allowed here."
        Know also about slackupdate_blacklist when and why to use it.
        If in doubt, just strike the enter key to end.
        You *must* enter a 1 to download the new updates.
        Or enter a 2 loads changelog in firefox and downloads updates
        Or enter a 3 loads Updater version check in firefox and downloads updates
        STUF
        print "\n**Read the above**";
        print "\nContinue? Enter a 1 or a 2 or 3 to download: ";
        chomp($choice = <STDIN>);
        unless( $choice =~ /^1$|^2$|^3$/ ) {
        $choice = 8;
        }
        }

        $choice = 9 unless $txt;
        if( $choice == 2 ) {
        print "Downloading . . .\n";
        `firefox
        http://slackware.mirrors.tds.net/pub/slackware/slackware-11.0/ChangeLog.txt`;
        system( "slackupdate -s ftp://slackware.mirrors.tds.net/pub/slackware" );
        }elsif( $choice == 8 ) {
        print "We ended without downloading because you didn't enter a 1 or 2 or
        3\n";
        }elsif( $choice == 1 ) {
        print "Downloading . . .\n";
        system( "slackupdate -s ftp://slackware.mirrors.tds.net/pub/slackware" );
        }elsif( $choice == 3 ) {
        print 'http://www.slackware.com/changelog/stable.php?cpu=i386', "\n";
        print "Downloading . . .\n";
        `firefox http://www.darklinux.net/slackupdate/`;
        system( "slackupdate -s ftp://slackware.mirrors.tds.net/pub/slackware" );
        } else {
        print "Your Slack is currently up2date\nThere's no new updates at above
        server\n";
        }
        # end

        --
        Alan.
      • Glenn Becker
        Below is what I came up with to turn the relevant lines of PACKAGES.TXT into a hash. It appears to work, tho I am confident there is a (are many!) way(s) to do
        Message 3 of 11 , Jun 11, 2007
        View Source
        • 0 Attachment
          Below is what I came up with to turn the relevant lines of
          PACKAGES.TXT into a hash. It appears to work, tho I am confident there
          is a (are many!) way(s) to do this more elegantly and -- maybe in one
          step instead of two ...

          Glenn

          ---------------------

          #!/usr/bin/perl

          use warnings;
          use strict;

          my (@packages, @locations, %mirror);

          open FH, "PACKAGES.TXT"
          or die "Can't open file: $!";

          foreach (<FH>) {
          if (/^PACKAGE NA/) {
          /:\s+(.*tgz$)/;
          push @packages, $1;
          } elsif (/^PACKAGE LO/) {
          /:\s+(\.\/.*$)/;
          push @locations, $1;
          }
          }

          close FH;

          for (0..$#packages) {
          $mirror{$packages[$_]} = $locations[$_]
          }
        • merlyn@stonehenge.com
          ... Glenn /: s+(.*tgz$)/; Glenn push @packages, $1; This is really dangerous.... don t do this. Don t use $1 unless you ve
          Message 4 of 11 , Jun 11, 2007
          View Source
          • 0 Attachment
            >>>>> "Glenn" == Glenn Becker <burningc@...> writes:

            Glenn> /:\s+(.*tgz$)/;
            Glenn> push @packages, $1;

            This is really dangerous.... don't do this. Don't use $1 unless you've also
            checked that the match has succeeded. If you're *absolutely* sure it will
            *always* match, at least add "or die" so that your program will abort *when*
            you are wrong (not "if" :).


            --
            Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
            <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
            Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
            See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
          • Glenn Becker
            ... Aww, I shoulda known it was just too easy! I ain t THAT smart! :^T Thanks again, Randal. Back to the proverbial drawing board ... er, xterm. G
            Message 5 of 11 , Jun 12, 2007
            View Source
            • 0 Attachment
              > Glenn> /:\s+(.*tgz$)/;
              > Glenn> push @packages, $1;
              >
              > This is really dangerous.... don't do this. Don't use $1 unless
              > you've also checked that the match has succeeded. If you're
              > *absolutely* sure it will *always* match, at least add "or die" so
              > that your program will abort *when* you are wrong (not "if" :).

              Aww, I shoulda known it was just too easy! I ain't THAT smart! :^T

              Thanks again, Randal. Back to the proverbial drawing board ... er, xterm.

              G
            • Glenn Becker
              ... Would this be a reasonable place to use eval -- say, wrap the two lines above in an eval block? like the following eval { /: s+(.*tgz$)/; push
              Message 6 of 11 , Jun 12, 2007
              View Source
              • 0 Attachment
                > Glenn> /:\s+(.*tgz$)/;
                > Glenn> push @packages, $1;
                >
                > This is really dangerous.... don't do this. Don't use $1 unless
                > you've also checked that the match has succeeded.

                Would this be a reasonable place to use "eval" -- say, wrap the two
                lines above in an "eval" block? like the following

                eval { /:\s+(.*tgz$)/;
                push @packages, $1 };
                print "No match occurred!\n:$@" if $@;

                Is the danger of using $1 that, if the match does not succeed, you are
                accessing a "left-over" $1 from another match?

                Thanks,

                Glenn
              • merlyn@stonehenge.com
                ... Glenn Would this be a reasonable place to use eval -- say, wrap the two Glenn lines above in an eval block? like the following Glenn eval {
                Message 7 of 11 , Jun 12, 2007
                View Source
                • 0 Attachment
                  >>>>> "Glenn" == Glenn Becker <burningc@...> writes:

                  >> This is really dangerous.... don't do this. Don't use $1 unless
                  >> you've also checked that the match has succeeded.

                  Glenn> Would this be a reasonable place to use "eval" -- say, wrap the two
                  Glenn> lines above in an "eval" block? like the following

                  Glenn> eval { /:\s+(.*tgz$)/;
                  Glenn> push @packages, $1 };
                  Glenn> print "No match occurred!\n:$@" if $@;

                  No, it won't throw an exception if it doesn't match. Just follow the
                  guidelines. Use it in a conditional if you *expect* it not to match
                  sometimes. Use it with "or die" if you *never* expect it not to match.

                  Glenn> Is the danger of using $1 that, if the match does not succeed, you are
                  Glenn> accessing a "left-over" $1 from another match?

                  Yes. Or just garbage.... after all, even if $1 was undef on a non-match, your
                  code would be wrong, because it would push undef into @packages, still ruining
                  the algorithm.

                  --
                  Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
                  <merlyn@...> <URL:http://www.stonehenge.com/merlyn/>
                  Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
                  See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
                Your message has been successfully submitted and would be delivered to recipients shortly.