Linux Blog

Bash, PHP or Perl?

Filed under: Shell Script Sundays — TheLinuxBlog.com at 12:23 pm on Sunday, January 6, 2008

One thing that is for certain is that some people like performance. Running an operating system like Linux gives you choices right out of the box for scripting. This blog post will show the differences in times between Bash, PHP and Perl.

Basically what I have done is created three scripts, one in each language. Then I run them with the time tool to test out the execution time.

Here are the results of echoing 1-100000 out to /dev/null for each script:

BASH:
 
real    0m1.966s
user    0m1.664s
sys     0m0.072s
 
PHP:
 
real    0m0.309s
user    0m0.220s
sys     0m0.048s
 
Perl:
 
real    0m0.126s
user    0m0.096s
sys     0m0.000s

You can clearly see out of this the order of performance by this test is Perl, PHP and then BASH. Now, what is interesting is when I run the script to stdout these are the results:

BASH:
 
real    0m8.689s
user    0m2.408s
sys     0m0.416s
 
PHP:
 
real    0m4.381s
user    0m0.544s
sys     0m0.372s
 
Perl:
 
real    0m3.938s
user    0m0.244s
sys     0m0.324s

Bash takes about twice as long as Perl / PHP to complete the exact same task. The winner of this test is Perl but I’ll be doing more tests over time when I become more familiar with Perl

Linux Christmas Gifts

Filed under: General Linux — TheLinuxBlog.com at 1:43 pm on Saturday, December 15, 2007

With Christmas rapidly approaching time is running out to buy gifts for all of your family.

With this I post the question: “What are you getting for your Linux Geek for Christmas?”.

Gifts for Linux users are hard to find, luckily for you the reader I’ve compiled this handy list of Linux Swag (KEYWORD) that you can buy your Linux Geek.

1) Top choice is gadgets. Now they can vary in price so maybe you could team up with a couple of friends to afford something or get a gift card if your not exactly sure what to buy. USB Hard drives are great choices, if you know they already have drives check out the Linux Based NSLU2 from major big box stores. Its a great little box that your Linux geek will know exactly what to do with. If you’ve got a lot of money to spare a Fox Box (I haven’t posted about this yet) will be the perfect gift. Make sure you order now or you might not get it in time for christmas unless you live in Europe.

2) Clothing is a good gift. Get your Linux geek some nice clothes. T-Shirts are great choices here are some in order by taste: “Generic Linux Shirt“, “Arrogant Linux Elitist” or stuff from Hackerthreads.com the Open Source and hack wear sections are my favorite.

3) When you need to get the job done some times Caffeine is essential. That link has a ton of products not only to give to Linux geeks but for Linux geeks to give.

Well, I hope this has given you some ideas on what to buy Linux geeks for Christmas :)

Select Statements in Bash

Filed under: Shell Script Sundays — TheLinuxBlog.com at 10:40 pm on Sunday, August 19, 2007

Shell scripts often need a menu so that the user can interact with the script and choose options. An easy way to so this is with the “select” statement.

Syntax:

select selection_var [in list]; do
statements (can use selection_var)
done

Example:

echo "########################################";
echo "#  SELECTS EXAMPLE - TheLinuxBlog.Com  #";
echo "########################################";
echo "#        Please Choose an Option       #";
echo "########################################";
 
select selection_var in Number_1 This_is_Choice_2 Exit; do
case $selection_var in
Number_1 )
echo "You picked $selection_var or #$REPLY"
;;
This_is_Choice_2 )
echo "You picked $selection_var or #$REPLY"
;;
Exit )
echo Goodbye!
break
;;
* )
echo "Invalid Selection"
;;
esac
done

This bash select example has three choices. The first two are just examples, the third exits the script. There is a fourth case but it is not a choice, it tells the user that they have entered an invalid selection. I recommend using a case inside of a select because it will make life easier when adding onto a script. The $REPLY variable is returned from the select statement as a means of knowing what number was pressed. The $REPLY variable can be used in the case statement but I avoid doing so as all of the case blocks will have to be rearranged every time a new option is added.

The select statement in bash is very easy to implement and it can add a whole new range of functionality into your scripts. So try them out and look out for them in future shell scripts from The Linux Blog.