Archive for May, 2008

Hello Script for bash

Monday, May 12th, 2008

Hello Script series for Bash. ‘Hello Script’ is a file that contains the most commonly used elements of a programming language so that it can be used as a cheat sheet when working with that language.

bash

bash is the most commonly used shell in Linux. That makes the bash scripting language the most popular shell scripting language. OK, maybe after batch. But then again, bash is much more powerful than batch(DOS scripting language). If want to learn bash, I will recommend this tutorial - Advanced Bash Scripting

Officially, I hate bash. I use perl or other similar high level language to create shell script. I use bash only for the simplest scripts. But even I admit that bash has its uses. So, here is the hello script for bash…

Hello Script


#!/usr/sh

# Printing(IO)
echo "Hello World"

# Variables, concatenation
name='Binny'
year=2008
echo "Hello, " $name " - welcome to " $year

#If,else conditions
if [ $year -gt 2008 ]; then
	echo “Welcome to the future - yes, we have flying cars!”

elif [ $year -lt 2008 ]; then
	echo “The past - please don’t change anything. Don’t step on any butterflies. And for the sake of all that’s good and holy, stay away from your parents!”

else
	echo “Anything wrong with your time machine? You have not gone anywhere, kiddo.”
fi

# If you are using anything after this, consider using a high level language.
# For loop
for i in 1 2 3
do
	echo $i “) Hi there!”
done

#Numerical Array, foreach
rules[0]=”Do no harm”
rules[1]=”Obey”
rules[2]=”Continue Living”

for ((i=0; i < 3; i++))
do
	echo “Rule” `expr $i + 1` “:” ${rules[$i]}
done

#A While Demo
keys=(hello foo lorem)

i=0
while [ $i -lt 3 ]
do
	echo ${keys[$i]}
	i=`expr $i + 1`
done

# Function, argument, return, call
hello () {
	myname=$1  #First argument.
	echo “Hello” $myname
}
hello “Binny”

# File IO
# File reading
contents=`cat Hello.sh` #For some reason, I’m losing all the \n’s in the file.
echo “Hello has `echo $contents|wc -m` chars” # Or wc -m Hello.sh

# Writing to a file
echo “Hello World from shell script” > /tmp/hello.txt

# Command Executing
ls

# Regular Expressions
string=”Hello World”
evil=`echo $string | grep ‘^Hell’`
if [ "$evil" != "" ]; then
	echo “Yup - its evil”
fi
echo “Hello World” | sed -e ’s/l//g’ #Will return Heo Word

# http://tldp.org/LDP/abs/html/

And, by the way, this will work only in Linux - or if you installed cygwin in your Windows system. Use sh <File_Name> to execute the above code.

URL Lister - My First Firefox Plugin

Wednesday, May 7th, 2008

URL Lister Logo

I just released my first firefox plugin - URL Lister. It shows the URLs of all the open tabs in a textarea so that they can be copied easily.

Download/Install

Install URL Lister

If you have installed it, consider rating it or reviewing it at Firefox plugins sandbox. Go to the public page for URL Lister and write a review for the application. I need reviews to promote it to the main extensions page - sandbox plugins are for registered users only.

Usage

Lets say you have these four tabs open…

Right click any tab and click on the ‘URL Lister…’ to open up the main dialog. You can also use ‘Tools > URL Lister’. You will find the URLs of all the open tabs there.

URL Lister Screenshot

There is a drop down menu at the bottom - it has these three options…

  • Plain Text
  • HTML Anchors
  • Linked List

Open URLs

If you have a list of URLs and want to open them all, all you have to do is copy those URLs into this dialog and press OK - this will open up all the given URLs.

And my thanks goes to…

URL Lister is sort of a response to this post by Matt Cutts. I want to thank him for the idea.

There is another plugin with a similar function - Tab URL Copier. I lifted some code from that plugin when creating URL Lister. Thanks.

Hello Script for Ruby

Thursday, May 1st, 2008

Hello Script is a file that contains the most commonly used elements of a programming language so that it can be used as a cheatsheet when working with that language. This Hello Script for Ruby is the sixth post in this series.

I have some experience with Ruby. I will not call myself an expert - but I am comfortable with Ruby. I like ruby. I even have written a few applications in it.

Hello Code


#!/usr/bin/ruby

print "Hello World\n"

name = "Binny"
year = 2008
print "Hello, " + name + " - welcome to " + year.to_s + "\n"

if (year > 2008) then
	print "Welcome to the future - yes, we have flying cars!"
elsif (year < 2008) then
	print "The past - please don't change anything. Don't step on any butterflies. And for the sake of all that's good and holy, stay away from your parents!"
else
	print "Anything wrong with your time machine? You have not gone anywhere, kiddo."
end
print "\n\n"

# For loop like structure
0.upto(3) { |i|
	print i.to_s + ") Hi there!\n"
}
print "\n"

# Numerical array
rules = ['Do no harm', 'Obey', 'Continue Living']
i = 0
while i<rules.length do
	print “Rule ” + (i+1).to_s + “: ” + rules[i] + “\n”
	i = i+1
end
print “\n”

# Associated arrays
associated = {
	‘hello’	=>	‘world’,
	‘foo’	=>	‘bar’,
	‘lorem’	=>	‘ipsum’
}
associated.each { |key,value|
	print key + ” : ” + value + “\n”
}
print “\n”

# Using Join and Split
csv_values = “hello,world,how,are,you\n”.split(”,”)
print csv_values.join(”:”)

# Function, argument, return, call
def hello(name)
	return “Hello ” + name
end
hello_string = hello(”Binny”)
print “Function call returned ‘” + hello_string + “‘\n\n”

# One for the OOP fanboys - Class, members, object and stuff.
class Movie
	public
	@name = ”
	@rating = 0

	def initialize(name)
		@name = name
		self.rateMovie()
	end

	def rateMovie()
		@rating = (@name.length % 10) + 1 #IMDBs rating algorithm. True story!
	end

	def printMovieDetails()
		print “Movie : ” + @name + “\n”
		print “Rating : ” + ‘*’ * @rating + “(” + @rating.to_s + “)\n\n”
	end
end
# Create the object
ncfom = Movie.new(”New Country for Old Men”) #It’s a sequel!
ncfom.printMovieDetails()

# File IO
# File reading, easy method…
file_in = File.new(’Hello.rb’, ‘r’)
contents = file_in.read
file_in.close
print “Hello has ” + contents.length.to_s + ” chars\n”

# Writing to a file
file_out = File.new(’/tmp/hello.txt’, ‘w’)
file_out.print “Hello World from Ruby.”
file_out.close

# Command Executing
print `ls` #Execute the command ‘ls’ and print its output
print “\n”

# Regular Expressions
string = “Hello World”
print “Yup - its evil\n” if(/^Hell/.match(string))
print string.gsub(/l([^l])/, ‘\1′) #Remove an ‘l’ from both words. Should print ‘Helo Word’ - The second arg must be in single quotes

print “\n”

# Some special/only-in-ruby stuff…
# Using a library
require “fileutils”

#Using yield/code blocks…
def doXTimes(i)
	0.upto(i) {|count|
		yield count+1
	}
end
doXTimes(5) {|count|
	print count.to_s + “th Time\n”
}

Subscribe to Feed