shell basics - wordpress.comwoodworking tools, from screwdrivers and chisels to power sanders and...
TRANSCRIPT
-
Shell Basics
Sahaj Computer Solutions
Visit: www.sahajsolns.com 1
-
Analogy
�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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
2
-
Analogy � Shell scripting is similar to a woodworking project.
� To build something out of wood, you need touse 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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
3
-
Shell basics
� Before you can build things using the shell, you need to learn some basics.
� Commands
� The shell
Sahaj Computer Solutions
Visit: www.sahajsolns.com
4
-
What is a command? � 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
$
Sahaj Computer Solutions
Visit: www.sahajsolns.com
5
-
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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
6
-
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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
7
-
Complex Commands
� 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 behaviour of a command.
� In this case, the command name is who, and the arguments are am and i.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
8
-
Complex Commands
� 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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
9
-
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
Sahaj Computer Solutions
Visit: www.sahajsolns.com
10
-
Compound Commands
� 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 ;
Sahaj Computer Solutions
Visit: www.sahajsolns.com
11
-
Compound Commands
� 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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
12
-
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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
13
-
What Is the Shell? � 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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
14
-
What Is the Shell? � For this reason, the shell is often referred to as the UNIX system's command interpreter.
� 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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
15
-
The Shell Prompt � The prompt, $, 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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
16
-
The Shell Prompt � 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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
17
-
Different Types of Shells
� 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.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
18
-
Different Types of Shells
� 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)
Sahaj Computer Solutions
Visit: www.sahajsolns.com
19
-
Bourne shell � The original UNIX shell was written in the mid-1970s by Stephen R. Bourne while he was at AT&T Bell Labs in New Jersey.
� The Bourne shell was the first shell to appear on UNIX systems, thus it is referred to as "the shell.“
� The Bourne shell is usually installed as /bin/sh on most versions of UNIX.
� For this reason, it is the shell of choice for writing scripts to use on several different versions of UNIX.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
20
-
Bourne shell � In addition to being a command interpreter, the Bourne shell is a powerful language with a programming syntax similar to that of the ALGOL language. It contains the following features: � Process control � Variables � Regular expressions � Flow control � Powerful input and output controls � Functions
Sahaj Computer Solutions
Visit: www.sahajsolns.com
21
-
Bourne shell
�One of the main drawbacks of the original Bourne shell is that it is hard to use interactively. The three major drawbacks are
� No file name completion
� No command history or command editing
� Difficulty in executing multiple background processes or jobs
Sahaj Computer Solutions
Visit: www.sahajsolns.com
22
-
The C Shell
� Bill Joy developed the C shell while he was at the University of California at Berkeley in the early 1980s.
� It was designed to make interactive use of the shell easier for users.
� Another design goal was to change the syntax of the shell from the Bourne shell's older ALGOL style to the newer C style.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
23
-
The C Shell
� The C language style of the C shell was intended as an improvement because the C language was familiar to the programmers working on UNIX at Berkeley.
� The idea was that a shell that used C language style syntax would be easier to write scripts in than a shell that used the ALGOL style syntax.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
24
-
The C Shell
� As it turned out, the C shell could not be used for much more than the most trivial scripts. Some of the major drawbacks are
�Weak input and output controls
� Lack of functions
�Confusing syntax due to a "lazy" command interpreter
Sahaj Computer Solutions
Visit: www.sahajsolns.com
25
-
The C Shell � Although the C shell did not catch on for scripts, it has become extremely popular for interactive use.
� Some of the key improvements responsible for this popularity follow:
� Command History. You can recall commands you previously executed for re- execution. You can also edit the command before it is re-executed.
� Aliases. You can create short mnemonic names for commands. Aliases are a simplified form of the Bourne shell functions.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
26
-
The C Shell
� File Name Completion. You can have the C shell automatically complete a filename by just typing a few characters of the file's name.
� Job Controls. The C shell enables you to execute multiple processes and control them using the jobs command.
� The C shell is usually installed on most systems as /bin/csh.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
27
-
The C Shell
� The TENEX/TOPS C shell, tcsh, is a newer version of the C shell that enables you to scroll through the command history using the up and down arrow keys.
� It also enables you to edit commands using right and left arrow keys.
� Although it is widely available in educational UNIX machines, tcsh is not always present on corporate UNIX machines.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
28
-
The Korn Shell � For a long time, the only two shells to choose from were the Bourne shell and the C shell.
� This meant that most users had to know two shells, the Bourne shell for programming and the C shell for interactive use.
� To rectify this situation, David Korn of AT&T Bell Labs wrote the Korn shell, ksh, which incorporates all the
� C shell's interactive features into the Bourne shell's syntax. For this reason, the Korn shell has become a favorite with users.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
29
-
The Korn Shell � In recent years, most vendors have started to ship the Korn shell with their versions of UNIX.
� Usually you will find it installed as /bin/ksh or /usr/bin/ksh.
� In general, ksh can be treated as fully compatible with sh, but some differences will prevent scripts from functioning correctly.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
30
-
The Korn Shell
� Some of the additional features that the Korn shell brings to the Bourne shell include the following:
�Command history and history substitution
�Command aliases and functions
� File name completion
� Arrays
� Built-in integer arithmetic
Sahaj Computer Solutions
Visit: www.sahajsolns.com
31
-
The Korn Shell
� There are three major versions of ksh available:
� The Official version ( ksh)
� The Public Domain version ( pdksh)
� The Desktop version ( dtksh)
Sahaj Computer Solutions
Visit: www.sahajsolns.com
32
-
The Bourne Again Shell � The Bourne Again shell, bash, was developed as part of the GNU project and has replaced the Bourne shell, sh, for GNU-based systems like Linux.
� All major Linux distributions, including Red Hat, Slackware, and Caldera, ship with bash as their sh replacement.
� Although it includes C shell ( csh and tcsh) and Korn shell ( ksh) features, bash retains syntax compatibility with the Bourne shell, enabling it to run almost all Bourne shell scripts.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
33
-
The Bourne Again Shell � bash was written by Brian of the Free Software Foundation and is currently maintained by Chester Ramey of Case Western Reserve University.
� Because bash is an implementation of the IEEE POSIX 1003.2/ISO 9945.2 Shell and Tools specification, it is extremely portable and can be built on most UNIX systems. It has also been ported to QNX, Minix, OS/2, and Windows 95/NT.
� Currently, only Linux ships with the Bourne Again shell. It is installed as /bin/bash. On most Linux systems, it is also installed as /bin/sh.
Sahaj Computer Solutions
Visit: www.sahajsolns.com
34
-
The Bourne Again Shell � Some features that bash includes in addition to those of the Korn shell are
� Name completion for variable names, usernames, host names, commands, and filenames
� Spelling correction for pathnames in the cd command
� Arrays of unlimited size
� Integer arithmetic in any base between 2 and 64
Sahaj Computer Solutions
Visit: www.sahajsolns.com
35