Wednesday, November 10, 2010

Help! Its Dark, I'm on the Command Line and I can Smell UNIX.

The Unix command line can be a daunting and scary place for the first time Unix user.  The first thing you need to do is assess your situation.  While all command prompts may look the same, and most act the same, they can be very different.  We are going to answer two questions.  Which operating system am I on and which shell am I using?

Which Operating System
Unix comes in many flavors.  The version you are using will make a difference in how you interact with the system and where the programs are located.  For example, Linux stores most additional applications in /opt/ directory where BSD stores the same programs in /usr/local/.  Mac OS X is also a flavor of Unix and it stores the applications in different locations as well.

To find out which operating system you are on, you will use the uname(1) command.  The (1) on the uname command refers to where it is found in the unix manual.  There may be two different sections that refer to this command, so we differentiate in documentation using (1)  to denote the section.    To use this command just type uname and press the return key.  You will get back the name of the operating system.

On my Mac OS X system, I get:
uname
  Darwin


On my work computers:
uname

FreeBSD

But that isn't all that it can do.  Lets try again with some options.  Actually, all the options, the -a implies all the options.

uname -a

Darwin admins-macbook-pro.local 10.4.0 Darwin Kernel Version 10.4.0: Fri Apr 23 18:28:53 PDT 2010; root:xnu-1504.7.4~1/RELEASE_I386 i386

You can see from this output I got that, I typed the command on a Mac OS X box.  Darwin is the Unix subsystem of Mac OS X.

Here it is again on my work computers.

uname -a
FreeBSD roadrunner.loonytunes.lan 7.3-RELEASE-p2 FreeBSD 7.3-RELEASE-p2 #0: Mon Jul 12 19:23:19 UTC 2010     root@amd64-builder.daemonology.net:/usr/obj/usr/src/sys/GENERIC  amd64

There is a lot of information here.  The Operating System.  The host name, or the name of the computer. The operating system version, when it was compiled and by whom.  It also includes the platform or processor type.

Once you know which system you are on, its much easier to look up specific information about the commands you need to learn.



Which Shell

So far, we have discovered which operating system we are working on by typing in a single command to the command line.  The next thing we need to do if figure out which shell we are using.  The command line you are typing at is called a "shell".  It accepts your commands and does its best to carry them out.  The shell is set as a user preference.  You can switch shells without changing operating systems.  You can even use the same shell when switching to a new brand of Unix.

The most common shells are: bash, tcsh and zsh.  All shells have a common set of commands, but each shell has its own set of unique extended features that make life easier.  If you learn the bash shell on Linux, you can use the same shell on Mac OS X, or FreeBSD.  This makes transferring between operating systems much easier.

To figure out which shell you are on type:

ps -p $$


This is actually a pretty complex command that searches the running processes and finds the one that is your shell and prints out its name.



> ps -p $$
  PID  TT  STAT      TIME COMMAND
75196  p0  Ss     0:00.01 -tcsh (tcsh)

The > is the tcsh prompt that I typed the command at.  The rest is the output from the ps(1) command.  To determine your shell, all you really need to look at is what is at the end.

$ ps -p $$
  PID TTY           TIME CMD
 4729 ttys000    0:00.02 bash

For this one, the $ is the prompt that I typed at and bash is my shell.

Once you know where you are, it will make getting around much easier.

No comments:

Post a Comment