970 likes | 1.44k Views
Lesson-4 The Shell as Interpreter. By Simi. LINUX Shell. The shell sits between user and the operating system, acting as a command interpreter. It reads the terminal input and translates the commands into actions taken by the system. The shell is analogous to command.com in DOS. Shell.
E N D
Lesson-4The Shell as Interpreter By Simi
LINUX Shell • The shell sits between user and the operating system, acting as a command interpreter. • It reads the terminal input and translates the commands into actions taken by the system. • The shell is analogous to command.com in DOS.
Shell • When you log into the system you are given a default shell. • When the shell starts up it reads its startup files and may set environment variables, command search paths, and command aliases, and executes any commands specified in these files. • The shell gathers input from user and executes programs based on that input.
Shell • When a program finishes executing, it displays that program's output. • The original shell was the Bourne shell, sh. • Every Unix / LINUX platform will either have the Bourne shell,or a Bourne compatible shell available.
Shell • The default prompt for the Bourne shell is $ (or #, forthe root user). • Another popular shell is C Shell. The default promptfor the C shell is %.
Shell • Numerous other shells are available from the network. • Almost all of them are based on either sh or csh with extensions to provide job control to sh. • It allow in-line editing of commands.
Shell • Page through previously executed commands. • Provide command name completion and custom prompt, etc.
The Shell • Shell is often referred to as the LINUX system's command interpreter. • It is much more than a command interpreter. • It is also a powerful programming language, complete with conditional statements, loops, and functions.
Different Types of Shells • Linux has a variety of different shells: • Bourne shell (sh) • C shell (csh) • Korn shell (ksh) • TC shell (tcsh) • Bourne Again shell (bash)
Types of Shells • The Korn shell, ksh, by David Korn • Bourne Again Shell, bash, from the Free Software Foundations GNU project. • Both based on sh, the T-C shell, tcsh, and the extended C shell, cshe, both based on csh.
Bourne shell (sh) • Bourne shell (sh):Initialization scripts (.profile) • This login script should be added to the home directory at the time of user creation. • When user logs in, the shell executes these two files.
Bourne shell (sh) • A global initialization file, /etc/profile, that sets the variables. • Issues commands meant to be common to all users. • The user’s own .profile containing settings made by the user. • A .profile can be quite large depending on the user’s requirement.
The C shell • The C shell was written by Bill Joy at the University of California at Berkeley. • His main intent for writing the C shell was to create a shell with C language-like syntax. • The initialization Scripts (.cshrc, .login and .logout)
The C shell • tcsh is a Unix Shell based on and compatible with the C shell (csh). • It is essentially the C shell with programmable command, completion, command-line editing, and a few other features.
The C shell • Its syntax is modeled after the C Programming Language. • The C shell added many feature improvements over the Bourne shell, such as aliases and command history. • Today, the original C shell is not in wide use on Unix; it has been superseded by other shells such as the Tenex C Shell(tcsh) based on the original C shell code.
The C shell • Adding filename completion and command line editing, comparable with the Korn shell (ksh), and the GNU Bourne-Again shell (bash). • An independently-developed and modernized C shell, created by Nicole Hamilton, also survives on Windows in the form of Hamilton C shell.
Born Again Shell (Bash) • The most popular shell is “bash”. • Bash is the shell that will appear in the GNU operating system. • Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh).
Born Again Shell (Bash) • It offers functional improvements over sh for both programming and interactive use. • Bash is not only an excellent command line shell, but a scripting language in itself. • Shell scripting allows to use the shell's abilities and to automate a lot of tasks that would otherwise require a lot of commands.
Tcsh • Tcsh is an enhanced, but completely compatible version of the Berkeley UNIX C shell (csh). • It is a command language interpreter usable both as an interactive login shell and a shell script command processor. • It includes a command-line editor, programmable word completion, spelling correction, a history mechanism, job control and a C-like syntax.
Tcsh • The 't' in tcsh comes from the T in TENEX, an operating system which inspired Ken Greer, the author of tcsh, with its command-completion feature. • Early versions of Mac OS X shipped with tcsh as the default shell, but it has since been replaced by bash.
Korn Shell • Korn shell is a superset of Bourne shell. • Everything included in Bourne shell applies to korn shell as well. • Korn is rich in features and needs its own environment (rc) script. • This often is the file -/.kschrc as most users migrate to this shell from the C shell.
Korn Shell • When user logs in to the system these are the scripts that are executed in this order: • A global initialization file, /etc/profile, containing settings common to all users. • The user’s own -/.profile. • The file defined in ENV variable, which is usually -/.kshrc.
Korn Shell • ENV is a special variable which must be present in every .profile used by Korn. • The ENV variable serves two purposes: • It defines the file that is also run on login and when invoking a sub shell. • It also executes the environment file.
Korn Shell • .profile is executed only on login. • Those statements that need to be executed only once should be placed in this file. • The remaining statements should go to the environment file.
Bourne Again shell (bash) • Like Korn, bash is also a superset of Bourne. • When a user logs in to the system, the scripts are executed in following order: • A global initialization file, /etc/profile, containing settings common to all users.
Bourne Again shell (bash) • The user’s own “profile” bash looks for these three files in the sequence specified - /.bash_profile, -/.bash_login and -/.profile. • The moment it finds one file, it ignores the others. • An environment file set by the variable BASH_ENV, but only if explicitly specified in the profile. This file is generally ~/.bash
Bourne Again shell (bash) • Bash also executes the file ~/.bash_logout before a user quits the system.
The shell’s Interpretive cycles • The activities which are performed by the shell in it’s interpretive cycle are: • Issues a prompt and waits for user to enter the command. • The shell scans the command line for metacharacters and expands the abbreviations like (* in rm *) to recreate a simplified command line.
Contd: • It then passes on the command line to the kernel for execution. • The shell waits for the command to complete and normally can’t do any work while the command is running. • After the command execution is complete the prompt reappears and the shell returns to it’s waiting role to start the next cycle. • User is now free to enter the next command.
Wild cards • Wildcards are place holders used to allow users to search for or use multiple files with similar names. • The subject of wildcards is part of the larger subject of regular expressions in linux. • Two of the most common wildcards are "*" and "?".
The asterisk "*" • The asterisk, "*", represents any character or string of characters. • The entry a*.txt could refer to: • apple.txt • array.txt
Wild cards • For example, to find a file called "sneaksomething.txt," enter: ls sneak*.txt. • The linux shell lists every file that matches that pattern: sneakers.txt. • Regular expressions are more complex than the straightforward asterisk or question mark.
Wildcards and Regular Expression • Here are wildcards and regular expressions: • * — Matches all characters • ? — Matches one character • \* — Matches the * character • \? — Matches the ? character • \) — Matches the ) character
Wildcards * • Example: • . $ ls –x chap* • chap chap01 chap02 chap03 chapx chapy chapz
The ? wildcard • The ? wildcard means 'any single characters'. • Although you probably will not use this wildcard as much as the *, it is still useful. • For example, if we want to list only the files that start with 'myfile' and end with a single additional character, we could type: • % ls myfile? • myfile2 myfile3
Wildcard Matches ? Any single character * Any group of zero or more characters [ab] Either a or b [a-z] Any character between a and z, inclusive [set] Any character in set [!set] Any character not in set.
Examples • ls –l *This will match ‘* ‘ with all filenames in the current directory and complete long listing of the files will be the output. • ls –l ab? This will match all filenames of three characters starting with ‘ab’. • ls –l c* This will match all names starting with c followed by zero or more number of characters.
Examples • ls –x .???* This will match all files starting with a dot and followed by at least three characters. Example: .exrc, .profile, .cshrc, .mailrc , .abc, the three characters followed by * which matches any number of characters. • echo * This will display a list of all files in the current directory
Examples • cp chap[0123] progs : This will copy all files starting with ‘chap’ and followed by any one of 0,1,2,3 into progs directory. • ls –x chap[!012] : This lists all files starting with ‘chap’ and not followed by 0,1,2. • cat emp[!0-9]: This concatenates all files beginning with the string ‘emp’ and followed by a non-numeric characters.
Examples • ls [A-Z] *[0-9]: Matches any filenames starting with a capital letter and ending with a digit. • ls * [0-9][0-9]: Matches any filename ending with two digits. Each bracket pair represents just one character place.
Redirection • Shell sets up these three standard files (for input, output and error) • Shell attaches them to a user’s terminal at the time of logging in. • Any program that uses streams will find them open and available. • The shell also closes these files when the users logs out.
Standard input :- The default source is the keyboard. • Standrad output:- The default destination is the terminal. • Standrad error:- The default destination is the terminal.
Input and output redirection : • You can redirect the standard input and output of a command with the following syntax statements. • < file Take input from ‘file’ rather than the standard input in overwrite mode. • >file Send output not to the standard output, the terminal, but to the ‘file’ in overwrite mode.
Command-line characters • \ Continue command on next line. • n>file Redirect the output of the file descriptor ‘n’ to file. n is zero for standard input,’1’ for standard output and ‘2’ for standard error. • 1>&2 Redirect standard output to standard error i:e merge standard output and standard error.
Command-line characters • & Run preceding command in background • ; Command Terminator, Enter is another command terminator.
Example: Redirection • cat < file1 > file2 : Input can be taken from a file file1 other than standard input and send output to file file2 other than standard error or output on screen. • cp file1 file2 2>errmesg : Error, if any occurs, are not printed on the screen but redirected to the file ‘errmesg’. The 2 is file descriptor associated with standard error.
Standard error into standard output • cp file file1 2> &1: This merges standard error into standard output.
Command Grouping • Command grouping is used to send the combined output of two separate commands to a file. • Example: $ (date; cat results ) > newfile
Background Process • Any command typed on the command line suffixed by an & is run as a background process. • The pid (Process identifier) of the submitted job is returned. • Example: • $ cat file1 | lpr & This will pipe the contents of file1 to printer as a background job.
Pipes • The shell uses pipes to connect input and output streams. • No intermediate files need to be created. and data is passed serially. • Special operator “|” is used as a connector between two commands. • The output of the command is input to the command after pipe.