2007/12/25

Linux Shell Basics

My father has a tool chest that holds all his woodworking tools, from screwdrivers and chisels to power
sanders and power drills. He has used these tools to build several desks, a shed, a bridge, and many toys.
By applying the same tools, he has been able to build all the different elements required for his projects.
Shell scripting is similar to a woodworking project. To build something out of wood, you need to
use the right tools. In UNIX, the tools you use are called utilities or commands. There are simple commands
like ls and cd, and there are power tools like awk, sed, and the shell.
One of the biggest problems in woodworking is using the wrong tool or technique while building a project.
Knowing which tool to use comes from experience. In this book, you will learn how to use the UNIX tools via
examples and exercises.
The simple tools are easy to learn. You probably already know how to use many of them. The power tools
take longer to learn, but when you get the hang of them, you'll be able to tackle any problem. This book
teaches you how to use both the simple tools and the power tools. The main focus is on the most powerful
tool in UNIX, the shell.
Before you can build things using the shell, you need to learn some basics. This chapter looks at the
following topics:
l Commands
l The shell
It's time to get started.
What Is a Command?
Simple Commands Compound Commands
Complex Commands Command Separators
In UNIX, a command is a program that you can run. In other operating systems, such as Mac OS
or Windows, you point to the program you want to run and click it. To run a command in UNIX, you type its
name and press Enter.
For example:

$ date [ENTER]
Wed Dec 9 08:49:13 PST 1998
$
Here, the date command has been entered. This command displays the current day, date, time, and year.
After the current date appears, notice that the $ character is displayed.
In this book, I use the $ character to indicate the prompt. Wherever you see a prompt, you can type the
name of a command and press Enter. This executes the command that you type. While a command
executes, the prompt is not displayed. When the command finishes executing, the prompt is displayed
again.
Caution - The $ character is a prompt for you to enter a command. It is not part of the command itself.
For example, to execute the date command, you type the word date at the prompt, $. Don't type $ date
. Depending on your version of UNIX, an error message might be displayed if you type $ date instead of
date at the prompt.
Now look at another example of running a command:
$ who
vathsa tty1 Dec 6 19:36
sveerara ttyp2 Dec 6 19:38
ranga ttyp0 Dec 9 09:23
$
Here, I entered the command who at the prompt. This command displays a list of all the people, or users,
who are currently using the UNIX machine.
The first column of the output lists the usernames of the people who are logged in. On my system, you can
see that there are three users, vathsa, sveerara, and ranga. The second column lists the terminals they
are logged in to, and the final column lists the time they logged in.
The output varies from system to system. Try it on your system to see who is logged in.
For those readers who are not familiar with the process of logging in to a UNIX system, the details are
discussed in Chapter 2, "Script Basics."
Simple Commands
The who and date commands are examples of simple commands. A simple command is one
that you can execute by just giving its name at the prompt:
$ command
Here, command is the name of the command you want to execute. Simple commands in UNIX can be small
commands like who and date, or they can be large commands like a Web browser or a spreadsheet
program.You can execute most commands in UNIX as simple commands.

Complex Commands
You can use the who command to gather information about yourself when you execute it as follows:
$ who am i
ranga pts/0 Dec 9 08:49
$
This tells me the following information:
l My username is ranga.
l I am logged in to the terminal pts/0.
l I logged in at 8:49 on Dec 9.
This command also introduces the concept of a complex command, which is a command that
consists of a command name and a list of arguments.
Arguments are command modifiers that change the behavior of a command. In this case, the
command name is who, and the arguments are am and i.
When the who command runs as a simple command, it displays information about everyone who
is logged in to a UNIX system. The output that is generated when a command runs as a simple command is
called the default behavior of that command.
The arguments am and i change the behavior of the who command to list information about you only. In
UNIX, most commands accept arguments that modify their behavior.
The formal syntax for a complex command is:
$ command argument1 argument2 argument3 ... argumentN
Here, command is the name of the command you want to execute, and argument1 through argumentN
are the arguments you want to give command.
Compound Commands
One of the most powerful features of UNIX is the capability to combine simple and complex commands
together to obtain compound commands.
A compound command consists of a list of simple and complex commands separated by the
semicolon character ( ;). An example of a complex command is
$ date ; who am i ;
Wed Dec 9 10:10:10 PST 1998

ranga pts/0 Dec 9 08:49
$
Here, the compound command consists of the simple command date and the complex command who am
i. As you can see from the output, the date command executes first, followed by the who am i
command. When you give a compound command, each of the individual commands that compose it
execute in order.
In this example, the complex command behaves as if you typed the commands in the following order:
$ date
Wed Dec 9 10:25:34 PST 1998
$ who am i
ranga pts/0 Dec 9 08:49
$
The main difference between executing commands in this fashion and using a complex command is that in
a complex command you do not get the prompt back between the two commands.
The formal syntax for a complex command is:
$ command1 ; command2 ; command3 ; ... ; commandN ;
Here, command1 through commandN are either simple or complex commands. The order of execution is
command1, followed by command2, followed by command3, and so on. When commandN finishes
executing, the prompt returns.
Command Separators
The semicolon character ( ;) is treated as a command separator, which indicates where one
command ends and another begins.
If you don't use it to separate each of the individual commands in a complex command, the computer will
not be able to tell where one command ends and the next command starts. If you execute the previous
example without the first semicolon
$ date who am i
an error message similar to the following will be produced:
date: bad conversion
Here, the date command thinks that it is being run as a complex command with the arguments who, am,
and i. The date command is confused by these arguments and displays an error message. When using
complex commands, remember to use the semicolon character.
You can also terminate individual simple and complex commands using the semicolon character. For
example, the commands

$ date
and$ date ;
produce the same output due to the order in which commands execute.
In the first case, the simple command date executes, and the prompt returns.
In the second case, the computer thinks that a complex command is executing. It begins by executing the
first command in the complex command. In this case, it is the date command. When this command finishes,
the computer tries to execute the next command. Because no other commands are left to execute, the
prompt returns.

No comments: