Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lesson-4 The Shell as Interpreter

Similar presentations


Presentation on theme: "Lesson-4 The Shell as Interpreter"— Presentation transcript:

1 Lesson-4 The Shell as Interpreter
By Simi

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

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

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

5 Shell The default prompt for the Bourne shell is $
(or #, for the root user). Another popular shell is C Shell. The default prompt for the C shell is %.

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

7 Shell Page through previously executed commands.
Provide command name completion and custom prompt, etc.

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

9 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)

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

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

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

13 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)

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

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

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

17 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).

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

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

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

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

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

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

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

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

26 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

27 Bourne Again shell (bash)
Bash also executes the file ~/.bash_logout before a user quits the system.

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

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

30 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 "?".

31 The asterisk "*" The asterisk, "*", represents any character or string of characters. The entry a*.txt could refer to: apple.txt array.txt

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

33 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

34 Wildcards * . $ ls –x chap*
Example: . $ ls –x chap* chap chap01 chap02 chap03 chapx chapy chapz

35 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

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

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

38 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

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

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

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

42 Standard input :- The default source is the keyboard.
Standrad output:- The default destination is the terminal. Standrad error:- The default destination is the terminal.

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

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

45 Command-line characters
& Run preceding command in background ; Command Terminator, Enter is another command terminator.

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

47 Standard error into standard output
cp file file1 2> &1: This merges standard error into standard output.

48 Command Grouping Command grouping is used to send the combined output of two separate commands to a file. Example: $ (date; cat results ) > newfile

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

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

51 The output of who is directly passed as input to ‘wc’.
Example: who|wc –l The output of who is directly passed as input to ‘wc’. The output of the above command would be number of users on the system.

52 Examples Example: who | sort
Pipe connects the output of the who command directly to the input of the sort command. Both commands in the pipeline run simultaneously.

53 If the “reader” attempts to read when the pipe is empty, it will wait until some data appears in the pipe before continuing. If the pipe becomes full( there may be some limit to the size of the pipe) The “writer” waits until some data has been removed from the other end.

54 Pipes only operate on stdout stream of commands.
Any messages written to stderr will still be written on the terminal screen unless it is separately redirected. By appending an ampersand (&) after the pipe character, one can combine the standard error and standard output and send it to the standard input of the program receiving the piped output.

55 Multistage Pipes Multistage Pipes : Pipes can be used to connect more than just two commands together. Multistage pipelines are common in Linux. Example: Who | grep tty | wc –l The output from the who command is processed by grep command,

56 It filters out all lines that do not contain the string “ttyp”.
This output is ultimately piped into wc, which counts the number of lines left that corresponds to the number of network users. The ability to connect small commands together in this way, to form larger more powerful utilities, is one the most useful aspects of Linux.

57 Demetafying characters
Many a times it is required to use a shell metacharacters literally rather than as a metacharacters. In this case metacharacters have to be stripped off their special meaning by attaching demetafying characters.

58 ‘ ‘(Single quotes) : Demetafies any metacharacters enclosed within it.
These are: \( Backslash) : It negates the special properties of the single character it precedes. ‘ ‘(Single quotes) : Demetafies any metacharacters enclosed within it. “ “(Double quotes): Demetafies all enclosed metacharacters except $,; and \ i.e: variable evaluation, command substitution and backslash.

59 Examples: ? $ \ [ $ echo \* \? \* * ? * $ a= ZENITH $ echo $a ZENITH
? $ \ [ $ echo \* \? \* * ? * $ a= ZENITH $ echo $a ZENITH $ echo “*$a*” *ZENITH*

60 Examples $ echo ‘The average pay is $1000’

61 Command Substitution In command substitution, command output is used as an argument in the command line when a command is enclosed with a pair of backquotes(`). The shell executes the command and replaces the enclosed text with the output of the command.

62 Example: $ echo “ There are `who|wc –l` users logged in”
There are 3 users logged in $ echo the date today is `date` The date today is Mon Jun 10 23:19:16 IST 2003

63 SHELL Variables Each shell has variable that can be assigned a value.
Bourne shell has three major types of shell variables. System/Environment variables User defined variables Positional variables

64 System/Environment variables
A set of pre-defined variables that govern the general operation of the environment. These are, as a convention defined in uppercase. To see the list of such variables : $ set

65 System Variables A list of common system variables are:
EXINIT HOME IFS LOGNAME MAIL PATH PS1 PS2 TERM SHELL TZ

66 EXINIT: It stores the initialization instructions for the ex and vi editors.
HOME: This stores the pathname of a user’s home directory. On logging in, a user is automatically placed in the home directory. To echo a variable use $ echo $HOME /usr/James

67 IFS: This is set to a list of characters that are used to separate words in a command line. This can be set to invisible characters like space, tabs, newlines or any other characters. Default is white spaces. LOGNAME: This lists the user’s login name.

68 MAIL: All mail addressed to the user is placed in a directory : mail
MAIL: All mail addressed to the user is placed in a directory : mail. The shell periodically checks this directory and notifies the user with a message, if mail arrives. PATH: All alternate paths in a sequence that can be searched to find the executable for any command or file are stored in it.

69 PS2: This is the secondary prompt,
PS1: This stores the value of primary prompts like $ for Bourne shell, % for C shell and so on. PS2: This is the secondary prompt, a prompt that occurs if a new line is started without finishing a command. Continue on a new line by using a backslash \ before hitting the return key. Example: $ echo This is an exa \ > mple of secondary Prompt.

70 TERM : It identifies your type of terminal.
There are utilities that are terminal dependent like vi. It makes use of control file in the directory ‘/usr/lib/terminfo’

71 User defined variables
A user can define as many variables as needed. These variables can be used without type declarations or initialization. These variables are treated as string variables and automatically initializes them to a null string. To perform numeric operations, explicit commands have to be used.

72 Shell variables are assigned values using the = assignment operator, but there should not be a white space on either side of it. Example: $#x=37 Assigns the string 37 to variable x echo $x To evaluate the value of a variable precede it by a $ symbol. 37

73 Positional Variables Shell scripts also accept arguments from the command line. This is implemented through positional variables which are $0,$1,$2…….$9. $0 gives the name of the program. $1 to $9, the command line arguments.

74 Positional Variables There a major difference between other variables and positional variables. Positional parameters are assigned values through command line arguments. Example: $ cat emp.sh echo “\program name : $0 number of arguments :$# “ $ emp.sh manager director Program name : emp.sh Number of arguments : 2

75 Comparison of BOURNE AND ‘C’ Shell
Bourne and ‘C’ Shells are two major types of shells supported by mostly all UNIX systems. These have much in common like the redirection, filename expansion, command substitution features. Some details like shell variables are handled differently in ‘C’ shell.

76 The shell script programming commands are different.
C shell has additional features not available in Bourne shell ie command history and alias. An alias system is for setting up shorthand notations for commands. History provides a more extensive job control system that allows processes to be moved from background to foreground and vice versa.

77 Aliases Korn, Bash and C shell accept aliases. For C shell
Examples % alias l ls –l Sets l as an alias for ls –l % alias l wc ‘ls | wc –l’ Quoting is necessary when using characters which have special meaning for shell.

78 Command History You can store , edit and execute previous commands by setting the size of history list by using the following command: % set history =100 This stores last 100 commands in memory. As History the list is stored in memory.

79 Command History Their size should be optimum.
Once you log out the history list is lost. C shell always, automatically stores the previous command in its memory, whether history variable is set or not.

80 To run the previous commands specify the event number or a string:
!g Runs the last command beginning with g. !! Repeats the last command. !5 Runs the event number 5. !5:p Prints event number 5, without executing it.

81 Thank You


Download ppt "Lesson-4 The Shell as Interpreter"

Similar presentations


Ads by Google