Unconfigured Ad Widget

Collapse

Special treat for Calguns Linux users

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pokute
    Junior Member
    • Oct 2010
    • 45

    Special treat for Calguns Linux users

    Run Linux? Then you will know what to do with this:

    Code:
    #!/bin/ksh
    #
    # Simple script to be run via cron:
    #
    #   5 * * * * ~/powder-valley.sh
    #
     
    email='me@mail.dom'
     
    in_stock="\nhttp://www.powdervalleyinc.com/primers.shtml\n\n"
     
    command='links -force-html -dump'
     
    cci_query='http://www.powdervalleyinc.com/CCIprimers.shtml'
    cci_primer_rx='CCI(300|350|500|550).+(Yes)'
     
    federal_query='http://www.powdervalleyinc.com/FEDprimers.shtml'
    federal_primer_rx='FED(100|200|150|155).+(Yes)'
     
    remington_query='http://www.powdervalleyinc.com/REMprimers.shtml'
    remington_primer_rx='REM(1.5|2.5|5.5).+(Yes)'
    
    typeset -i in_stock_chars=${#in_stock}
     
    IFS=$'\n'
     
    data=$(eval $command $cci_query)
     
    for line in $data
    do
       if [[ "$line" =~ $cci_primer_rx ]]
       then
          in_stock="${in_stock}\n$line"
       fi
    done
     
    data=$(eval $command $federal_query)
     
    for line in $data
    do
       if [[ "$line" =~ $federal_primer_rx ]]
       then
          in_stock="${in_stock}\n$line"
       fi
    done
    data=$(eval $command $remington_query)
     
    for line in $data
    do
       if [[ "$line" =~ $remington_primer_rx ]]
       then
          in_stock="${in_stock}\n$line"
       fi
    done
    
    if [[ ${#in_stock} -gt $in_stock_chars ]]
    then
       print $in_stock |mail -s 'Powder Valley Primers In Stock!' $email
    fi
    Last edited by pokute; 07-27-2013, 2:55 PM.
  • #2
    spamsucker
    Banned
    • Jun 2012
    • 701

    nice. Any reason you didn't use wget or curl instead of links (lynx, elinks)?

    Comment

    • #3
      pokute
      Junior Member
      • Oct 2010
      • 45

      Originally posted by spamsucker
      nice. Any reason you didn't use wget or curl instead of links (lynx, elinks)?
      lynx or w3m would also work. The output from the "browsers" is both easier to parse and more robust than the "scrapers" because even if the site changes the way it generates the tables, or changes the table appearance, the parsing doesn't break because the table rendering code in links, lynx, and w3m reduces the table to a sane, basic form.

      Anyway, I like links. I even use it to browse!

      BTW, I formatted the code the way I did, without using an outer loop, to make it trivially easy for folks to extend the script by just cutting and pasting.
      Last edited by pokute; 07-27-2013, 11:16 AM.

      Comment

      • #4
        RM14
        Junior Member
        • Feb 2013
        • 84

        eval? Bad idea. Use backticks to execute commands.

        data=`$command $cci_query`
        "Every absurdity has a champion to defend it." -Oliver Goldsmith

        Comment

        • #5
          spamsucker
          Banned
          • Jun 2012
          • 701

          Here... I improved it (at least from my point of view, it works on my mac now).

          #!/bin/bash
          mailaddr="you@mail.dom"


          for BRAND in CCI CHE FED REM TUL WIN
          do
          ########### BEGIN Pick Your OS ##########
          # Comment out the appropriate line
          # MAC OS
          LYNXPATH="/Applications/Lynxlet.app/Contents/Resources/lynx/bin/lynx"
          INSTOCK=`${LYNXPATH} -force-html -dump http://www.powdervalleyinc.com/${BRAND}primers.shtml | grep ${BRAND} | grep Yes`

          # Linux and many Unix's
          #LYNXPATH=`which lynx`
          #INSTOCK=`${LYNXPATH} -force-html -dump http://www.powdervalleyinc.com/${BRAND}primers.shtml | grep ${BRAND} | grep Yes`
          ########### END Pick Your OS ##########
          if [ "${INSTOCK}" != '' ]
          then
          echo "${INSTOCK}" | mail -s "Primers in stock at Powder Valley" $mailaddr
          echo "${BRAND}\n ${INSTOCK}"
          fi
          done
          What struck me about it was that you had so many variables defined needlessly. I figure a for loop could recurse through the brands with a single bit of instructions and it's easier for me to read this way. I've added Tula, Cheddite and Winchester brands to the list. I think it works better now.
          Last edited by spamsucker; 07-27-2013, 11:34 AM.

          Comment

          • #6
            pokute
            Junior Member
            • Oct 2010
            • 45

            Originally posted by RM14
            eval? Bad idea. Use backticks to execute commands.

            data=`$command $cci_query`
            eval is only bad if commandline or cgi arguments are passed to it. Which they aren't here.
            Shells have an eval command for a good reason, not just because they want to trip you up.
            Last edited by pokute; 07-27-2013, 3:00 PM.

            Comment

            • #7
              pokute
              Junior Member
              • Oct 2010
              • 45

              Originally posted by spamsucker
              Here... I improved it (at least from my point of view, it works on my mac now).
              I don't want email for every primer that's made, only for the ones I might need.

              Also, note:

              Code:
              OS=`uname`
              
              Linux () {
               echo Linux!
              }
              
              SunOS () {
               echo Solaris!
              }
              
              Darwin () {
               echo OSX!
              }
              
              ${OS};
              OSX ships ksh now, so you can stop calling grep (and sed, and awk).
              Last edited by pokute; 07-27-2013, 3:03 PM.

              Comment

              • #8
                vtrigger77
                Senior Member
                • Aug 2012
                • 1450

                Whats a linux? Lol

                Comment

                • #9
                  DMorris2321
                  Senior Member
                  • Jul 2011
                  • 1158

                  I would of floppy disked back up dos for improved Ram

                  Comment

                  • #10
                    rjpsb1
                    Member
                    • Dec 2009
                    • 446

                    Also, use $(expression) instead of backticks. As suggested to me by the author of bash.
                    Last edited by retired; 07-27-2013, 5:18 PM.
                    sigpic

                    Comment

                    • #11
                      pokute
                      Junior Member
                      • Oct 2010
                      • 45

                      Originally posted by rjpsb1
                      Also, use $(expression) instead of backticks. As suggested to me by the author of bash.
                      You see any backticks in the script? There aren't any.

                      Geez, I post something useful, and I get mostly flack for it... Fine, just keep sittin' there in your lawn chair at Walmart.

                      Comment

                      • #12
                        locosway
                        I need a LIFE!!
                        • Jun 2009
                        • 11346

                        Originally posted by pokute
                        You see any backticks in the script? There aren't any.

                        Geez, I post something useful, and I get mostly flack for it... Fine, just keep sittin' there in your lawn chair at Walmart.
                        Why didn't you write a web interface and allow CGN members to signup for which alerts they want and host everything for free for us???

                        Stop being so selfish!

                        OCSD Approved CCW Instructor
                        NRA Certified Instructor
                        CA DOJ Certified Instructor
                        Glock Certified Armorer

                        Comment

                        • #13
                          rjpsb1
                          Member
                          • Dec 2009
                          • 446

                          Originally posted by pokute
                          You see any backticks in the script? There aren't any.

                          Geez, I post something useful, and I get mostly flack for it... Fine, just keep sittin' there in your lawn chair at Walmart.
                          Post #4 said use backticks. I didn't say you did. But thanks for the hostility.
                          sigpic

                          Comment

                          • #14
                            the86d
                            Calguns Addict
                            • Jul 2011
                            • 9587

                            Originally posted by pokute
                            Run Linux? Then you will know what to do with this:

                            Code:
                            #!/bin/ksh
                            #
                            # Simple script to be run via cron:
                            #
                            #   5 * * * * ~/powder-valley.sh
                            #
                             
                            email='me@mail.dom'
                             
                            in_stock="\nhttp://www.powdervalleyinc.com/primers.shtml\n\n"
                             
                            command='links -force-html -dump'
                             
                            cci_query='http://www.powdervalleyinc.com/CCIprimers.shtml'
                            cci_primer_rx='CCI(300|350|500|550).+(Yes)'
                             
                            federal_query='http://www.powdervalleyinc.com/FEDprimers.shtml'
                            federal_primer_rx='FED(100|200|150|155).+(Yes)'
                             
                            remington_query='http://www.powdervalleyinc.com/REMprimers.shtml'
                            remington_primer_rx='REM(1.5|2.5|5.5).+(Yes)'
                            
                            typeset -i in_stock_chars=${#in_stock}
                             
                            IFS=$'\n'
                             
                            data=$(eval $command $cci_query)
                             
                            for line in $data
                            do
                               if [[ "$line" =~ $cci_primer_rx ]]
                               then
                                  in_stock="${in_stock}\n$line"
                               fi
                            done
                             
                            data=$(eval $command $federal_query)
                             
                            for line in $data
                            do
                               if [[ "$line" =~ $federal_primer_rx ]]
                               then
                                  in_stock="${in_stock}\n$line"
                               fi
                            done
                            data=$(eval $command $remington_query)
                             
                            for line in $data
                            do
                               if [[ "$line" =~ $remington_primer_rx ]]
                               then
                                  in_stock="${in_stock}\n$line"
                               fi
                            done
                            
                            if [[ ${#in_stock} -gt $in_stock_chars ]]
                            then
                               print $in_stock |mail -s 'Powder Valley Primers In Stock!' $email
                            fi
                            I'll give this a go when I get on work rig...
                            Originally posted by DMorris2321
                            I would of floppy disked back up dos for improved Ram
                            Make sure to signal when required while driving.

                            Comment

                            • #15
                              SoCal326
                              Senior Member
                              • Oct 2011
                              • 1098

                              Thanks for the script.

                              I wouldn't take it personally that people are commenting on your code. It's pretty common at my company to have someone look over code and offer suggestions to improve it. You will see this same behavior on any community maintained project. It's just how code gets better.

                              I'm not saying any changes were needed, just don't take it personally.

                              Comment

                              Working...
                              UA-8071174-1