How to use bc, the calculator tool

You can do calculation stuffs easily with bash on the command line, however it isnt very intuitive. Bash still works well with some simple calculations but in case you want to perform more complicated calculations, this may become a real hassle. And that is why bc is a better tool for you to perform calculating on the command line. The tool bc can do much more than bash for you and its syntax is ways easier.

As far as I know, bc is a pre-included tool in all Linux distributions so you can use it whenever you want without installing anything.

There are three ways to use bc. The first one is to run it as a shell prompt where you can insert your operations to get the result. Just open the terminal and type "bc" and then you can do what you need, and type "quit" to quit bc.

The second way to use bc is by using STDIN. For example, here is a simple command:
 echo '1+2*3/4' | bc  

After you hit enter, you will get the result in the terminal.

The third way to use bc is to use files. Just write down your bc script into a file then call it:
 bc /path/to/file  

Basic math operations to do with bc

You can run all the basic math operations with bc. With the basic operations like addition, subtraction,  multiplication, division ... you can just open bc as a shell prompt and do everything as with a simple calculator.

However, one thing to notice is that by default, the number of digits which follow the decimal point in the result is 0. Which means if you want to get the square root of 10 with this command:
 echo 'sqrt(10)' | bc  

The result you get will be "3". To get the more exact result, you will have to change the scale variable first. For example, to get the result of square root of 10 with 10 digits following the decimal point, the command you can use is:
 echo 'scale=10;sqrt(10)' | bc  

With this command, you will have the result as "3.1622776601"

If you use bc as a shell prompt, you will need to run the scale command first before doing the calculation.

ibase & obase

ibase and obase are special variables which define output and input base. They are usually used to convert numbers. For example, to convert decimal digits to binary, in the shell prompt of bc, you can type:
 ibase = 10  
 obase = 2  
 23  

And you will get the result "10011"

To convert decimal number to hexadecimal using the STDIN method, you can use the command:
 echo 'ibase=10;obase=16;42' | bc  

And you will get the result "2A"

The pre-defined math functions

bc also has several basic math functions like sine - s(x), cosine - c(x), a(x) for the arctangent, l(x) for the natural logarithm, e(x) for raising the Euler constant to the power of x and j(n, x) for the Bessel function. However, you will have to invoke the library option ( -l) first to use these functions. Or else you will get an error. To use these math functions, just run "bc -l" instead of "bc" when doing the maths. These math functions will calculate their results to the scale set at the time, 20 as default, of their call.

For example, to find sine of 3 ( in radians ), the command you can use is:
 echo 's(3)' | bc -l  

Define your own functions

Just like any normal programming language, bc let you define your own functions. For example, in the shell prompt of bc, you can define a function, say mul3(x), to multiple a variable by 3 like this:
 define mul3(x) { return x*3 }  

Then you can just call the function to multiple 10 by 3:
 mul3(10)  

To get "30" as the result

New 32-way Raspberry Pi cluster built by US PhD candidate

Joshua Kiepert, a PhD candidate from Boise University, has built an awesome 32-way cluster from Raspberry Pis. Although clusters from Pi's have been made before, and even much larger, this is still a seriously cool project.

Keipert is currently doing a doctoral research, something related to data sharing system for wireless sensor networks. And he needs a cluster so he could simulate lots of distributed sensors. But the only available cluster in his university, a Xeon-powered one, is always in-demand, and its specs are also a little too much for what he needs. That's why he decided to build his own cluster for his lengthy experiments.

New 32-way Raspberry Pi cluster

With the help of fellow doctoral students, Kiepert built his own Beowulf cluster using RPis. Not only were the RPi’s unique features for controlling simple hardware very useful, but the cost of creating the cluster was incredibly low: at $45 per Pi, including an 8GB SD card, he could acquire the raw materials for a 32-way cluster for $1500. This is equal to the cost of just one node of the Xeon-powered cluster. In short, Kiepert was able to create an entire 32 node RPi cluster that requires less power than a single node in the cluster of his university facility.

USB power would also have complicated the housing for the cluster by adding weight, a problem Kiepert said was not insignificant because 32 ethernet cables were a drag on the slim and light computers.


When the cluster is really a cool project, there are still several downsides. The most prominent one is that that an RPi is nowhere near as powerful as a desktop PC. Also, because of the limited processing capability, the RPiCluster will not reliably support multiple users simultaneously. However, the creator of this project believes there wont be any negative impact on his current doctoral research. As a matter of fact, according to Kiepert, the performance is perfectly acceptable for his simulation needs and he is now using the cluster extensively for his dissertation work.

Read more here

How to use the "tee" command


How to use the "tee" command
The tee command is a little tool which is very useful but often overlooked.  The function of tee doesnt sound very exciting, it was created for only one purpose: write STDIN to STDOUT and a file. But tee actually can do a lot for you. Basically if you want to redirect STDOUT of a program as well as printing it to the screen, tee is the right tool to use. A very simple example would be:
 grep "anyString" file.txt | tee result.txt  

This command searches for all occurrences of "anyString" in the file file.txt and pipes it to tee which prints to the screen and writes it to the file result.txt. Normally, the example above isnt used much but you can do more with tee. For example, if you want to write some string into two files at once, the command with tee you can use is:
 echo "some text" | tee file1 > file2  

Now we've passed our example text to tee that writes it to file1 and STDOUT which we've redirected to file2. And that results to both files containing the same text. Thats how tee can help you speed up your workflow when piping together multiple operations. And of course, you can extend this to writing into 3, 4, ... or more files at once:
 echo "some text" | tee file1 | tee file2 > file3  

Another thing tee can help you is to avoid one problem when using sudo. The normal output redirection operator is always executed with your user privileges, also when you write a sudo in front of the command which generates the STDOUT text. In other words, this will fail if you dont have the permission to write to that file:
 sudo echo "something" > bar  

But with tee, everything will go well:
 echo "something" | sudo tee bar  

One last hint at the very end: tee behaves like the output redirection operator > so if the file already exists it will be overwritten. To make tee behave like the append operator >> use parameter -a.

Emmabuntus review - very disappointing

2 weeks ago, I got an email from the team of Emmabuntus asking for a review for their distro.  I agreed that I will check it soon but I had been very busy then. So today I decided to give Emmabuntus a try.

In case you have never heard about Emmabuntus before, it is a distro based on Xubuntu and according to the email I got, the aim of Emmabuntus is to be user-friendly and light-weight so it can be used on old computers.

First impressions, download and installation

Emmabuntus doesnt have a dedicated website, it uses a page on sourceforge.net for introduction about the distro and hosting the download image files.

Currently, there are 3 image files to download and I picked the latest version which is based on Xubuntu 12.04. The file size is really bigger than my expectation, over 3GB. So if you want to try this distro, make sure you use a DVD or a big enough USB.

I used Unetbootin to create the bootable USB for Emmabuntus. It booted up with no problem and the installation process is just similar to that of Ubuntu or Linux Mint, so new Linux users wont have any trouble to install Emmabuntus. Make sure you select the right keyboard layout before installing because French layout is set by default in Emmabuntus.

The installation took quite long due to the big size of the ISO file.


Emmabuntus desktop

Right after booting up Emmabuntus the first time, you will be ask to select a setting for the Cairo Dock. There are 3 options by default and I picked the second one, called "Simple". After that, the dock will appear and it will be there in the next boot up.

Emmabuntus review

One the default desktop of Emmabuntus, you got a panel on top and Cairo Dock at the bottom. On the top panel there are several applets. And on the Cairo Dock, there are many applications. All your internal partitions will be shown on the desktop by default.

review for emmabuntus

The default wallpaper is bluish, with a quote in French at the top right corner. I think it means " One day, the world will be free!"

Pre-installed applications

Since the ISO file is really big, there are a lot of default applications here, which reaches to the level of redundancy. You have both Firefox and Chromium, 3 music players, several chat clients, 2 video players,  many games and educational apps,  4 dictionaries, and many other miscellaneous apps like wikipedia, free software news ... When you click on the Menu on the top panel, each time you hover the mouse on a section, a huge submenu will appear showing a lot of applications.

there are a lot of preinstalled apps for everything

In short,  there are a lot of pre-installed applications in Emmabuntus, and many of them were unknown to me before. And this is something I really dislike because I always prefer minimal distros with just some basic preinstalled applications.

Performance and problems

This review is quite short because there are too many bugs that didnt allow me to try anything with Emmabuntus. The most serious bug happened to the keyboard layout. I didnt know what was wrong but half of my laptop keys were malfunctioned. They just gave wrong outputs. I thought something was wrong with my keyboard but after checking with a liveCD of Linux Mint, everything was still fine. So I believe it must be a problem of Emmabuntus. And since I cannot use the keyboard with Emmabuntus, I had to ditch it after several hours of testing.

Another problem with Emmabuntus is that whenever I boot up, there is always a dialog box asking me for the root password for something I dont even know.

One more thing I noticed is that the plymouth image of the bootable USB is still that of Xubuntu. After installing Emmabuntus successfully, the plymouth image will be changed. But in my opinion, it still looks quite ugly. Besides the grub 2 menu still shows Ubuntu instead of Emmabuntus.

Conclusion

Perhaps this is the first time I write a negative review for a Linux distro but I really dont like Emmabuntus. It is just Xubuntu with a lot of redundant pre-installed applications, which make the distro really bloated. Moreover the developing team is too careless and neglectful, they didnt test and double check anything before releasing the distro. So eventually I removed Emmabuntus after 3 hours.

How to navigate in Vim

Vim is always my favorite text editor. I've been using Vim for over 2 years and still learning it. If you want to use Vim, the first thing you will need to know is how to navigate in Vim, since it is text-based. In this article, I will show you some basic navigation shortcut keys to use in Vim.

How to navigate in Vim

1. Basic navigation

The most simple way to navigate around in vim is by using the arrow keys. Another method is use the four key:
- k: move up
- j: move down
- h: to the left
- l: to the right

In case you dont want to press the same key many times, a shortcut method is to use these key with a number. For example, if you want to move the cursor to the right skipping 12 letters, you can just hit "12l" or hit "12" then press the right arrow key.

2. Navigate within a line

If you want to navigate to different position within a line, you have 4 shortcut keys:
- 0: move to the beginning of the line
- ^: to the first character of the line ( not a space)
- $: to the end of the line (space included)
- g_ : to the last character of the line ( there is a hyphen here)

3. Screen navigation

Once you open a text file in Vim, you can navigate in relation to text shown in just the screen, not the entire file. Here are the shortcut keys:

- H: to the first line in the screen
- M: to the middle line in the screen
- L: to the last line in the screen
- Ctrl+f: Jump forward one full screen.
- Ctrl+b: Jump backwards one full screen
- Ctrl+d: Jump forward (down) a half screen
- Ctrl+u: Jump back (up) one half screen

4. Navigate through paragraphs

You can use the 2 following key to navigate through paragraphs in Vim:
- {: to the beginning of a paragraph, you can press it again to move to the next paragraph
- }: to the end of a paragraph, and similarly you can press it many times to move to the next paragraphs

5. Open a file from a specific position

You can open a text file from a specific position with vim. For example, if you want to open a file from line #32, you can use the following command to open it:
 vim +32 / path/to/the/file  

You can also open a file from a specific word, for example, to open a file from a certain word, say "Newyork", you can just use the following command:
 vim +/Newyork /path/to/the/file  

And vim will open the file with the cursor at the word "Newyork".

6. Some other navigation shortcuts

- N%: Go to the Nth percentage line of the file.
- NG: Go to the Nth line of the file.
- G: to the end of the file.
- g: to the beginning of the file.
-  `": to where you were in normal mode last time
- `^: to where you were in insert mode last time

Some tricks with htop

htop is an ncurses process viewer for Linux. It is somehow similar to top but much better. In this article, I will enumerate several useful tricks with htop. These tricks will be useful for those who work as a sysadmin.

1. Display processes in a tree structure

Displaying process in a tree structure is very useful, it is much easier to identify processes with a tree structure. To enable this view, you can press F5 or the key "t".

In this tree view, you can collapse or expand a specific process by pressing "+" or "-".


2. Display the setup menu

When you press F2 or "s" , the setup menu will appear, there are big 4 setup menus. In each big menu there are several submenus for you to select.

Meters: change the behavior of the meter bar
Display options: change the display options of htop
Colors: change the color theme
Columns: Choose what column needs to be displayed for the processes in the htop output


3. Change the default update interval

The default update interval of htop is around 1.5 seconds, to change it, you can use the following command to launch htop:
 htop -d n  

"n" is in tenths of seconds

4. Kill a process with htop

You can kill a process using htop. From the process list, just select the one you need to kill then press F9. A menu of signal will appear, to kill this process, select "SIGKILL" and hit Enter.


5. Kill multiple processes with htop

You can kill multiple processes with htop by grouping them first then using the SIGKILL signal. To group the processes, just select each process respectively then hit Space. The selected processes will be highlighted in yellow. To kill these processes, just hit F9 then select SIGKILL.


In case you select a wrong process, to un-select it, just hit Space again. To unselect all the selected proceses, just hit "u"