Você está na página 1de 27

The Linux Shells Introduction

Operating Systems Laboratory Amir Saman Memaripour

Linux OS Layers

Shell

User

Hardware

Kernel

The Origin of Bash


A shell is a program that runs operating system commands. The first Unix shell was developed by Steven R. Bourne in 1974 for the Seventh Edition of Unix. Called the Bourne shell (or sh) in honor of its creator. It set the standard for Unix shells, including the default dollar sign ($) prompt common to most shells.

The Origin of Bash


Users frequently had to type the same commands over and over again. Writing a program to repeat their commands required a high-level language like C. Instead, it was useful to compose a list of commands for the shell to execute, as if the shell was following a well-rehearsed script. With the addition of features to make simple decisions and loops, these shell scripts could run commands and determine whether they succeeded or failed, taking appropriate action, without resorting to a different language.

The Origin of Bash


Many rival shells began to appear, offering improvements in speed and capabilities. Two of the most common successors were csh, a shell loosely based on the C language, and the Korn shell (ksh), an improved Bourne shell developed by David G. Korn. Shell designers incorporated commands into the shell for better speed and portability and to make the shells easier to work with. Successful features were freely copied between shells as they matured.

The Origin of Bash


An enhanced version of the Bourne shell was developed as part of the GNU project used by the Linux operating system. The shell was called Bash (for the pun Bourne Again Shell). This shell was compatible with the original shell created by Steven R. Bourne, but included many enhancements. It was also compliant with the POSIX standard for shells.

When Is a Program a Shell Script?


A script is similar to a program but there is no formal definition for what constitutes a script. It is a type of simple program, and scripts and programs share many features in common. Linux programmers with few special requirements never advance beyond writing shell scripts. Most shells, including Bash, can perform bit manipulation, string processing, and TCP/IP connections. They have typed variables, built-in commands, and user-defined functions. The distinction between shells and programming languages has become blurred.

When Is a Program a Shell Script?


In general, there are two key differences between shell scripts and more complex programs.

1. Shells are designed to handle short, simple tasks. They lack the
rigid structure and semantic checking of a high-level language. Shell scripts can be written quickly because they assume the programmer knows what he or she is doing, and for short scripts, this is usually true.

2. Shells always execute their scripts slowly. Although most shells


perform some kind of pre-processing to speed up execution, they still interpret and carry out one line at time. High-level languages are faster because they almost always translate a program into machine language to gain the best performance.

The Necessity of Structured Shell Programming


Many business processes are simply a matter of following a series of steps, after which the results are labeled and archived. This is the very type of activity scripts are made to handle. Because of the essential openness and complex syntax of Bash, its to the benefit of any serious project to adapt some kind of standard. Bash is very forgiving, but the costs are not. For example, there are many ways to add 2 + 2 in Bash. Like the autopilot anecdote, its not practical to expect the programmers who maintain the scripts to have to deal with this kind of redundancy.

Installing Bash
Bash is the standard shell on most Linux distributions. However, there are other Linux shells available and theres no requirement that Bash should be present on any particular distribution. The Bash shell is open source software released under the GNU Pubic License (GPL). If you need to install Bash, the C language source code is freely available from the Free Software Foundation at www.gnu.org or through one of its download mirrors.

10

Installing Bash
The Bash installation procedure can vary over time. Complete instructions are found in the README file that accompanies the sources. However, the installation is largely automated. The basic procedure is as follows:

1. Run the configure program to determine your operating system


and verify the features necessary to compile Bash.

2. Run make to build Bash. 3. Run make tests. This will run diagnostic tests to ensure Bash was
built properly.

4. Run make install to install Bash under the /usr/local/ subdirectory.

11

The Linux Environment


Unix-based operating systems, including Linux, extend this principle to long-term storage. Linux organizes information on a disk as a collection of files. Every file, whether a program or data, is treated the same, making the operating system very simple to build as well as flexible to use. Commands that work on a certain kind of file tend to have a similar effect on other kinds of files as well, thus reducing the number of commands a programmer needs to learn.

12

Files and File Systems


Pathnames without a beginning slash are called relative paths because they specify the location of a file in comparison to the current directory. Relative paths are useful for representing files in your current directory or subdirectories of your current directory. Pathnames with a beginning slash are called absolute paths. Absolute paths describe the location of a file in relationship to the root directory. No matter where your current directory is, absolute paths always identify the file precisely. Absolute paths are useful when locating common files that are always stored in the same place.

13

Files and File Systems


Most Linux distributions include the following directories:

/dev /bin and /usr/bin /lib and /usr/lib /var /etc /usr/local/bin /opt /tmp /sbin and /usr/sbin

Contains device drivers Contains standard Linux commands Contains standard Linux libraries Contains configuration and log files Contains default configuration files Contains commands not a part of the distribution, added by your administrator Contains commercial software Stores temporary files Contains system administration commands (/sbin stands for safe bin)

14

Directories
Root directory Home directory (~) Current directory (.) Parent directory (..)

15

Inodes and Links


Normally, each file is listed in a single directory. Linux can create additional listings for a single file. These shortcuts are called links and can refer to any kind of file.

Hard link Symbolic link

16

Hard Links
A hard link is a reference to another file in the current directory or a different directory. Whenever some action is performed to the hard link, it is actually done to the file the hard link refers to. Hard links are accessed quickly because they do not have to be dereferenced, but Linux limits where a hard link can be placed. As long as a file is being referred to by at least one directory entry, it wont be deleted. For example, if a file has one hard link, both the link and the original file have to be deleted to remove the file.

17

Symbolic Links
The second and more common link is the symbolic link. This link is a file that contains the pathname of another file. Unlike hard links, symbolic links have no restrictions on where they can be used. They are slower and some commands affect the link file itself instead of the file the link points to. Symbolic links are not hard because they have to be dereferenced: When Linux opens the symbolic link file, it reads the correct pathname and opens that file instead. When the file being referred to is deleted, the symbolic link file becomes a dangling link to a non-existent file.

18

Inodes
Using links means that two different pathnames can indicate the same file. To identify a particular file, Linux assigns a number to each file. This number is called the inode (or index node) and is unique to any storage device. If two pathnames refer to a file with the same inode, one of the paths is a hard link.

19

Inodes
In the ext2 file system, there is a limited number of inodes, which in turn places an upper limit to the number of files that can be stored on a disk. The number of inodes compared to the amount of disk space is called the inode density. The density is specified when a disk or partition is initialized. Most Linux distributions use an inode density of 4K, or one node per every 4096 bytes of disk space.

20

Pipe and Socket Files


Pipe files are a special kind of file shared between two programs. The file acts as a buffer for sharing information. One program writes to the pipe file and the other reads from the pipe. When the pipe file reaches a certain size, Linux halts the writing program until the reading program can catch up. A similar kind of file is called a Unix domain socket file. A socket file acts like a pipe but works using network sockets. However, this kind of file is not easily used in shell scripts and it wont be covered here.

21

Device Files
The last common kind of nonregular file is a device file. Keeping with the file-oriented design of Linux, devices are represented by files. Device files allow direct communication to a particular device attached to a computer. There are actually two kinds of device files, but shell programmers are mainly interested in the type called character device files.

22

Device Files
All devices files are located in the /dev directory. Even though many files are listed in /dev, not all of these devices might actually be present. Rather, /dev contains a list of devices that can be attached to your computer because the Linux kernel was configured to recognize them if they were attached. Most of these files are not accessible to regular users, but there are a few that are open to general use.

23

Device Files
/dev/null One important device file available to all users is /dev/null. This file represents an imaginary black hole device attached to your computer that consumes anything sent to it. This is useful for discarding unwanted responses from a shell command. /dev/null can also be read, but the file is always empty.

24

Device Files
/dev/zero Another device file is /dev/zero. This file contains an endless stream of zeros, and can be used to create new files that are filled entirely with zeros.

25

Device Files
There are a variety of other devices that might appear in /dev, depending on your distribution and computer hardware. Common device files include:

/dev/tty: The terminal window (or console) your program is


running under

/dev/dsp: The interface that plays AU sound files on your


sound card

/dev/fd0: The first floppy drive /dev/hda1: The first IDE drive partition /dev/sda1: The first SCSI drive partition

26

Assignment (5 points)
Make a list of similarities and differences between inodes in various Linux file systems such as ext-2 and ext-3. Deadline: November 13th, 10:00 AM How to Via email to memarypour@gmail.com This assignment is mandatory! deliver?!

27

Você também pode gostar