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

Simple Bash Shell Scripts:

Joined
Feb 1, 2010
Messages
178 (0.06/day)
Likes
46
Location
~/
System Name Slackware Linux
Processor yes
Motherboard yes
Cooling currently convection, but considering mineral oil
Memory sometimes fails due to too much beer
Video Card(s) ATI Radeon HD5570 series
Storage IDE
Display(s) 32" LCD TV
Case sometimes
Audio Device(s) huh? what? speak up, I can't hear you
Power Supply yes
Software Slackware running Open-Source software (it doesn't get any better)
#1
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).




Here I start the script and the data starts streaming.




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



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 ;)
 
Joined
Feb 1, 2010
Messages
178 (0.06/day)
Likes
46
Location
~/
System Name Slackware Linux
Processor yes
Motherboard yes
Cooling currently convection, but considering mineral oil
Memory sometimes fails due to too much beer
Video Card(s) ATI Radeon HD5570 series
Storage IDE
Display(s) 32" LCD TV
Case sometimes
Audio Device(s) huh? what? speak up, I can't hear you
Power Supply yes
Software Slackware running Open-Source software (it doesn't get any better)
#2
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...



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



The same API could be used w/ php or any other language
on a website. (I've also used it w/ python).
 
Joined
May 19, 2007
Messages
7,662 (1.96/day)
Likes
536
Location
c:\programs\kitteh.exe
Processor C2Q6600 @ 1.6 GHz
Motherboard Anus PQ5
Cooling ACFPro
Memory GEiL2 x 1 GB PC2 6400
Video Card(s) MSi 4830 (RIP)
Storage Seagate Barracuda 7200.10 320 GB Perpendicular Recording
Display(s) Dell 17'
Case El Cheepo
Audio Device(s) 7.1 Onboard
Power Supply Corsair TX750
Software MCE2K5
#3
me like