1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Simple Bash Shell Scripts:

Discussion in 'Programming & Webmastering' started by regexorcist, Feb 18, 2010.

  1. regexorcist

    regexorcist New Member

    Joined:
    Feb 1, 2010
    Messages:
    178 (0.11/day)
    Thanks Received:
    46
    Location:
    ~/
    This thread is for simple bash shell scripts or code snippets. :)

    Here is a simple script I put together using wget, grep, sed
    and of course a few regular expressions (regex... see my username :laugh: )

    Code:
    #!/bin/bash
    
    var_links=`wget -q -L -O - \
     http://www.phillylinux.org/ \
     | grep -o '<a href="http://[^"]*' \
     | sed -e 's/<a href=\"//' -e 's/\"/\n/'`
    
    wget --spider $var_links
    As you can see, I use wget to retrieve the webpage
    and after parsing, I use wget again to spider each link
    and display the status.

    Here is the page that is retrieved (it has about 30 links or so).

    [​IMG]


    Here I start the script and the data starts streaming.

    [​IMG]


    Here we have scrolled down to the end where the script has finished,
    it took about 20 seconds or so to complete.

    [​IMG]

    The script may be small and simple, but for large website maintenance
    it's very powerful. My regular expressions are set up for absolute links
    but easily modified for relative ones, just as the spider output could be
    grepped down to a single line.

    Have Fun and lets see some of your scripts ;)
  2. regexorcist

    regexorcist New Member

    Joined:
    Feb 1, 2010
    Messages:
    178 (0.11/day)
    Thanks Received:
    46
    Location:
    ~/
    Weather by zipcode:

    I wrote this little script a while back
    and there were a few that found it useful. :D

    I use the google weather API and parse the
    result with a few regular expressions (regex).

    Code:
    #!/bin/bash
    
    echo -n "Enter a valid 5-digit zipcode: "
    read zipcode
    
    var_url="http://www.google.com/ig/api?weather=$zipcode&hl=en"
    
    var_weather_wget=`wget -q $var_url -O -`
    var_weather_xml=`echo "$var_weather_wget" | sed 's/<forecast_conditions>.*//'`
    var_weather=`echo "$var_weather_xml" | sed 's/></>\n</g'`
    
    var_date=`echo "$var_weather" | grep -e '<forecast_date' | \
    sed -e 's/<forecast_date data="//' -e 's/"\/>//'`
    
    var_city=`echo "$var_weather" | grep -e '<city' | \
    sed -e 's/<city data="//' -e 's/"\/>//'`
    
    var_condition=`echo "$var_weather" | grep -e '<condition' | \
    sed -e 's/<condition data="//' -e 's/"\/>//'`
    
    var_temp_f=`echo "$var_weather" | grep -e '<temp_f' | \
    sed -e 's/<temp_f data="//' -e 's/"\/>//'`
    
    var_temp_c=`echo "$var_weather" | grep -e '<temp_c' | \
    sed -e 's/<temp_c data="//' -e 's/"\/>//'`
    
    var_humidity=`echo "$var_weather" | grep -e '<humidity' | \
    sed -e 's/<humidity data="//' -e 's/"\/>//'`
    
    var_wind=`echo "$var_weather" | grep -e '<wind' | \
    sed -e 's/<wind_condition data="//' -e 's/"\/>//'`
    
    echo "Date: $var_date"
    echo "City: $var_city"
    echo "Condition: $var_condition"
    echo "Temp: $var_temp_f Deg. Fahrenheit / $var_temp_c Deg. Celsius"
    echo "$var_humidity"
    echo "$var_wind"
    Here it is in action...

    [​IMG]

    As you can see, I've typed a zipcode and hit <enter>
    see the result...

    [​IMG]

    The same API could be used w/ php or any other language
    on a website. (I've also used it w/ python).
  3. [I.R.A]_FBi

    [I.R.A]_FBi New Member

    Joined:
    May 19, 2007
    Messages:
    7,664 (2.93/day)
    Thanks Received:
    540
    Location:
    c:\programs\kitteh.exe
    me like
    regexorcist says thanks.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page