Archives for April, 2010

14
Apr

Easy Way to Bring Binaires Down to Size

In an embedded environment, the amount of space your binaries (kernel modules, programs, etc) take up is important to consider. A very easy way to remove a lot of bulk from them is to use the objcopy command:

objcopy --strip-unneeded filename
13
Apr

Neat Find Trick with Wildcards

Blasphemy! Originally, I had titled this post “Neat Find Trick with Regular Expressions”, but the use of brackets is not necessarily a regular expression. Turns out, it’s actually one type of wildcard character. So, the post title has been changed to “Neat Find Trick with Wildcards” to reflect this new knowledge (thanks Jim). Okay, onward…

This is one of those “how did I miss that while learning Linux” tips. You can use regular expressions (regex) wildcards in the terminal to simplify your life. For example, the find command can take advantage of this sorta thing. Say that you’re looking for all files ending with .c or .h. Instead of using the ‘find’ command twice, you can simply use “*.[ch]” to search for them. The brackets tell find to search for any single character within them. You can learn more about regular expressions here, wildcards here, but for now, let’s see this in action:

find . -type f -name "*.[ch]"

As expected, this will find all files that end in a .c or .h extension. Have fun!

12
Apr

Generate Random Quotes with a Shell Script [Part 3/3: Integrate with Evolution Email]

This is the last part of the “Generate Random Quotes with a Shell Script” series of posts where we looked at 1) setting up user defined/specified quotes and 2) getting quotes from an online source such as quotationspage.com. For this post, I’ll walk you step-by-step on how to integrate an online source of quotes into a signature in the Evolution email client. Let’s get started…
[Read more →]

9
Apr

Generate Random Quotes with a Shell Script [Part 2/3: From Quotationspage.com Online]

This post expands upon “Generate Random Quotes with Shell Scripts [Part 1/3: Your Own Specified Quotes]” where we took a look at how to generate a user defined list of quotes and then randomly select and display one of them (user_defined). Here in part 2, we’ll take a look at how to expand on this script to add two methods of random quote generation, both of which use the service provided by http://www.quotationspage.com (which, by the way, works in both Mac OSX and Linux). One of the methods we’ll call online_1mqotd which will obtain a quote from this javascript source, and the other we’ll call online_rand which will search randomly through the thousands of quote pages at quotationspage.com. I’ll let the script do the talking and explaining, so without further ado, here it is (usage instructions follow):

#!/bin/sh

if [ $# -lt 1 -o $1 == "help" ]
then
	echo "Usage: $0 <user_defined|online_1mqotd|online_rand>"
	exit
else
	source=$1
fi

## Grab a quote from a user defined list
if [ $source == "user_defined" ]
then

num_quotes=10
rand=$[ ( $RANDOM % $num_quotes ) + 1 ]

case $rand in
	1) quote="Some things Man was never meant to know. For everything else, there's Google.";;
	2) quote="The Linux philosophy is 'Laugh in the face of danger. Oops. Wrong One. 'Do it yourself'. Yes, that's it.  -- Linus Torvalds";;
	3) quote="... one of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -- Robert Firth";;
	4) quote="There are 10 kinds of people in the world, those that understand trinary, those that don't, and those that confuse it with binary.";;
	5) quote="My software never has bugs. It just develops random features.";;
	6) quote="The only problem with troubleshooting is that sometimes trouble shoots back.";;
	7) quote="If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.";;
 8) quote="You know you're a geek when... You try to shoo a fly away from the monitor with your cursor. That just happened to me. It was scary.";;
	9) quote="We all know Linux is great... it does infinite loops in 5 seconds. -- Linus Torvalds about the superiority of Linux on the Amterdam Linux Symposium";;
	10) quote="By golly, I'm beginning to think Linux really *is* the best thing since sliced bread.  -- Vance Petree, Virginia Power";;
esac

	echo "Random Quote: $quote" | fmt -80

## Grab and parse the "motivational quote of the day" from quotationspage.com.
# TODO: Parse the author as well.  Maybe use same method as online_rand.
elif [ $source == "online_1mqotd" ]
then
	quote=$(wget -q -O - http://www.quotationspage.com/data/1mqotd.js | grep "tqpQuote" |
	        sed "s/document.writeln('<dt class=\\'tqpQuote\\'>/"/g; s/</dt>');/"/g")

	echo "Random Quote: $quote" | fmt -80

## Grab and parse a random quote from quotationspage.com.
# TODO: Only print author mini-bio if it exists, as opposed to all the time.
elif [ $source == "online_rand" ]
then
	getquote(){
		num_online_quotes=9999
		rand_online=$[ ( $RANDOM % $num_online_quotes ) + 1 ]
		# In HTML source, <dt> is unique to quote and </dd> is unique to author
		# The field separators (FS) are either < or > which is [<>] in posix
		# TODO: Wanted to print as "quote -- author n (mini-bio)", but MacOSX 'fmt' is blah
		quote=$(wget -q -O - "http://www.quotationspage.com/quote/$rand_online.html" |
		 grep -e "<dt>" -e "</dd>" | awk -F'[<>]' '{
			if($2 ~ /dt/)
			{ print $3 }
			else if($4 ~ /b/)
			{ print "-- " $7 "  n(" $19 ")"}
		 }')
	}
		i=1
		# 5 Attempts at obtaining a quote.  Silently fail.
		while [ $i -lt 5 ]
		do
			getquote
			echo "$quote" | grep ERROR > /dev/null
			if [ $? -eq 0 ]
			then
				getquote
				i=`expr $i + 1`
			else
				echo "Random Quote: $quote" | fmt -80
				exit
			fi
		done
fi

You can see the script in action here:

$ ./random_quote
Usage: ./random_quote <user_defined|online_1mqotd|online_rand>

$ ./random_quote user_defined
Random Quote: If you give someone a program, you will frustrate them for a day;
if you teach them how to program, you will frustrate them for a lifetime.

$ ./random_quote online_1mqotd
Random Quote: "Patience serves as a protection against wrongs as clothes do
against cold. For if you put on more clothes as the cold increases, it will have
no power to hurt you. So in like manner you must grow in patience when you meet
with great wrongs, and they will then be powerless to vex your mind."

$ ./random_quote online_rand
Random Quote: Between two evils, I always pick the one I never tried before.  --
Mae West (US movie actress  (1892 - 1980))

In part 3 of 3 coming up Monday, we’ll conclude by taking a look at a clever way of utilizing this script in the Evolution email client. This way, your email recipients will always be pleasantly surprised by a random quote in your signature every time you respond.

8
Apr

Generate Random Quotes with a Shell Script [Part 1/3: User Defined/Specified Quotes]

Here’s a method for generating randomized quotes in a shell script based on a list of quotes that has been populated by you. Most of what you’ll see is pretty self-explanatory and easy to follow. I’ve scattered comments into the source as well for even more verbosity.

#!/bin/sh

# Keep this updated when you add or take away quotes on the case list
num_quotes=10

# Generate a random quote number variable, 'rand'
rand=$[ ( $RANDOM % $num_quotes ) + 1 ]
case $rand in  #BEGIN CASE
        1) quote="Some things Man was never meant to know. For everything else, there's Google.";;
        2) quote="The Linux philosophy is 'Laugh in the face of danger. Oops. Wrong One. 'Do it yourself'. Yes, that's it.  -- Linus Torvalds";;
        3) quote="... one of the main causes of the fall of the Roman Empire was that, lacking zero, they had no way to indicate successful termination of their C programs. -- Robert Firth";;
        4) quote="There are 10 kinds of people in the world, those that understand trinary, those that don't, and those that confuse it with binary.";;
        5) quote="My software never has bugs. It just develops random features.";;
        6) quote="The only problem with troubleshooting is that sometimes trouble shoots back.";;
        7) quote="If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.";;
        8) quote="You know you're a geek when... You try to shoo a fly away from the monitor with your cursor. That just happened to me. It was scary.";;
        9) quote="We all know Linux is great... it does infinite loops in 5 seconds. - Linus Torvalds about the superiority of Linux on the Amterdam Linux Symposium";;
        10) quote="By golly, I'm beginning to think Linux really *is* the best thing since sliced bread.  -- Vance Petree, Virginia Power";;
esac  # END CASE

# Display the random quote from case statement, and format it to line wrap at 80 characters
echo "Random Quote: $quote" | fmt -80

To run this script, simply create a new file, copy/paste the contents below, make the script executable, and then run it. Something like this ought to get you started:

vi random_quote
# Copy/paste the script contents to new file
chmod +x random_quote
./random_quote

This was part 1 of 3 in a series of generating random quotes in a shell script. Tomorrow, I’ll discuss part 2 of 3 which will be how to generate a random quote from The Quotations Page. The final part will be on example usage of this script where I will show you how to use it to insert random quotes as a signature in the Evolution email client. See you then.

7
Apr

Convert Small Array to Single Variable and Back Again

Here is a method that will allow you to essentially merge elements of an array into a single variable and then back again in c.

unsigned char mac_addr[6];
long long int x;

x = mac_addr[0] << 40;
x |= mac_addr[1] << 32;
x |= mac_addr[2] << 24;
x |= mac_addr[3] << 16;
x |= mac_addr[4] << 8;
x |= mac_addr[5];

mac_addr[0] = x >> 40;
mac_addr[1] = x >> 32;
mac_addr[2] = x >> 24;
mac_addr[3] = x >> 16;
mac_addr[4] = x >> 8;
mac_addr[5] = x;

You could probably take it a step further and throw it into a loop of sorts. If you have another solution, feel free to share it in the comments below!

6
Apr

Sharing a Variable Between Two Sources in Linux Kernel

Sharing a variable between two sources in the kernel tree is done pretty simply with the EXPORT_SYMBOL function. For example, to share the variable mac_address of source1.c with source2.c you would use the following.

source1.c

unsigned char mac_address[6];
EXPORT_SYMBOL(mac_address);

source2.c

extern unsigned char mac_address[6];
5
Apr

Update CVS Recursively

Here’s a trick that works with adding files recursively to CVS. Thanks to Torsten Curdt of vafor.org for this one:
http://vafer.org/blog/20050107005746

Basically, since there’s no way to tell the cvs add command to recursively search and add files to CVS, you must utilize other utilities like find,grep, and xargs. For the sake of convenience, I’m going to basically quote directly from the above website (to save on clicks):

find . -type d -print | grep -v CVS | xargs cvs add
find . -type f -print | grep -v CVS | xargs cvs add
cvs commit *

In summary, the find command is used to find the directories in the current working directory (.) and print them (-type d -print). The results are then piped into grep to remove any references to ‘CVS’ (-v CVS). These results are then piped into xargs where is executed for each result. This process is repeated for the files and then the changes are then committed to CVS (cvs commit *).

There are other recommended methods as well in the comments, like this one as well as this one (a MacOSX trick). I found that the already mentioned step works well, so that’s the one I’m going to suggest.

2
Apr

Display Progress and Speed When Using ‘cp’ Command

I ran across an interesting method for getting ‘cp’ to show progress and speeds as well. Take a look at the page here:
http://chris-lamb.co.uk/2008/01/24/can-you-get-cp-to-give-a-progress-bar-like-wget/

There is some controversy to it since there is so much overhead (through strace, awk, etc), but the thought is very cool. I wonder if there’s a better way of doing this… the pv command maybe? Or a more in-depth solution?

I noticed that gnome introduced this sort of information in the copy file dialog. It get’s me wondering how they did that (and even more-so how accurate it really is).

1
Apr

A (Very) Quick Ethernet Device Speed Test

Want to perform a quick speed/bandwidth test to get an idea of how fast your Ethernet device can download data? Here’s a one-liner that downloads a randomly stuffed testfile of 20MB that I uploaded to mediafire (you can use your own if you’d like):

wget -O - http://www.mediafire.com/?0jzdkokzmz4 > /dev/null

Now, of course, this gives you a very quick idea of typical, real world speeds. You can take it a step further and download a file from your local network. The more steps you take to eliminate other external factors, the better.

BTW: It’s April Fool’s Day today in case you’ve forgotten! Go trick somebody special…

« Previous Page