Tag Archives: Scripts

Calculate Your GPA Using this Bash Script

This Bash script is used to calculate your Grade Point Average (GPA) on the command line. Usage might not be intuitive. Please see the usage function or just run the script without passing it any arguments.

The gval function should be edited to reflect your own region or university. It has been written and tested on Bash 3.2.48.

#!/bin/sh
#
# Bash GPA calculator
#
# Isam | r0cketjump@yahoo.com | biodegradablegeek.com
# 05/21/2009 - Just another 4 AM project

function usage {
  echo -e "\nBASH GPA Calculator"
  echo
  echo -e "\tAccepts an even # of arguments in the form of C G C G C G ..."
  echo -e "\t (C = number of credits, G = grade for the course)"
  echo
  echo -e "\tExample: You got a B+ in a 4 credit course, "
  echo -e "\t         an A in a 3 credit course, etc.."
  echo
  echo -e "\tUSAGE: $0 4 B+ 3 A 3 F 3 B-"
  echo
  echo "Acceptable grades are A B C D F WU (eq to F)"
  echo
}

function calc {
  echo `echo "scale=3; $1" | bc`
}

function gval {
  grade=`echo "$1" | tr [a-z] [A-Z]`
  case $grade in
    A+ ) echo '4.3';;
    A ) echo '4';;
    A- ) echo '3.7';;

    B+ ) echo '3.3';;
    B ) echo '3.00';;
    B- ) echo '2.7';;

    C+ ) echo '2.3';;
    C ) echo '2.0';;
    C- ) echo '1.7';;

    D+ ) echo '1.3';;
    D ) echo '1.0';;
    D- ) echo '0.7';;

    F ) echo '0';;
    WF ) echo '0';;
    WU ) echo '0';;
  esac
}

# check # of arguments. is it even?
let MOD=$#%2
if [ ! $MOD -eq 0 ]; then
  usage
  exit
elif [ $# -eq 0 ]; then
  usage
  exit
fi

args=($@)
n=${#args[@]}

points=0
credits=0

for ((i=0;i<$n-1;i+=2)); do
  k=${i}

  creds=${args[$k]}
  cgrade=${args[$k+1]}

  # convert cgrade (C-) to a number
  grade=`gval $cgrade`
  pts=`calc $grade*$creds`

  echo "$creds * $cgrade ($grade) = $pts"

  points=`calc $points+$pts`
  credits=`calc $credits+$creds`
done

gpa=`calc $points/$credits`
echo "------------"
echo "Total points  = $points"
echo "Total credits = $credits"
echo "------------"
echo "** GPA (pts/crd) = $gpa"
echo "------------"

(Script uses bc as the calculator. Change that in the calc function if you need to.)

I’ll never get used to Bash’s ugly ass syntax. … esac?

Bash Script to Force an Empty Git Push

Sometimes, like when you’re testing hooks or trying to create synced remote and local repos, you’ll find yourself touching empty files just to get a git push going. This script automates this task by creating a unique temporary file, committing it, pushing, and then removing the file.

#!/bin/sh
TMP=tmp-`date +'%m%s'`
touch $TMP
git add $TMP
git commit $TMP -m '(forced push)'
git push
git rm $TMP
</pre>
Usage, assuming you named it git-force and made it executable (chmod)
<pre lang="bash">cd git-repo/
./git-force

I place this in ~/bin/ which is in my $PATH. You might want to if you use this a lot.

Quick BASH Script to Dump & Compress a MySQL Database

A quick script I whipped up to dump my MySQL database.
Usage: sh backthatsqlup.sh

(be warned that it dumps ALL databases. This can get huge uncompressed)


#!/bin/sh
# Isam (Biodegradablegeek.com) public domain 12/28/2008
# Basic BASH script to dump and compress a MySQL dump

out=sequel_`date +'%m%d%Y_%M%S'`.sql
dest=/bx/

function e {
  echo -e "n** $1"
}

e "Dumping SQL file ($out). May take awhile..."
#echo "oh snap" &gt; $out
sudo mysqldump -u root -p --all-databases &gt; $out
if [ $? -ne 0 ]; then
  e "MySQL dump failed. Check that server is up and your username/pass"
  exit 7
fi

e "Uncompressed SQL file size"
du -hs $out

e "Compressing SQL file"
gz=$out.tar.gz
tar -zvvcf $gz $out
rt=$?

if [ $rt -ne 0 ]; then
  e "tar failed (error=$rt). Will NOT remove uncompressed SQL file"
else
  e "Removing uncompressed SQL file"
  rm -f $out
  out=$gz

  e "Compressed SQL file size"
  du -hs $out
fi

e "Moving shit to '$dest'"
sudo mv $out $dest

download BackThatSqlUp.sh

Script to Quickly Setup WebApp Environment and Domain

Just sharing a script I wrote to quickly deploy WordPress (and eventually a few other webapps) sites, which somebody might find useful. This uses Linode‘s API* to add the domain name to the DNS server along with some subdomains. If you’re using another server, (Slicehost, your own, etc), you can alter the dns class to use that API, or just ignore the DNS stuff completely; Its optional.

This will be updated periodically as I refactor and add support for more apps (notably Joomla and Clipshare – though this would violate their terms unless you have the unlimited license). This was written primarily because I couldn’t stand setting up another vhost and WordPress installation. There are plenty of existing deployers but I plan on adding very specific features and tweaking this for in-house work. I also wanted to try Rio (Ruby-IO). GPL license. Go nuts.

* As of 10/11, the apicore.rb file on the site has some syntactic errors in the domainResourceSave method. I sent an email out to the author about it. Problems aren’t major. You can get my apicore.rb here.

This won’t run unless you create the appropriate folder structure in /etc/mksite/. I’ll get going on this in a bit. See the code below:

Continue reading Script to Quickly Setup WebApp Environment and Domain

How to POST Form Data Using Ruby

POSTing data on web forms is essential for writing tools and services that interact with resources already available on the web. You can grab information from your Gmail account, add a new thread to a forum from your own app, etc.

The following is a brief example on how this can be done in Ruby using Net::HTTPand this POST form example.

Looking at the source (interlacken.com/webdbdev/ch05/formpost.asp):

<form method="POST" action="formpost.asp">
<p><input type="text" name="box1″ size="20″ value="">
<input type="submit" value="Submit" name="button1″></p>
</form>

We see two attributes are sent to the formpost.asp script when the user hits the submit button: A textbox named box1 and the value of the submit button, named Submit. If this form used a GET method, we would just fetch the URL postfixed with (for example) ?box1=our+text+here. Fortunately, Ruby’s Net::HTTP makes posting data just as easy.

The Ruby code:

#!/usr/bin/ruby

require "uri"
require "net/http"

params = {'box1′ => 'Nothing is less important than which fork you use. Etiquette is the science of living. It embraces everything. It is ethics. It is honor. -Emily Post',
'button1′ => 'Submit'
}
x = Net::HTTP.post_form(URI.parse('http://www.interlacken.com/webdbdev/ch05/formpost.asp'), params)
puts x.body

# Uncomment this if you want output in a file
# File.open('out.htm', 'w') { |f| f.write x.body }

Sending the value of button1 is optional in this case, but sometimes this value is checked in the server side script. One example is when the coder wants to find out if the form has been submitted – as opposed to it being the user’s first visit to the form – without creating a hidden attribute to send along w/ the other form fields. Besides, there’s no harm in sending a few more bytes.

If you’re curious about URI.parse, it simply makes the URI easier to work with by separating and classifying each of its attributes, effectively letting the methods in Net::HTTP do their sole job only, instead of having to analyze and parse the URL. More info on this in the Ruby doc.

Assuming no errors, running this example (ruby postpost or chmod a+x postpost.rb; ./postpost.rb) yields:

<form method="POST" action="formpost.asp">
<p><input type="text" name="box1″ size="20″ value="NOTHING IS LESS
IMPORTANT THAN WHICH FORK YOU USE. ETIQUETTE IS THE
SCIENCE OF LIVING. IT EMBRACES EVERYTHING. IT IS ETHICS.
IT IS HONOR. -EMILY POST">
<input type="submit" value="Submit" name="button1″></p>
</form>

In practice, you might want to use a more specialized library to handle what you’re doing. Be sure to check out Mechanize and Rest-client.