Thursday, August 23, 2012

Shell Basics




Shell Basics

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 blog, 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 blog 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:
  •  Commands
  •  The shell
It's time to get started.

What Is a Command?
Simple Commands, Compound Commands, Complex Commands and 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.

Caution - The $ character is a prompt for you to enter a command. It is not part of the command itself.

$ 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.

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:
  •  My username is ranga.
  •  I am logged in to the terminal pts/0.
  •  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.

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.


Note - You will frequently see the semicolon used to terminate simple and complex commands in scripts. Because the semicolon is required to terminate commands in other languages, such as C, Perl, and Java, many script programmers use it the same way in scripts. No extra overhead is incurred by using the semicolon in this manner.


What Is the Shell?

  • The Shell Prompt
  • Different Types of Shells

In the preceding section, I explained that when you type the command
$ date

the computer executes the date command and displays the result.
But how does the computer know that you wanted to run the command date?

The computer uses a special program called the shell to figure this out. The shell provides you with an interface to the UNIX system. It gathers input from you and executes programs based on that input. When a program finishes executing, it displays that program's output.

For this reason, the shell is often referred to as the UNIX system's command interpreter. For users familiar with Windows, the UNIX shell is similar to the DOS shell, COMMAND.COM.

The real power of the UNIX shell lies in the fact that it is much more than a command interpreter. It is also a powerful programming language, complete with conditional statements, loops, and functions. If you are familiar with these types of statements from other programming languages, great. You'll pick up shell programming quickly. If you haven't seen these before, don't fret. By the time you finish this blog, you'll know how to use each of these statements.

The Shell Prompt

The prompt, $, which was discussed in the beginning of this chapter, is issued by the shell. While the prompt is displayed, you can type a command. The shell reads your input after you press Enter. It determines the command you want executed by looking at the first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.

To the shell, your input looks like the following:
$ word1 word2 word3 ... wordN

The shell always picks word1 as the name of the command you want executed. If there is only one word 
$ date
the shell's job is easy. It executes the command. If there are more words
$ who am i
the shell passes the extra words as arguments to the command specified by word1.

Different Types of Shells

You might notice that your prompt looks slightly different than the $ prompt I am using. The actual prompt that is displayed depends on the type of shell you are using.
In UNIX there are two major types of shells:
  •  The Bourne shell (includes sh, ksh, and bash)
  •  The C shell (includes csh and tcsh)

If you are using a Bourne-type shell, the default prompt is the $ character. If you are using a C-type shell, the default prompt is the % character. This blog covers only Bourne-type shells because the C-type shells are not powerful enough for shell programming.

Note - In UNIX there are two types of accounts, regular user accounts and the root account. Normal users are given regular user accounts. The root account is an account with special privileges the administrator of a UNIX system (called the sysadmin) uses to perform maintenance and upgrades.
If you are using the root account, both the Bourne and C shells display the # character as a prompt. Be extremely careful when executing commands as the root user because your commands effect the whole system.

None of the examples in this blog require that you have access to the root account to execute them.



The different Bourne-type shells follow:
  •  Bourne shell ( sh)
  •  Korn shell ( ksh)
  •  Bourne Again shell ( bash)
  •  POSIX shell ( sh)

The different C-type shells follow:
  •  C shell ( csh)
  •  TENEX/TOPS C shell ( tcsh)

Unless explicitly noted, the examples and exercise answers in this blog will work with any Bourne-type shell.


Task:

1. Classify each of the following as simple, complex, or compound commands:
$ ls
$ date ; uptime
$ ls -l
$ echo "hello world"

2. What is the effect of putting a semicolon at the end of a single simple command or a complex command?

3. What are the two major types of shells? Give an example of a shell that falls into each type.


No comments:

Post a Comment