Software Projects

Shell Script Functions for .bashrc

Posted in Uncategorized by rmt on June 2, 2011

These are mostly untested.

# Ensure Unix line endings
function fixlines()
{
/usr/bin/perl -pi~ -e 's/\r\n?/\n/g' "$@" ;
}

# Remove tabs from a file
function fix-tabs() 
{
expand -t 4 "$1" > "$1.expanded"; mv -f "$1.expanded" "$1";
}

# Back up a file with a date-time stamp
function bu()
{
cp $1 ${1}-`date +%Y%m%d%H%M`.backup ;
}

# colored status of running services
function services()
{
printf "$(service --status-all 2>&1|sed -e 's/\[ + \]/\\E\[42m\[ + \]\\E\[0m/g' -e 's/\[ - \]/\\E\[41m\[ - \]\\E\[0m/g' -e 's/\[ ? \]/\\E\[43m\[ ? \]\\E\[0m/g')\n";
}

# Download a web page and show info on what took time
function debug_http()
{
/usr/bin/curl $@ -o /dev/null -w "dns: %{time_namelookup} connect: %{time_connect} pretransfer: %{time_pretransfer} starttransfer: %{time_starttransfer} total: %{time_total}\n" ;
}

# Get just the HTTP headers from a web page (and its redirects)
function http_headers()
{
/usr/bin/curl -I -L $@ ;
}

# Recursively get images from a web page
function http_images()
{
wget -r -l 0 -U Mozilla -t 1 -nd -D $@ -A jpg,jpeg,gif,png "http://$@" -e robots=off
}

# do a svn update and show the log messages since the last update
function svn_uplog() {
  local old_revision=`svn_rev $@`
  local first_update=$((${old_revision} + 1))
  svn up -q $@
  if [ $(svn_rev $@) -gt ${old_revision} ]
  then
    svn log -v -rHEAD:${first_update} $@
  else
    echo "No Changes."
  fi
}

# Concatenate PDF files. Usage: cat_pdfs -o combined.pdf file1.pdf file2.pdf file3.pdf
function cat_pdfs()
{
python '/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/join.py' "$@" ;
}

# myip - finds your current IP if your connected to the internet
function myip()
{
lynx -dump -hiddenlinks=ignore -nolist http://checkip.dyndns.org:8245/ | awk '{ print $4 }' | sed '/^$/d; s/^[ ]*//g; s/[ ]*$//g'
}

# Google spell checker
function spellcheck()
{
typeset y=$@;curl -sd "<spellrequest><text>$y</text></spellrequest>" https://www.google.com/tbproxy/spell|sed -n '/s="[0-9]"/{s/<[^>]*>/ /g;s/\t/ /g;s/ *\(.*\)/Suggestions: \1\n/g;p}'|tee >(grep -Eq '.*'||echo -e "OK");
}

# Query Wikipedia via console over DNS
function mwiki()
{
blah=`echo $@ | sed -e 's/ /_/g'`; dig +short txt $blah.wp.dg.cx;
}

# view a twitter stream from terminal
function grabtweets()
{
curl -s "twitter.com/$1" | sed -ne '/entry-content/{s/<[^>]*>//g;s/^[ \t]*//;p}';
}

# update twitter via curl as function
function tweet()
{
curl -u "$1" -d status="$2" "http://twitter.com/statuses/update.xml";
}

# Google search
function google()
{
chromium-browser "http://www.google.com/search?&num=100&q=${@}" &
}

# Google text-to-speech in mp3 format
function say()
{
mplayer -user-agent Mozilla "http://translate.google.com/translate_tts?tl=en&q=$(echo $* | sed 's#\ #\+#g')" > /dev/null 2>&1 ;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s