[Bash] Reading from Standard Input

To read from standard input in a Bash shell, use read. The following script reads content from stanard input and outputs the content:


# Uncomment this to use comma as a separator.
# IFS=","

read first second third
echo first=\"${first}\" second=\"${second}\" third=\"${third}\"

Read more of this post


[Sed] Editing files in place

Sed provides the -i or --in-place option for saving changes made to the file. For example, you can replace all tabs with a space with the following:

sed 's/\t/ /g' -i file.txt

Read more of this post

[KVM] Installing Virtio drivers in a KVM Windows guest VM

KVM provides a Virtio interface for the virtual hard disk and NIC. To use them in a Windows guest VM, the drivers from Fedora (you only need the ISO file) must first be installed into Windows. To install them in a Windows guest VM, it must also be started with these interfaces so that Windows can detect them. While the Windows guest could be started with the Virtio NIC without any problems, we can not start with the Windows image (i.e. the virtual hard disk image where Windows is installed on your guest VM) using the Virtio hard disk interface (until the driver is installed, Windows will not know how to use the Virtio hard disk interface). The easiest solution to this problem is to create another virtual disk that can use Virtio:

qemu-img create -f qcow2 <image name> <size>

Read more of this post

Using Shared Memory in Linux Programming

Shared memory is one way for processes to share information with each other. In the Linux environment, there are two main ways of obtaining shared memory in C or C++. The first method uses shmget to obtain an ID for the shared memory segment, then using shmat to attach the segment to the address space of the calling process. The declaration of these methods are as follows:

int shmget(key_t key, size_t size, int shmflg);

void *shmat(int shmid, const void *shmaddr, int shmflg);

Read more of this post

Using Ion, a Tiling Window Manager

When working in Linux, I usually have multiple windows open. I always need at least one terminal to compile and execute commands. I would usually use Vim to edit my files too. Most likely, I would also have Firefox open with some information. During my work cycle, I constantly need to use these three windows.This means I need to be able to see the contents of these windows. The default Gnome and KDE window managers are rather inefficient at this because they can stack windows on top of one another. To make them non-overlapping, I have to individually resize the windows.

Ion is a tiling window manager. Unlike the stacking window managers, a tiling window manager can arrange your windows, such that they are non-overlapping (note I said can, because some they can still let you put a window on top of another).

Ion window manager with three non-overlapping frames.

Ion window manager with three non-overlapping frames.

To install, visit the Ion website. If you are using Ubuntu, you can install the Ion3 package instead. Once installed and running, you are likely to be presented with a window containing two frames.

Starting Applications in Frames

To open an application in the frame, press F3 and type in the command to start it (such as “firefox”). If you want to open up a terminal, you can press just F2 instead and F1 for man page. There are even short cuts to use SSH (F4), edit files (F5) and view text files (F6).

If you have tried starting multiple applications, you’ll notice that they will all open up to the frame that is in focus. Moving applications to the other frame can be done using the keyboard. For a number of the keyboard shortcuts, Ion uses the mod key. By default, the mod key is set to the alt key. If you do not like this, you can configure Ion to use a different key.

Moving Between Windows

Switching between the windows in the same frame is done by pressing mod + k  n (That is, press mod and k keys simultaneously. Let go of the k key, but keep holding the mod key. Then press n while still holding the mod key).

Moving Windows to Other Frames

Using a single frame is not a good utilisation of the available screen space. In Ion, moving an application to another frame requires you to “tag” it first. To tag an application, bring the application to focus and pressing mod + t. A mark is placed at the top corner of the title bar to show that it has been tagged.

A tagged title bar.

A title bar with a "tagged" mark in the top right corner.

Pressing mod + t again will remove the tagging. It is possible to have multiple windows tagged simultaneously. This is particularly helpful when you want to move multiple windows

Attaching Windows

Once the window the window is tagged, move horizontally to the adjacent frame with mod + tab. To move vertically, use mod + n or mod + p. Pressing mod + a will prompt you for the name of the window to attach to the frame. If you want to place all of the tagged windows to the frame, you can simply use mod + k a (like before, press mod + k then mod + a, without letting go with the mod key).

Reorganising Your Frames

Ion allows you to reorganise your frames, however you want. First, to resize a frame, press mod + r and then use arrow keys to make the frame grow. Windows can be maximised horizontally (mod + k h) and vertically (mod + k v). To further split the current frame, use mod + k s for horizontal splits. To delete the current frame, use mod + k x.


Finally, Ion support for multiple workspaces (similar to having multiple desktops). Workspaces are created using the F9 key. Before one is created, it will prompt you for a name for the workspace. If you enter the name of an existing workspace, it will go to that one instead. You can also switch between workspaces by holding the mod key and pressing a numeric key.