17
Linux Shell

Linux Notes By V - II

  • Upload
    marin

  • View
    220

  • Download
    1

Embed Size (px)

DESCRIPTION

Linux Notes by Vipul Vermawww.valferdo.comwww.youthutility.com

Citation preview

Linux Shell

Linux Shell

Computer understand the language of 0's and 1's called binary language.In early days of computing, instruction are provided using binary language, which is difficult for all of us, to read and write. So in OS there is special program called Shell. Shell accepts your instruction or commands in English (mostly) and if its a valid command, it is pass to kernel.Shell is a user program or its environment provided for user interaction. Shell is a command language interpreter that executes commands read from the standard input device (keyboard) or from a file.Shell is not part of system kernel, but uses the system kernel to execute programs, create files etc.Command history allows users to conveniently find and reissue previously issued commands. Every Unix-like system contains sh or another shell which incorporates its commands.bash(Bourne-again shell) is the default shell on Linux. It also runs on nearly every other Unix-like operating system as well, and versions are also available for other operating systems including the Microsoft Windows systems. Bash is asupersetof sh (i.e., commands that work in sh also work in bash, but the reverse is not always true), and it has many more commands than sh, making it a powerful tool for advanced users. But it is also intuitive and flexible, and thus it is probably the most suitable shell for beginners. bash was written for theGNUproject (whose goal is to develop a complete, Unix-compatible, high performance and entirelyfree operating system), primarily by Brian Fox and Chet Ramey. Its name is a pun on the name of Steve Bourne.ash(the Almquist shell) is acloneof sh that was written by Kenneth Almquist in 1989. It is the shell that is the most compliant with sh, and it does not have any additional functions that other interactive shells such as bash and tcsh offer. ash also features smallmemoryrequirements compared to the other sh-compliant shells and is thus well suited for systems with small memories, especially smallembedded systems(i.e., systems built into other products). It is available on most commercial Unix-like systems.csh(the C shell) has a syntax that resembles that of the highly popularC programming language(also developed at Bell Labs), and thus it is sometimes preferred by programmers. It was created in 1978 by Bill Joy (who also wrote thevi text editorand later co-founded Sun Microsystems) at the University of California at Berkeley (UCB).ksh(the Korn shell) is a superset of sh developed by David Korn at Bell Labs in 1983. It contains many features of the C shell as well, including a command history, which was inspired by the requests of Bell Labs users. It also features built-in arithmetic evaluation and advanced scripting capabilities similar to those found in powerful programming languages such as awk, sed and perl.

tcsh(the TENEX C shell) is based on csh but also has programmable file name completion, command line editing, a command history mechanism and other features lacking in csh. It is named after the TENEX operating system, which inspired the author of tcsh. tcsh replaced the csh as the default shell on some BSD operating systems (i.e., FreeBSD and Darwin).zsh(the Z shell) is similar to ksh, but it also includes many features from csh. That is, it attempts to combine the programmability and syntax of the Korn shell with useful features from the C shell (which has some disadvantages as a programming language). It was written by Paul Falstad around 1990.The great flexibility of shells on Unix-like operating systems is further enhanced by the fact that it is extremely easy tochange the current shell. The shell for any user can be switched temporarily, or that user's default shell can be changed.Some other families of operating systems also have shells. For example,MS-DOSbecame the shell on earlier versions of Microsoft Windows. However, it was replaced with a shell emulator on later versions (e.g., Windows 2000 and Windows XP), apparently because of the Microsoft philosophy of attempting to make the GUI all-powerful and de-emphasizing the command line.

Linuxand otherUnix-likeoperating systemsfeature the concept ofstandard streamsofdata. Eachcommand, and therefore eachprocess(i.e., runninginstanceof a program), is automatically initialized with (i.e., assigned) three data streams: one input stream, calledstandard input, and two output streams, calledstandard output andstandard error. These streams consist of data inplain textform (i.e., human readablecharacters) and are considered to be special types of files.This terminology can be confusing to new users. However, it is useful to become familiar with it because it is commonly employed indocumentation(including the onlinemanpages, which are found on almost all Unix-like systems).Users familiar with theC programming language(in which the Linuxkerneland many of its utilities are written) will be aware that it includes routines to perform basic operations on standard input and output. Examples includeprintf, which allows text to be sent to standard output, andscanf, which allows a program to read from standard input.Programs that run from aconsole(i.e., an all-text display mode) or from aterminal window(i.e., an all-text displaywindowin aGUI) can receive input data in several ways. One is throughcommand linearguments, which are names of files entered on the command line after the name of the program (or command) and any options. In the following example,file1andfile2are arguments for thewccommand:

wc -w file1 file2

That is,file1andfile2provide the input data for wc, which by default counts the number of lines, words and characters in any given text. The-woption customizes wc by telling it to count only the number of words in each file and ignore the number of lines and characters.

Standard InputStandard input, often abbreviatedstdin, is the input data for a program in the absence of any command line arguments. It is by default any text entered from the keyboard. Thus ifwcis typed in at the command line and the ENTER key is pressed without providing any arguments, any text typed in on all subsequent lines will be stored in memory until the wc command is executed by simultaneously pressing the CONTROL anddkeys on a new, blank line. wc will then count the lines, words and characters in that stored text.Standard input can beredirectedto come from any text file in place of the keyboard by using the input redirection operator, which is a leftward pointing angular bracket. Thus, to redirect the standard input for the commandsort(which sorts lines of text in alphabetic order) from the keyboard to the file namedfile3, type:

sort < file3

The result is the same as using file3 as an argument, although the mechanism is different, i.e.,

sort file3

A program can alternatively obtain its input from another command through the use of apipe, which can be considered a type of redirection operator and is represented by the vertical bar character. In the following example, the output of theheadcommand, which by default reads the first ten lines of a file, becomes the standard input for the sort command:

head file3 | sort

Standard OutputCommand line programs write to standard output, sometimes abbreviatedstdout. Its default destination is generally the display screen, but it can be redirected (e.g., to a file, where it will be saved, or to a printer) or piped to another program to serve as its standard input.For example, the commandfilewrites the names of files and their types (e.g., text, HTML, GIF or directory) to standard output, which is, as usual, the monitor screen. Thus the following command will display on the screen a list showing the name and type of each file in thecurrent directory(i.e., the directory in which the user is currently working):

file *

In this example, file's argument is thewildcardrepresented by the star character. A wildcard is a character than can represent some set of characters. The star wildcard can represent anystring(i.e., sequence of characters) containing any number of characters, and thus it can represent the name of any item in a directory.The standard output for this command can easily be redirected to another file using the output redirection operator, which is a rightward facing angular bracket. In the example below, it is redirected to a file namedfile4:

file * > file4

Or it can be redirected to become the standard input of another program using the pipe operator, such as:

file * | grep directory

Here the filtergrepis used to search for any lines in its standard input (which, of course, is the standard output offile *) that contain the sequence of characters directory. Again, because no redirection is used with grep, its standard output is the display screen.To carry the example a bit further, the standard output of grep could be redirected to a file or to another program. For example, the followingpipeline of commands sends grep's standard output to become the standard input forwc -l, which counts the number of lines in text. The number of lines is the same as the number of directories in the current directory, because each directory is listed on a separate line (as is each file). The standard output fromwc -lis then redirected tofile5, where it is saved for future reference:

file * | grep directory | wc -l > file5

Standard ErrorCommand line programs send error messages to the user via standard error, abbreviatedstderr. As is the case with standard output, its destination is the display screen by default, and it can likewise be redirected (e.g., to a file or printer). Standard error is a separate data stream from standard output in order to allow the two streams to be redirected separately and thus prevent them from becoming intermingled.As an example of an error message, thecatcommand, which reads the contents of a file, will produce an error message if an attempt is made to use it to read a non-existent file, such as:

cat nofile

In such case, an error message similar to the following will appear on a new line on the monitor screen, as the standard error was not redirected:cat: nofile: No such file or directory.Standard error can be redirected with the basic standard error redirection operator, which consists of the numeral 2 followed without an intervening space by a rightward facing angular bracket. In this case, it will create the file to which it is redirected if it does not yet exist, or it will overwrite the contents of the file if a file with the same name already exists. In the following example, the error message will be sent to a file namedfile6and will not appear on the display screen:

cat nofile 2> file6

An alternative is to use the standard error appending operator, which appends any error messages to the end of the text in the file rather than overwriting it. This operator, which consists of the numeral 2 followed with no intervening spaces by two rightward facing angular brackets, is useful for error log files.

PIPES

Standard i/p & standard o/p constitutes 2 separate streams that can be individually manipulated by the shell.

To count the number of users & stores in a file.

$ who > user.txt (enter)$ cat user.txt (enter)

Who gives number of users, stores it in a file.

$ wc l < user.txt

Displays total number of users who are logged in.

The above method of running 2 commands separately has 2 disadvantages:-1. For long running commands this process can be slow. The second command act unless 1st has completed its job.1. When handling large files, temporary files can build up easily & eat up disk space in no time.The shell can connect the 2 streams- whos standard o/p redirected & so was wcs standard i/p, and both used same disk space, using a special operator- the | (pipe) & avoid the creation of disk file.

$ who | wc l (enter)

When a sequence of commands is combined together in this way, a pipeline is said to be formed. Theres no restriction on the number of commands you can use in a pipeline. But you must know the behavioral properties of these commands to place them.

Eg:- $ man grep | col b | lp

The online manual (man) pages of a command often show the keywords in bold face. The pages contains a number of control characters which are removed here by col b command lp reads its standard i/p from cols o/p & prints the file. Here, the leftmost command must be able to write the standard o/p while rightmost command must be able to read from the standard i/p. Intermediate command must be able to do both.

Two special files

Sometimes, you would like to check whether a program runs successfully without seeing/p o/p on the screen. You may not want to save this o/p either in a file. You have a special file that simply accepts any stream without growing in size the file /dev/null.

Eg:- $ cmp file1 file2 > /dev/null (enter)$ cat /dev/null (enter)

Its file size is always zero. This facility is useful in redirecting error messages away from terminal so that they dont appear on screen. /dev/null is a pseudo-device coz unlike all other files, its not associated with a physical file/device./dev/tty indicates ones terminal. This isnt the file that represents standard o/p or standard error. Consider, for instance that user1 is working on terminal /dev/pts/1 & user2 on /dev/pts/2. However, both the users refer to their own terminal with the same filename -/dev/tty.

If user1 issues the command,

who> /dev/tty (enter)

the list of current users is sent to the terminal he is currently using /dev/pts/1.Same way user2 can use the command to see the o/p on his terminal.

Difference between Name and Nameless PipesNamed PipesNameless Pipes

1. Named Pipes are also called as Fifo.

2. A FIFO special file is similar to a pipe, except that it is accessed as part of the file system. When processes are exchanging data via the FIFO, the kernel passes all data internally without writing it to the file system

3. It connects unrelated processes.

4. Fifo are Unidirectional.

5. Fifo are first created then opened.

6. They exist in file system with a given filename.

7. Example:- mkfifo pathname1. Nameless Pipes are also called as Pipes.

2. Nameless Pipes is a combination of one or more commands, where the output of one command goes as input to another.

3. Pipes connect those processes which are related.

4. Pipes are Bidirectional.

5. Pipes are opened while it is being created.

6. They exist in Kernel.

7. Example:- ls - l | more