Tuesday, December 8, 2009

Day 1: Shell Scripting From Scratch

A friend of mine at UDSM is doing a course that requires shell scripting so I agree to take time out an cover the basic of shell scripting, and you are welcome to join us. It really for beginners in shell.

Day 1: Laying the Foundation

What Is a Shell?

A shell is a program that takes commands you typed and tell the operating system to run them. The shell interprets your commands. For example, you may use the shell to enter a command to list the files in a directory, such as ls, or a command to copy a file, such as cp.

Example: ls is a command to list the content of the current folder.

$ ls
bin/    dev/         initrd.img.old@  media/  root/     sys/  vmlinuz@
boot/   etc/         lib/             mnt/    sbin/     tmp/  vmlinuz.old@
build/  home/        lib64/           opt/    selinux/  usr/
cdrom@  initrd.img@  lost+found/      proc/   srv/      var/
The $ is the shell prompt, which tells you the shell awaits your commands. ls listed all the content of the current directory, worry not we will get to these in a moment.

How does it work?

The shell looks for a program — that is, a file with execute permissions — with the name ls. The shell looks at all the directories in your command path. The shell runs the first program found that matches the name, checks if you have permission to excutes it and if you have its excutes it and then displays the results of the program to your screen, as in the second and third lines in the code example.

The command path is stored in the environment variable named PATH

$ $ echo $PATH

A shell acts as a form of wrapper around the OS, hence the term shell.

Why Use Shells?

Unix was the first popular operating system to break free of the single-shell monopoly, which can still be seen in MS-DOS shell on Windows. In Day 2: we will cover how to break free of the single-shell monopoly on Windows.

The Unix philosophy is that one command should do one thing and do it well. So the complex commands are combination of small commands. In this context, a shell is simply another command — a command that facilitates combining other commands. The ability to combine commands allows you to create new commands, thereby adding value to your operating system.

In addition, most shells allow you to group a number of commands in a file, called a shell script. When you run the shell script file, the shell executes the commands in the script file in order.

What Kind of Shells Are There?

Since there is no monopoly on shells, you are free to run any shell you desire. That’s all well and good,but choosing a shell without knowing the alternatives isn’t very helpful. So lets look at a few main shell out there.

  • The Bourne Shell - original Unix shell is known as sh, short for shell, created by Steven Bourne, and has been considered a standard part of Unix for decades.
  • The C Shell - was so named because much of its syntax parallels that of the C programming language, and added some neat features to the Bourne shell, especially the ability to recall previous commands (and parts of previous commands) to help create future commands.
  • The Korn Shell - like the C shell but also backward compatible with the older Bourne shell syntax.
  • Bash, the Bourne Again Shell - it is said "bash shell answered a clear need, a need shown by the initial success of the Korn shell". So bash offers command-line editing like the Korn shell, file-name completion like the C shell, and a host of
    other advanced features.
  • Other Shells - Over the years, a number of other shells have appeared, each with a small but devoted following. These shells include ash, zsh, and rc. Ash is the default shell and appears as sh on the Cygwin environment for Windows.

Choosing a Shell

For example, when administrators assume everyone runs the Korn shell, they may set up parts of the environment that break in strange ways for users of other shells, particularly users of the C shells. So before choosing to another shell make sure it will work given the assumptions made before. Most modern shell are all pretty good anyway.

Changing Your Default Shell

The chsh command, short for change shell, allows you to change your default, or login, shell.


chsh username new_default_shell
Example:change user jkpaul to use csh, run the chsh command as follows
$ chsh jkpaul /bin/bash
Note that you need to enter the full path to the shell. You may required to type you password for security reasons.The new login shell will be available for use the next time you log in.

That is it for day one, so now we are ready to get into using shell.

NEXT: Day 2: Running Shell and Commands

Thursday, December 3, 2009

Use one keyboard and mouse on multiple cross platform computers

synergy: [noun] a mutually advantageous conjunction of distinct elements

Synergy lets you easily share a single mouse and keyboard between multiple computers with different operating systems, each with its own display, without special hardware. Redirecting the mouse and keyboard is as simple as moving the mouse off the edge of your screen. Synergy also merges the clipboards of all the systems into one, allowing cut-and-paste between systems.

I have this old computer that as use as a backup and multimedia server. I have hooked it to an overhead projector and a decent sound system, now you know what I am up to over the weekends. For all this time ssh and Remote Desktop Viewer have served me very faithully. But I started wondering is this it, can I use my mouse and keyboard on both machines but on different displays, and lo and behold, ladies and gentlemen I present to you Synergy.
So lets get to it.


There is a Dell (Inspiron 1520) aka WorkStation, running Ubuntu 9.10 also the Synergy server and as a second machine, home built server aka Storage running Ubuntu 8.04LTS.
WorkStation: Side [left] | Hostname [feruzi.trilabs.co.tz] | IP Address []
Storage: Side [right] | Hostname [storage.trilabs.co.tz] | IP Address []

Installing from the Command Line

Start the terminal (you can also use Synaptic Package Manager) and run the following command on both machines.
$ sudo aptitude update && sudo aptitude install synergy
PLEASE NOTE: For MS Windows and MacOS users visit the site for more information.

Configuring the Synergy server

In your home directory create a file called
and in in it is where the configureation for my set up.
section: screens
section: aliases
section: links
    right = storage.trilabs.co.tz
    left = feruzi.trilabs.co.tz
section: options
 screenSaverSync = false 

section: screens

Screen section is a list of screen names, one name per line, each followed by a colon. Names are arbitrary strings but they must be unique. The hostname of each computer is recommended, and that is what I have used.

section: aliases

aliases section is a list of screen names just like in the screens section except each screen is followed by a list of aliases, one per line, not followed by a colon. An alias is a screen name and must be unique. During screen name lookup each alias is equivalent to the screen name it aliases. Because my DNS was not setup properly I assigned it to my IP address.

section: links

links section is a list of screen names just like in the screens section except each screen is followed by a list of links, one per line. Each link has the form {left|right|up|down}[] = name[]. A link indicates which screen is adjacent in the given direction. So on the right of feruzi.trilabs.co.tz there is storage.trilabs.co.tz and on the left of storage.trilabs.co.tz there is feruzi.trilabs.co.tz.
PLEASE NOTE: There is more you can do here, again visit the site for more information.

section: options

options section is a list of lines of the form name = value. These set the global options. There is a lot of other options you can set, visit the site for more information.

Starting up Synergy server and clients

To start up the server, on feruzi.trilabs.co.tz run the following command,
$ synergys -f --config synergy.conf
the output should be something like this, depending on your setting and operating systems.
INFO: synergys.cpp,1042: Synergy server 1.3.1 on Linux 2.6.31-15-generic #50-Ubuntu SMP Tue Nov 10 14:54:29 UTC 2009 i686
DEBUG: synergys.cpp,1051: opening configuration "synergy.conf"
DEBUG: synergys.cpp,1062: configuration read successfully
DEBUG: CXWindowsScreen.cpp,847: XOpenDisplay(":0.0")
DEBUG: CXWindowsScreenSaver.cpp,339: xscreensaver window: 0x00000000
DEBUG: CXWindowsScreen.cpp,117: screen shape: 0,0 1280x800 
DEBUG: CXWindowsScreen.cpp,118: window is 0x04800004
DEBUG: CScreen.cpp,38: opened display
DEBUG: CXWindowsScreen.cpp,679: registered hotkey ScrollLock (id=ef14 mask=0000) as id=1
NOTE: synergys.cpp,500: started server
INFO: CServer.cpp,1141: screen "feruzi" shape changed

And the cliet, in this case on storage.trilabs.co.tz, ssh into the client machine and run the following command and we are ready to rumble.
$ synergyc -f --name storage
the output should be something like this, depending on your setting and operating systems.
INFO: synergyc.cpp,716: Synergy client 1.3.1 on Linux 2.6.27-15-generic #1 SMP Tue Oct 20 06:52:09 UTC 2009 i686
DEBUG: CXWindowsScreen.cpp,841: XOpenDisplay(":0.0")
DEBUG: CXWindowsScreenSaver.cpp,339: xscreensaver window: 0x00000000
DEBUG: CXWindowsScreen.cpp,111: screen shape: 0,0 1400x1050 
DEBUG: CXWindowsScreen.cpp,112: window is 0x03000004
DEBUG: CScreen.cpp,38: opened display
NOTE: synergyc.cpp,330: started client
DEBUG: CXWindowsClipboard.cpp,313: open clipboard 0
DEBUG: CXWindowsClipboard.cpp,266: empty clipboard 0
DEBUG: CXWindowsClipboard.cpp,289: grabbed clipboard 0
DEBUG: CXWindowsClipboard.cpp,348: close clipboard 0
DEBUG: CXWindowsClipboard.cpp,313: open clipboard 1
DEBUG: CXWindowsClipboard.cpp,266: empty clipboard 1
DEBUG: CXWindowsClipboard.cpp,289: grabbed clipboard 1
DEBUG: CXWindowsClipboard.cpp,348: close clipboard 1
NOTE: synergyc.cpp,247: connected to server

And that is it. Have fun, questions and comments are welcome.