Friday, 1 October 2010

Several important Bash shortcut

I've been using Bash a lot, and sometimes when typing long command line, having to press the arrow button multiple times to go to the first character is very tiring.

This are few important shortcut that I start to use:

Ctrl + a - Jump to the start of the line
Ctrl + e - Jump to the end of the line
Alt + d - Delete word
Alt + f - Move forward a word, where a word is composed of letters and digits

Alt + b - Move backward a word

More info from this blogpost : http://linuxhelp.blogspot.com/2005/08/bash-shell-shortcuts.html

Or from Bash Reference Manual regarding Readline Movement : http://www.gnu.org/software/bash/manual/bashref.html#Readline-Movement-Commands

Thursday, 24 June 2010

Looking for undergraduates that interested to learn and practicing PHP and MySQL

Hi all,

I'm looking for undergraduates in IPTA/IPTS, preferably between 1st year to 3rd year, that are interested to learn and practice PHP and MySQL.

The undergraduates should be studying around Klang Valley for logistics reason.

The requirement is, they will be assigned a task or mini project, and they will learn on their own what ever topic needed in PHP and MySQL in order to complete the task or the mini project.

If they manage to complete the mini project, which I expected to be around 1 month depending on complexity, they will be rewarded with cash. The amount will be informed before the task start.

During the project, I will be available for consultation, and I expect the candidate to have a face-to-face discussion about the project progress with me, preferably once a week.

At the end of the project, the whole code and documentation will become my intellectual property, but they are allowed to quote the project name and knowledge that they have gained for their own curricular vitae.

If you an undergraduate and you are interested, please send your curricular vitae to my email at sharuzzaman@gmail.com

If you know any undergraduate that would be interested, please forward them this info, and ask them to send their curricular vitae to my email above.

Thanks.

Thursday, 20 May 2010

Suppressing getopts unknown option error message in ksh

I'm writing a new ksh (Korn Shell) script today. Before this, I only write bash script, and I believe the knowledge that I gain today will be valuable to a lot of people.

When I'm trying to use getopts in ksh, the script complain that there are "unknown option" after I put some option that are not available.

Let see the code, and the way it respond.

user@computer $ cat -n getopts.sh
     1  #!/bin/ksh
     2
     3  while getopts "h" arg
     4  do
     5    case $arg in
     6      h)
     7        echo "Help!"
     8        ;;
     9      ?)
    10        echo "Others"
    11        ;;
    12    esac
    13  done

user@computer $ ./getopts.sh -h
Help!

user@computer $ ./getopts.sh -a
./getopts.sh[13]: -a: unknown option
Others

I expect when I put the option "-a", the script will just output "Others" without any issue. But it does not. Something is not correct with the code.

Searching the Internet for answer, I found this very informative site: http://aplawrence.com/Unix/getopts.html

At first read, the site did not specifically mentioned that you have to put leading ":" to suppress the errors, but after reading it the second time, I got the idea, especially after reading this word:

The leading ":" works like it does in "getopt" to suppress errors

So, to solve the issue, some minor modification has to be done to the script above. Let see the updated script and the output after the modification.

user@computer $ cat -n getopts.sh
     1  #!/bin/ksh
     2
     3  while getopts ":h" arg
     4  do
     5    case $arg in
     6      h)
     7        echo "Help!"
     8        ;;
     9      ?)
    10        echo "Others"
    11        ;;
    12    esac
    13  done

user@computer $ ./getopts.sh -h
Help!
 
user@computer $ ./getopts.sh -a
Others


Voila! The problem solved :)

Happy scripting :)

Monday, 29 March 2010

Alternative way to grep IP address accurately from log file

This post is an alternative way to grep IP address from log file, as a reply for Hisham Mohd Aderis (linuxwave) post.

Let say we have a log file that contain IP address as below

$ cat ipadd.txt
192.168.1.1
192.168.1.10
192.168.1.11
192.168.1.100
192.168.1.101

If we use just grep to get 192.168.1.1, all the IP address will be returned. This is because the default setting for grep regular expression is greedy, which means that it will match anything that have full or part of the string that we are searching for.

$ grep 192.168.1.1 ipadd.txt
192.168.1.1
192.168.1.10
192.168.1.11
192.168.1.100
192.168.1.101

But, grep got a "-w" switch that will match only the word that we are looking for.

       -w, --word-regexp
              Select  only  those  lines  containing  matches  that form whole
              words.  The test is that the matching substring must  either  be
              at  the  beginning  of  the  line,  or  preceded  by  a non-word
              constituent character.  Similarly, it must be either at the  end
              of  the  line  or  followed by a non-word constituent character.
              Word-constituent  characters  are  letters,  digits,   and   the
              underscore.

So, to match an IP address correctly, we should use the command as below

$ grep -w 192.168.1.1 ipadd.txt
192.168.1.1

$ grep -w 192.168.1.10 ipadd.txt
192.168.1.10

As shown, the IP address will be matched to the one that we are looking for.

Happy scripting. :)

Monday, 4 January 2010

My 2010 resolution

A lot of people failed to achieve their last year resolution. I did not make any resolution last year, but I believe I made a lot of progress last year. New job, better salary, Suse Linux Professional 10 certification, ITIL v3 Foundation certification... quite an achievement I would say.

Most people also think, resolution should be made on new year day, but I think, resolution should be made year round. In Islam, there is a concept of daily muhasabah before you goes to sleep, to think what you have done good today, what you have not done what you should be doing, what bad thing that you have done and what you should do tomorrow to make sure it is a better day than yesterday, and you become a better person than yesterday.

So, if you still don't have you 2010 resolution, read this article first: New Year's Resolutions Experiment

In essence, the article mention that
Men were significantly more likely to succeed when asked to engage in either goal setting (e.g., instead of trying to lose weight in general, aiming to lose a pound each week), or focusing on the rewards associated with achieving their goal (e.g., being more attractive to the opposite sex).

Women were more successful when they told their friends and family about their resolution, or were encouraged to be especially resilient and not to give up because they had reverted to the old habits (e.g., if dieting, treating a chocolate binge as a temporary setback rather than as failure).

It also said that it is better to have only one resolution, and the resolution is specific.

For 2010, I have several resolution, but I will try my best to achieve it for the length of the year. Here goes:

1. Be a Red Hat Certified Engineer
2. Learn Python, become a master, and get certified.
3. Be a Debian Developer

Resolution number 3 will be very tough, but I know I should start doing something to become a Debian Developer. Let see if I can achieve what I have planned here by the end of the year :)