The Road to Unix


Chr. Clemens Lee
 
2001-02-12


The intended audience of this page are experienced Windows or Main Frame users who want to or have to become proficient Unix users (e.g. some of my collegues).

Table of Contents

1.  Literature
2.  Connecting to Unix from Windows with Exceed
3.  Emacs and Vi
     3.1  .emacs Startup Script
4.  Some Shell Tricks
5.  How to move from Windows to Unix
Links

1.  Literature

Having good books at hand is rarely a mistake. But you don't really need them. Every Unix command is described in a man page (try "man man" on the command line). There are also tutorials for all kinds of Unix areas (Vi, Emacs, shell programming, cvs, etc.) on the net (GIYF - Google is your friend).

UNIX Power Tools
by Jerry D. Peek, Tim O'Reilly, Mike Loukides
O'Reilly & Associates, edition: 2nd, October 1997, ISBN: 1565922603
Study this book and you will become a Unix power user. Only drawback is it contains 1120 pages :o]. If you buy only one Unix book, it should be this.
The UNIX CD Bookshelf, 2nd Editio
by Arnold Robbins, O'Reilly, Associates Inc.
O'Reilly & Associates, edition: 2nd, February 2000, ISBN: 0596000006
This collection contains UNIX Power Tools in HTML format on CD together with five more ORA books. That might be convenient to carry the info between home and the office.
Portable Shell Programming: An Extensive Collection of Bourne Shell Examples
by Bruce Blinn
Prentice Hall, edition: 1st, October 19, 1995, ISBN: 0134514947
I don't have this book myself, but the reviews at Amazon.com are very enthusiastic.
Linux-Unix-Shells. Bourne-Shell, Korn-Shell, C-Shell, bash, tcsh
by Helmut Herold
Addison-Wesley, Muenchen, edition: 3., aktual. Aufl., ca. 2000, ISBN: 3827315115
I have this book and it is not bad. If you take a little time to study shell scripting you can do a lot without unwrapping the heavy artillery languages.
UNIX System Administration Handbook
by Evi Nemeth, Garth Snyder, Scott Seebass, Trent R. HeinBruce Blinn
Prentice Hall PTR, edition: 3rd, August 2000, ISBN: 0130206016
If you do or plan to do real Unix administration you should get this book. It is THE classic (formerly know as The Red Book, 2nd edition). But its price is a classic as well. There is an interesting discussion on Slashdot about this book with some nice anecdotes. (Update: there is now a green book just covering different Linux distributions.)
Essential System Administration
by Aeleen Frisch
O'Reilly & Associates, edition: 2nd, December 1996, ISBN: 1565921275
If you really spend 70$ for the Unix System Administration Handbook you should take this as well just as an amendment. It covers the same topic with a similar range, but it's nice to have another view on it. And it is well written while the Red Book (now Purple) is sometimes a little bit condensed.
Last not least if you think about using Linux you might consider buying a recent SuSE distribution just to get the accompanying book. It is a nice introduction into Linux which is very technical but without going into to much detail. You can download the book online in pdf format and check if it is of any interest to you:
ftp://ftp.suse.com/pub/suse/i386/7.0/docu/book-en.pdf

If you want to install Linux for the first time and you have the bandwidth to download a complete distribution I can only recommend the Mandrake distribution. SuSE always delays the free online access to their latest distribution. RedHat, the market leader, for my taste messes to much around with bleeding edge packages (glibc, gcc) which seem to regularly effect execution of Java programs.

Update 2002-01-20: I am a great Debian fan now :-). Mandrake 7.1 was nice to install, but every newer version did not like my hardware, so it is of no use to me. Debian has a wonderfull update system (security updates are so simple and fast to install) and individual software packages are very carefully crafted together. Thought the install process is years behind that of the other distributions.

2.  Connecting to Unix from Windows with Exceed

Warning: if you are not using MS Windows or no Exceed got magically installed on your box by some system administrator, please proceed at the next section.

Exceed is an X-Window manager for Microsoft Windows. It allows you to connect to a Unix machine, work there, but see the graphical output all on your Windows machine. Either you have one big window which represents your Unix screen, or for each Unix application (xterm, xemacs, etc.) a new window pops up.

I assume Exceed is already installed on your Windows machine and you have an Unix account somewhere and want to use Exceed in single window mode.

Then go to the Windows "Start" menu, look under Programms for the "Exceed" menu and start XConfig. Among other config programms you see there Input, Communication, and Screen Definition.

Input

Screen Definition

Click on Monitor Info if you are not sure about your current screen size.

Communication

Define the host machine after clicking on 'Configure...'.

3.  Emacs and Vi

For editing it is worth to learn the basics of both editors. Vi has the advantage that you will find it on every Unix box and starts up quick to edit a single file from the command shell.

Emacs (XEmacs) is much easier to use simply because it has a toolbar and menus, so beginners will find a way to save and exit, which is already a challenge with vi, without much guidance.

Emacs is good to edit lots of files and huge files. It is overall very powerful and has lots of different modes for all kind of purposes. That is also its disadvantage, it is very bloated and big parts of it are written in Lisp itself (which is not known to be a very fast language). Vi is very much the opposite of Emacs in this regard. But both vi and emacs have very weird key bindings, especially if you are coming from a unix world.

You can have the best of both worlds by using emacs as an editor server with gnuserv and loading files from the command line with gnuclient. There is also a vi mode (named viper) for Emacs which emulates vi commands and key bindings to a degree selectable by the user.

I don't know of good resource to learn vi (the Unix Power Tools book has a section for vi and emacs) but there are dozens of tutorials on the web, so pick one from this google search.

I guess there are similar links for Emacs as well, but Emacs comes with a lot of documentation itself. After starting (X)Emacs, type Ctrl-h, take your hands of the keyboard, and then type 'i' (for info) and select the section that deals with Emacs itself. That should give you enough to study for the next three months.

Btw, vi is already pretty old and hasn't changed much for quite some time, but has some problems, e.g. handling large files. Most systems might also have vim (VI Improved) installed, which is a very actively maintained vi clone, it can handle large files, supports colors, creates backups of your edited files by default etc. Actually on Linux vim is normally distributed instead of vi (and renamed to vi).

3.1   .emacs Startup Script

You can put the following into the .emacs file in your home directory to get some additional features out of Emacs which I personally find very useful.

Gnuserv
First check with
which gnuclient
that gnuclient and also gnudoit binaries are in your environment path. XEmacs normally comes together with the gnuserv package. For Gnu Emacs someone needs to install the gnuserv package separately (thanks to Richard Stallman's refusal to add any code to Emacs he does not understand).
(require 'gnuserv) 
(gnuserv-start) 
(setq gnuserv-frame (selected-frame)) 
Now put into your shell resource file (.profile, .bashrc, .kshrc, .cshrc, whatever your shell picks up) an alias for gnuclient:
alias e='gnuclient -q'
The '-q' from gnuclient stands for 'quit'. Otherwise gnuclient would wait on the command line until the buffer with the loaded file gets closed.

Now test on the command line with

q testfile.txt
if your test file gets loaded into XEmacs.

Abbreviation
This is a treasure. Add this to your .emacs file and you will be able to save yourself a lot of typing.
(defun my-dabbrev-expand ()
  (interactive)
  (let ((old-case-fold-search case-fold-search))
	 (setq case-fold-search nil)
	 (dabbrev-expand nil)
	 (setq case-fold-search old-case-fold-search)
	 )
  )

;(global-set-key [?\M- ] 'my-dabbrev-expand)
(global-set-key [(control -)] 'my-dabbrev-expand)
Start xemacs again. Now you can complete unfinished words if the same word exists already somewhere else in an Emacs buffer. E.g.:
somewhere
som#
If your cursor is after 'som' and you hit the magic key, e.g. Ctrl-'-' (Control and minus), the second word will be completed to 'somewhere'.

You like it? I am sure you will.

Macros
For repititious tasks a quick macro, which you throw away after the task is done, can save a lot of time.

Type the following:

C-x ( type your macro now C-x )
Now you have created a nameless macro. To use it, type:
C-x e
To use it several times, e.g. to change several lines in your file, type:
C-u 70 C-x e
This will execute your macro 70 times, but only if possible. If you over estimated the number, the macro stops if it can't be executed anymore and does not run havoc. But if it does, you can undo the changes anyway (look in the toolbar for undo!).

Duplicate line
I regularly use a shortcut to dublicate a whole line, e.g. when there are only a few characters to change in the new one.
;; duplicate current line with F4
(defun duplicate-current-line()
  "Duplicate current line"
  (interactive)
  (let ((old (point)))
	 (copy-current-line)
	 (end-of-line)
	 (forward-char 1)
	 (yank)
	 (push-mark old)
	 )
  (exchange-point-and-mark)
  (next-line 1)
  )
(global-set-key [f4] 'duplicate-current-line)
Asign it to whatever key you prefer.

Duplicate char from line above
Many times the next line starts with the same characters as the previous. But still the new line will differ to much from the previous to make it worthwhile to copy the line from above and change it. Then I use this feature to copy char by char from above until I start entering the new text.
;; duplicate char from line above
(defun duplicate-char-from-line-above()
  "Duplicate char from line above"
  (interactive)
  (push-mark)
  (previous-line 1)
  (let ((bgn (point)))
	 (forward-char 1)
	 (kill-ring-save bgn (point))
	 )
  (exchange-point-and-mark)
  (yank)
  )
(global-set-key [?\M-_] 'duplicate-char-from-line-above)

Colors
Turning colors on in XEmacs is easy looking under the 'Options' menu. Changing the default colors can be a little bit more tricky. Therefore I put some examples in here.
(set-face-background 'default "black")
(set-face-foreground 'default "seagreen")      ; normal text
(set-face-background 'text-cursor "grey")
(set-face-foreground 'text-cursor "black")
(set-face-background 'highlight "black")
(set-face-foreground 'highlight "yellowgreen")
(set-face-background 'zmacs-region "seagreen")
(set-face-foreground 'zmacs-region "black")
(set-face-background 'primary-selection "seagreen")
(set-face-foreground 'primary-selection "black")
(set-face-background 'modeline "#a0b0b8")
(set-face-foreground 'pointer "#a0b0b8")
(set-face-background 'pointer "black")

4.  Some Shell Tricks

This is not the place to introduce you into using unix shells interactively or writing scripts. But some details that could make a big difference in usability I want to mention none the less.

The "." command
One big difference between MS Dos command batch programms and unix shells is that for each shell script that gets executed, a whole new process with its own environment is created. Once that script is finished that process closes and the original environment of the invoking script or shell is not changed at all.

Therefore having a shell that just changes the current directory has no effect at all to the invoking environment. The same applies to setting environement variables. E.g. invoke the following script from the command line with "./test_cd.sh":

#!/bin/sh

cd /tmp
pwd
Once you did it type in "pwd" and you will see that you are most likely (unless your old directory was already /tmp) not in directory /tmp. That is because once the shell script exits your old environment is restored again (well, technically it has never been changed, the script did just run in another new shell).

Now type ". ./test_cd.sh" (note the first dot and then space) and then "pwd" again in the command line. You will see that now you ARE in the directory /tmp. With the "." command a shell script gets interpreted inside your current shell. Sometimes you need to know of this feature to work yourself around a cliff.

PS1
Put the current directory location in your shell prompt. If you don't like it yourself, do at the least the person a favour that comes over to your desk to help you out with something. It is annoying to have to type "pwd" all the time to see where you are. So how can we set up the shell prompt?

In a bash shell type the following:

export PS1="\u@\h \w:-)"
Now you will always see who you are, on which machine, and what your current directory is. For more details read the man page of bash and look for the documentation of the PS1 environment variable.

E.g. I put the current time and date into the prompt as well, but e.g. having a colorred prompt is too much for my taste. I can't say anything about the z shell (zsh), but for the korn shell (ksh) do something like:

export PS1="$REALUSER@$MACHINE $PWD:->"

Personal initialization file
Now that you know about the "." trick and the first way of altering your environment by changing the PS1 variable it is only a small step of preserving that change so you don't have to type everything again whenever you open a new shell.

Just add the "export PS1=..." statement into a new file and type

. my_new_setup_file.sh
instead.

Now you don't even want to type that. Rename your file:

mv my_new_setup_file.sh ~/.profile
It might be wise to keep a backup of the original .profile around. Please find out stuff like what ~ stands for yourself (if you don't know it already yet), as this is not the place to tell you all the basics, but instead I want to concentrate to point a beginner in the right direction with tricks that are worth knowing as soon as possible (and many people who use Unix for years still miss out a lot of these potential time savers).

Anyway, whenever you log in the content of your .profile will be interpreted inside your bash (the korn shell (ksh) uses ~/.kshrc instead).

(Advanced stuff: actually you should put all your stuff into .bashrc and have only one line in the .profile which inteprets the .bashrc file. Check the bash man page and look for "initialization file".)

File completion
File completion is another one of this essential time safers. You start typing a file or directory name and as soon as the name is non-ambiguous you hit tab (with bash) and the shell completes for you the rest of the name. So now even typing long file names is a matter of a few key strokes.

I like the bash because it binds this feature to the <tab> key which is very convenient. I hate the ksh for the same reason, as it binds this feature in emacs mode to ESC ESC. This means twice as many key strokes for a common feature with a key which sits at an inconvenient position (my hands are not so big).

So if you are using the korn shell for whatever reason, this is how to get file completion there (note, I know this only for emacs editing mode, you might want to check out the vi mode for yourself).

set -o emacs
cd /hom_
If your cursor stands after "/hom" then hit twice the ESC key and the shell should complete it to "/home/". If there is another directory "/homophone" there, file completion does not work as the shell has trouble deciding which directory name to choose. With the bash simply hit tab a second time and you will see a list of all choices. With the ksh type ESC = to see this list.

I can't say anything about the zsh and it might also be possible to map file completion on the korn shell to another non default key.

Note that using a shell (e.g. bash) for interactive use should not lure you into using the same shell for scripts executable in batch mode. In that case for reasons of platform independence the old bourne shell (sh) and korn shell should be among your preferred choices.

CDPATH
Obviously the PATH variable is pretty elemental (just like on Windows/Dos). A similar variable, CDPATH, is much less significant. For that reason you might not immediately learn about it, but I think it can save you a lot of typing and therefore I want to mention it early on.

From the bash man page:

"The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. A sample value is ".:~:/usr"."

In this example, if you type "cd local", the shell first looks in the current directory if there is a "local" directory and if so changes to that directory. If there is no such directory, the shell will cd you to /usr/local. Your CDPATH can contain as many directories as you like. With a good selection of "anchor" directories you can move around big parts of the system very fast.

Java, X, and the clipboard
The integration of the Java clipboard with X Window's is rather sh****, and it is hard to decide whom to blame first. An extra tool is needed to cut and paste between Java applications and native X Window applications:
xclipboard
I don't know what more to say about it, just use it if the need arises. Java 1.4 is supposed to have better clipboard integration for X Window.

5.  How to move from Windows to Unix

You see more and more of your collegues moving to Unix or Linux? Less and less buddies to ask how to hack the Windows registry? I wouldn't say Windows is dying anytime soon, but the trend on the developer workstations is clearly visible towards Linux. And it always starts with the so called geeks.

Anyway, if you can hardly imagine how to leave the environment you are highly accustomed with, but your love for Microsoft's products is quite limited, here are my $0.02 thoughts about moving from one OS to another.

When I made the final switch from Windows to Linux, I used already for months only applications that I knew would also run on Linux. The three main applications I needed where Netscape, Emacs, and Java (Latex, Apache as well, but much less important). All these applications are cross platform. Since I am not a havy user of office applications, leaving MS Word, Excel etc. behind was not a big loss for me (actually I was using Lotus stuff anyway). Today for personal use Star Office is just fine, and you can test and use it already under Windows.

The application I missed the most after switching was Paint Shop Pro. For this I use now Gimp instead, thought I hate Gimp's terrible user interface, and lately I am using Wine to sometimes run Paint Shop Pro again (the old and simple version). My personal information manager of choice was the Lotus Organizer. But I converted all that data already on Windows to a Java application which I could later use on Linux as well.

When I finally switched, I still had to overcome a period where everything I knew by heart was new and it sometimes took ages to figure out the most basic tasks. But at least I where able to continue with the real work immediately, because I had all the important applications at my disposal.

I am well aware that everybody needs completely different applications, and if your favorite application does not exist under an OS, the whole OS is utter useless. But if you think most of your applications would run under Linux as well, e.g. with the KDE desktop, I can only recommend the switch over to Linux.

Links

The original location of this article:  http://www.kclee.com/clemens/unix/rtu/
Clemens Unics Page:  http://www.kclee.com/clemens/unix/
Clemens' Home Page:  http://www.kclee.com/clemens/


clemens@kclee.com,    last revised:   $Date: 2002/12/05 22:23:57 $