Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The...

81
Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi

Transcript of Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The...

Page 1: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:1

Lesson-4The Shell as Interpreter

By Simi

Page 2: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:2

LINUX Shell

The shell sits between user and the The shell sits between user and the operating system, acting as a operating system, acting as a command interpreter. command interpreter.

It reads the terminal input and It reads the terminal input and translates the commands into translates the commands into actions taken by the system.actions taken by the system.

The shell is analogous to The shell is analogous to command.com in DOS. command.com in DOS.

Page 3: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:3

Shell

When you log into the system you are When you log into the system you are given a default shell. given a default shell.

When the shell starts up it reads its When the shell starts up it reads its startup files and may set environment startup files and may set environment variables, command search paths, and variables, command search paths, and command aliases, and executes any command aliases, and executes any commands specified in these files.commands specified in these files.

The shell gathers input from user and The shell gathers input from user and executes programs based on that input. executes programs based on that input.

Page 4: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:4

Shell

When a program finishes executing, it When a program finishes executing, it displays that program's output.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.

Page 5: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:5

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

Page 6: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:6

Shell

Numerous other shells are available Numerous other shells are available from the network.from the network.

Almost all of them are based on either Almost all of them are based on either sh or csh with extensions to provide job sh or csh with extensions to provide job control to sh.control to sh.

It allow in-line editing of commands.It allow in-line editing of commands.

Page 7: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:7

Shell

Page through previously executed Page through previously executed commands.commands.

Provide command name Provide command name completion and custom prompt, completion and custom prompt, etc. etc.

Page 8: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:8

The ShellThe Shell

Shell is often referred to as the LINUX Shell is often referred to as the LINUX system's system's command interpreter.command interpreter.

It is much more than a command It is much more than a command interpreter. interpreter.

It is also a powerful programming It is also a powerful programming language, complete with conditional language, complete with conditional statements, loops, and functions.statements, loops, and functions.

Page 9: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:9

Different Types of Shells

Linux has a variety of different shellsLinux has a variety of different shells::

Bourne shell (sh) C shell (csh) Korn shell (ksh) TC shell (tcsh) Bourne Again shell (bash)

Page 10: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page: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.

Page 11: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:11

Bourne shell (sh)

Bourne shell (sh):Bourne shell (sh): Initialization scripts Initialization scripts (.profile)(.profile)

This login script should be added to the This login script should be added to the home directory at the time of user creation.home directory at the time of user creation.

When user logs in, the shell executes these When user logs in, the shell executes these two files. two files.

Page 12: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page: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.

Page 13: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:13

The C shell

The C shell was written by Bill Joy at the The C shell was written by Bill Joy at the University of California at Berkeley.University of California at Berkeley.

His main intent for writing the C shell His main intent for writing the C shell was to create a shell with C language-was to create a shell with C language-like syntax.like syntax.

The initialization Scripts (.cshrc, .login The initialization Scripts (.cshrc, .login and .logout) and .logout)

Page 14: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:14

The C shell

tcsh is a Unix Shell based on and tcsh is a Unix Shell based on and compatible with the C shell (csh).compatible with the C shell (csh).

It is essentially the C shell with It is essentially the C shell with programmable programmable command, command, completion, command-line completion, command-line edediting, and a few other features.iting, and a few other features.

Page 15: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:15

The C shell

Its syntax is modeled after the C Its syntax is modeled after the C Programming Language.Programming Language.

The C shell added many feature The C shell added many feature improvements over the Bourne shell, improvements over the Bourne shell, such as aliases and command history.such as aliases and command history.

Today, the original C shell is not in wide Today, the original C shell is not in wide use on Unix; it has been superseded by use on Unix; it has been superseded by other shells such as the Tenex C other shells such as the Tenex C Shell(tcsh) based on the original C shell Shell(tcsh) based on the original C shell code.code.

Page 16: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:16

The C shell

Adding filename completion and Adding filename completion and command line editing, comparable with command line editing, comparable with the Korn shell (ksh), and the GNU the Korn shell (ksh), and the GNU Bourne-Again shell (bash).Bourne-Again shell (bash).

An independently-developed and An independently-developed and modernized C shell, created by Nicole modernized C shell, created by Nicole Hamilton, also survives on Windows in Hamilton, also survives on Windows in the form of Hamilton C shell.the form of Hamilton C shell.

Page 17: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:17

Born Again Shell (Bash)

The most popular shell is “bash”. The most popular shell is “bash”.

Bash is the shell that will appear in the Bash is the shell that will appear in the GNU operating system.GNU operating system.

Bash is an sh-compatible shell that Bash is an sh-compatible shell that incorporates useful features from the incorporates useful features from the Korn shell (ksh) and C shell (csh).Korn shell (ksh) and C shell (csh).

Page 18: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:18

Born Again Shell (Bash)

It offers functional improvements over It offers functional improvements over sh for both programming and interactive sh for both programming and interactive use.use.

Bash is not only an excellent command Bash is not only an excellent command line shell, but a scripting language in line shell, but a scripting language in itself.itself.

Shell scripting allows to use the shell's Shell scripting allows to use the shell's abilities and to automate a lot of tasks abilities and to automate a lot of tasks that would otherwise require a lot of that would otherwise require a lot of commands.commands.

Page 19: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:19

Tcsh

Tcsh is an enhanced, but completely Tcsh is an enhanced, but completely compatible version of the Berkeley UNIX C compatible version of the Berkeley UNIX C shell (csh).shell (csh).

It is a command language interpreter It is a command language interpreter usable both as an interactive login shell usable both as an interactive login shell and a shell script command processor. and a shell script command processor.

It includes a command-line editor, It includes a command-line editor, programmable word completion, spelling programmable word completion, spelling correction, a history mechanism, job correction, a history mechanism, job control and a C-like syntax. control and a C-like syntax.

Page 20: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:20

Tcsh

The 't' in tcsh comes from the T in The 't' in tcsh comes from the T in TENEX, an operating system which TENEX, an operating system which inspired Ken Greer, the author of tcsh, inspired Ken Greer, the author of tcsh, with its command-completion feature.with its command-completion feature.

Early versions of Mac OS X shipped with Early versions of Mac OS X shipped with tcsh as the default shell, but it has since tcsh as the default shell, but it has since been replaced by bash.been replaced by bash.

Page 21: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:21

Korn Shell

Korn shell is a superset of Bourne shell.Korn shell is a superset of Bourne shell. Everything included in Bourne shell Everything included in Bourne shell

applies to korn shell as well.applies to korn shell as well. Korn is rich in features and needs its Korn is rich in features and needs its

own environment (rc) script.own environment (rc) script. This often is the file -/.kschrc as most This often is the file -/.kschrc as most

users migrate to this shell from the C users migrate to this shell from the C shell. shell.

Page 22: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:22

Korn Shell

When user logs in to the system these When user logs in to the system these are the scripts that are executed in this are the scripts that are executed in this order: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.

Page 23: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page: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.

Page 24: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:24

Korn Shell

.profile is executed only on login..profile is executed only on login. Those statements that need to be Those statements that need to be

executed only once should be placed in executed only once should be placed in this file.this file.

The remaining statements should go to The remaining statements should go to the environment file. the environment file.

Page 25: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:25

Bourne Again shell (bash)

Like Korn, bash is also a superset of Like Korn, bash is also a superset of Bourne.Bourne.

When a user logs in to the system, the When a user logs in to the system, the scripts are executed in following order:scripts are executed in following order:

A global initialization file, /etc/profile, containing settings common to all users.

Page 26: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page: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

Page 27: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:27

Bourne Again shell (bash)

Bash also executes the file ~/.bash_logout before a user quits the system.

Page 28: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:28

The shell’s Interpretive cycles

The activities which are performed by the shell The activities which are performed by the shell in it’s interpretive cycle are: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.

Page 29: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page: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.

Page 30: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:30

Wild cards

Wildcards are place holders used to Wildcards are place holders used to allow users to search for or use multiple allow users to search for or use multiple files with similar names. files with similar names.

The subject of wildcards is part of the The subject of wildcards is part of the larger subject of regular expressions in larger subject of regular expressions in linux.linux.

Two of the most common wildcards are Two of the most common wildcards are ""*"*" and " and "??". ".

Page 31: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:31

The asterisk "*"

The asterisk, "*", represents any The asterisk, "*", represents any character or string of characters. character or string of characters.

The entry a*.txt could refer to:The entry a*.txt could refer to: apple.txt array.txt

Page 32: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:32

Wild cards

For example, to find a file called For example, to find a file called "sneaksomething.txt," enter: ls sneak*.txt."sneaksomething.txt," enter: ls sneak*.txt.

The linux shell lists every file that matches The linux shell lists every file that matches that pattern: sneakers.txt.that pattern: sneakers.txt.

Regular expressions are more complex than Regular expressions are more complex than the straightforward asterisk or question mark.the straightforward asterisk or question mark.

Page 33: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:33

Wildcards and Regular Expression

Here are wildcards and regular Here are wildcards and regular expressions: expressions:

* — Matches all characters ? — Matches one character \* — Matches the * character \? — Matches the ? character

\) — Matches the ) character

Page 34: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:34

Wildcards *

Example:Example:

. $ ls –x chap*

chap chap01 chap02 chap03 chapx chapy chapz

Page 35: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:35

The ? wildcard

The ? wildcard means 'any single characters'.The ? wildcard means 'any single characters'.

Although you probably will not use this wildcard Although you probably will not use this wildcard as much as the *, it is still useful.as much as the *, it is still useful.

For example, if we want to list only the files For example, if we want to list only the files that start with 'myfile' and end with a single that start with 'myfile' and end with a single additional character, we could type:additional character, we could type: % ls myfile? myfile2 myfile3

Page 36: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:36

WildcardWildcard Matches Matches

?? Any single characterAny single character

** Any group of zero or more Any group of zero or more characterscharacters

[ab][ab] Either a or bEither a or b

[a-z][a-z] Any character between a and z, Any character between a and z, inclusiveinclusive

[set][set] Any character in set Any character in set

[!set][!set] Any character not in set.Any character not in set.

Page 37: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:37

Examples

ls –l *ls –l * This will match ‘* ‘ with all This will match ‘* ‘ with all filenames in the current directory and filenames in the current directory and complete long listing of the files will be complete long listing of the files will be the output.the output.

ls –l ab?ls –l ab? This will match all filenames This will match all filenames of three characters starting with ‘ab’.of three characters starting with ‘ab’.

ls –l c*ls –l c* This will match all names This will match all names starting with c followed by zero or more starting with c followed by zero or more number of characters.number of characters.

Page 38: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:38

Examples

ls –x .???*ls –x .???* This will match all This will match all files starting with a dot and followed by files starting with a dot and followed by at least three characters. at least three characters. Example: .exrc, .profile, .cshrc, .mailrc , .Example: .exrc, .profile, .cshrc, .mailrc , .abc, the three characters followed by * abc, the three characters followed by * which matches any number of which matches any number of characters. characters.

echo * echo * This will display a list of This will display a list of all files in the current directoryall files in the current directory

Page 39: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:39

Examples

cp chap[0123] progs : This will cp chap[0123] progs : This will copy all files starting with ‘chap’ and copy all files starting with ‘chap’ and followed by any one of 0,1,2,3 into progs followed by any one of 0,1,2,3 into progs directory.directory.

ls –x chap[!012] : This lists all files ls –x chap[!012] : This lists all files starting with ‘chap’ and not followed by starting with ‘chap’ and not followed by 0,1,2.0,1,2.

cat emp[!0-9]: This concatenates all cat emp[!0-9]: This concatenates all files beginning with the string ‘emp’ and files beginning with the string ‘emp’ and followed by a non-numeric characters.followed by a non-numeric characters.

Page 40: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:40

Examples

ls [A-Z] *[0-9]: Matches any ls [A-Z] *[0-9]: Matches any filenames starting with a capital letter filenames starting with a capital letter and ending with a digit.and ending with a digit.

ls * [0-9][0-9]: Matches any ls * [0-9][0-9]: Matches any filename ending with two digits. Each filename ending with two digits. Each bracket pair represents just one bracket pair represents just one character place.character place.

Page 41: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:41

Redirection

Shell sets up these three standard files Shell sets up these three standard files (for input, output and error)(for input, output and error)

Shell attaches them to a user’s terminal Shell attaches them to a user’s terminal at the time of logging in.at the time of logging in.

Any program that uses streams will find Any program that uses streams will find them open and available. them open and available.

The shell also closes these files when the The shell also closes these files when the users logs out.users logs out.

Page 42: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:42

Standard inputStandard input :- The default source is :- The default source is the keyboard.the keyboard.

Standrad output:-Standrad output:- The default The default destination is the terminal.destination is the terminal.

Standrad error:-Standrad error:- The default The default destination is the terminal.destination is the terminal.

Page 43: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:43

Input and output redirection :

You can redirect the standard input You can redirect the standard input and output of a command with the and output of a command with the following syntax statements.following syntax statements.

< file < file Take input from ‘file’ Take input from ‘file’ rather than the standard input in rather than the standard input in overwrite mode.overwrite mode.

>file Send output not to the >file Send output not to the standard output, the terminal, but standard output, the terminal, but to the ‘file’ in overwrite mode. to the ‘file’ in overwrite mode.

Page 44: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:44

Command-line characters

\ \ Continue command on next Continue command on next line.line.

n>file Redirect the output of the n>file Redirect the output of the file descriptor ‘n’ to file. n is zero for file descriptor ‘n’ to file. n is zero for standard input,’1’ for standard standard input,’1’ for standard output and ‘2’ for standard error.output and ‘2’ for standard error.

1>&2 1>&2 Redirect standard output to Redirect standard output to standard error i:e merge standard standard error i:e merge standard output and standard error. output and standard error.

Page 45: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:45

Command-line characters

& & Run preceding Run preceding command in backgroundcommand in background

;; Command Terminator, Command Terminator, Enter is another command Enter is another command terminator.terminator.

Page 46: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:46

Example: Redirection

cat < file1 > file2 : cat < file1 > file2 : Input can be Input can be taken from a file file1 other than standard taken from a file file1 other than standard input and send output to file file2 other input and send output to file file2 other than standard error or output on screen.than standard error or output on screen.

cp file1 file2 2>errmesg : Error, if any cp file1 file2 2>errmesg : Error, if any occurs, are not printed on the screen but occurs, are not printed on the screen but redirected to the file ‘errmesg’. The 2 is redirected to the file ‘errmesg’. The 2 is file descriptor associated with standard file descriptor associated with standard error. error.

Page 47: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:47

Standard error into standard output

cp file file1 2> &1: This mergescp file file1 2> &1: This merges

standard error into standard standard error into standard output.output.

Page 48: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:48

Command Grouping

Command grouping is used to send Command grouping is used to send the combined output of two the combined output of two separate commands to a file.separate commands to a file.

Example:Example:

$ (date; cat results ) > newfile$ (date; cat results ) > newfile

Page 49: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:49

Background Process

Any command typed on the command Any command typed on the command line suffixed by an line suffixed by an & & is run as a is run as a background process.background process.

The pid (Process identifier) of the The pid (Process identifier) of the submitted job is returned.submitted job is returned.

Example:Example:

$ cat file1 | lpr &This will pipe the contents of file1 to printer as a background job.

Page 50: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:50

Pipes

The shell uses pipes to connect input The shell uses pipes to connect input and output streams.and output streams.

No intermediate files need to be created.No intermediate files need to be created.

and data is passed serially.and data is passed serially. Special operator “|” is used as a Special operator “|” is used as a

connector between two commands.connector between two commands. The output of the command is input to The output of the command is input to

the command after pipe.the command after pipe.

Page 51: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:51

Example: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.

Page 52: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:52

Examples

Example:Example: who | sortwho | sort

Pipe connects the output of the who command directly to the input of the sort command.

Both commands in the pipeline run simultaneously.

Page 53: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page: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.

Page 54: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:54

Pipes only operate on stdout stream of Pipes only operate on stdout stream of commands. commands.

Any messages written to stderr will still Any messages written to stderr will still be written on the terminal screen unless be written on the terminal screen unless it is separately redirected.it is separately redirected.

By appending an ampersand (&) after By appending an ampersand (&) after the pipe character, one can combine the the pipe character, one can combine the standard error and standard output and standard error and standard output and send it to the standard input of the send it to the standard input of the program receiving the piped output.program receiving the piped output.

Page 55: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:55

Multistage Pipes

Multistage PipesMultistage Pipes : : Pipes can be used to Pipes can be used to connect more than just two commands connect more than just two commands together. together.

Multistage pipelines are common in Multistage pipelines are common in Linux. Linux.

Example:Example: Who | grep tty | wc –lWho | grep tty | wc –l The output from the The output from the whowho command is command is

processed by processed by grep grep command,command,

Page 56: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:56

It filters out all lines that do not contain It filters out all lines that do not contain the string “ttyp”. the string “ttyp”.

This output is ultimately piped into wc, This output is ultimately piped into wc, which counts the number of lines left which counts the number of lines left that corresponds to the number of that corresponds to the number of network users.network users.

The ability to connect small commands The ability to connect small commands together in this way, to form larger more together in this way, to form larger more powerful utilities, is one the most useful powerful utilities, is one the most useful aspects of Linux. aspects of Linux.

Page 57: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:57

Demetafying characters

Many a times it is required to use a shell Many a times it is required to use a shell

metacharacters literally rather than as a metacharacters literally rather than as a

metacharacters. metacharacters.

In this case metacharacters have to be In this case metacharacters have to be

stripped off their special meaning by stripped off their special meaning by

attaching demetafying characters.attaching demetafying characters.

Page 58: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:58

These are:These are:

\( Backslash) : It negates the special \( Backslash) : It negates the special properties of the single character it properties of the single character it precedes.precedes.

‘ ‘‘ ‘(Single quotes) : Demetafies any (Single quotes) : Demetafies any metacharacters enclosed within it.metacharacters enclosed within it.

“ ““ “(Double quotes): Demetafies all (Double quotes): Demetafies all enclosed metacharacters except $,; enclosed metacharacters except $,; and \ i.e: variable evaluation, command and \ i.e: variable evaluation, command substitution and backslash.substitution and backslash.

Page 59: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:59

Examples:

$ echo \? \$ \\ \[

? $ \ [ $ echo \* \? \** ? * $ a= ZENITH $ echo $a ZENITH $ echo “*$a*”*ZENITH*

Page 60: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:60

Examples

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

$ echo The average pay is \$1000$ echo The average pay is \$1000The average pay is $1000The average pay is $1000

$ echo “The average pay is $1000”$ echo “The average pay is $1000”

The average pay is 000The average pay is 000

Page 61: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:61

Command Substitution

In command substitution, command In command substitution, command output is used as an argument in the output is used as an argument in the command line when a command is command line when a command is enclosed with a pair of backquotes(`).enclosed with a pair of backquotes(`).

The shell executes the command and The shell executes the command and replaces the enclosed text with the replaces the enclosed text with the output of the command.output of the command.

Page 62: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:62

Example:

$ echo “ There are `who|wc –l` users $ echo “ There are `who|wc –l` users logged in”logged in”

There are 3 users logged inThere are 3 users logged in

$ echo the date today is `date`$ echo the date today is `date` The date today is Mon Jun 10 23:19:16 The date today is Mon Jun 10 23:19:16

IST 2003IST 2003

Page 63: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:63

SHELL Variables

Each shell has variable that can be Each shell has variable that can be assigned a value. assigned a value.

Bourne shell has three major types of Bourne shell has three major types of shell variables.shell variables. System/Environment variables User defined variables Positional variables

Page 64: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:64

System/Environment variables

A set of pre-defined variables that A set of pre-defined variables that govern the general operation of the govern the general operation of the environment.environment.

These are, as a convention defined in These are, as a convention defined in uppercase.uppercase.

To see the list of such variables :To see the list of such variables : $ set

Page 65: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:65

System Variables

A list of common system variables are:A list of common system variables are:

EXINITEXINIT HOMEHOME IFSIFS LOGNAMELOGNAME

MAILMAIL PATHPATH PS1PS1 PS2PS2

TERMTERM SHELL SHELL TZTZ

Page 66: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:66

EXINIT: It stores the initialization EXINIT: It stores the initialization instructions for the instructions for the exex and and vi vi editors.editors.

HOME: This stores the pathname of a HOME: This stores the pathname of a user’s home directory. On logging in, a user’s home directory. On logging in, a user is automatically placed in the home user is automatically placed in the home directory. To echo a variable usedirectory. To echo a variable use $ echo $HOME

/usr/James

Page 67: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:67

IFS: This is set to a list of characters IFS: This is set to a list of characters that are used to separate words in a that are used to separate words in a command line. This can be set to command line. This can be set to invisible characters like space, tabs, invisible characters like space, tabs, newlines or any other characters. newlines or any other characters. Default is white spaces.Default is white spaces.

LOGNAME: This lists the user’s login LOGNAME: This lists the user’s login name.name.

Page 68: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:68

MAIL: All mail addressed to the user is MAIL: All mail addressed to the user is placed in a directory : mail. The shell placed in a directory : mail. The shell periodically checks this directory and periodically checks this directory and notifies the user with a message, if mail notifies the user with a message, if mail arrives.arrives.

PATH: All alternate paths in a sequence PATH: All alternate paths in a sequence that can be searched to find the that can be searched to find the executable for any command or file are executable for any command or file are stored in it.stored in it.

Page 69: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:69

PS1: This stores the value of primary PS1: This stores the value of primary prompts like $ for Bourne shell, % for C prompts like $ for Bourne shell, % for C shell and so on.shell and so on.

PS2: This is the secondary prompt,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.

Page 70: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:70

TERM : It identifies your type of terminal.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’

Page 71: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:71

User defined variables

A user can define as many variables as A user can define as many variables as needed.needed.

These variables can be used without These variables can be used without type declarations or initialization.type declarations or initialization.

These variables are treated as string These variables are treated as string variables and automatically initializes variables and automatically initializes them to a null string.them to a null string.

To perform numeric operations, explicit To perform numeric operations, explicit commands have to be used. commands have to be used.

Page 72: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:72

Shell variables are assigned values using Shell variables are assigned values using the = assignment operator, but there the = assignment operator, but there should not be a white space on either should not be a white space on either side of it.side of it.

Example: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

Page 73: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:73

Positional Variables

Shell scripts also accept arguments from Shell scripts also accept arguments from

the command line.the command line.

This is implemented through positional This is implemented through positional

variables which are $0,$1,$2…….$9.variables which are $0,$1,$2…….$9.

$0 gives the name of the program.$0 gives the name of the program.

$1 to $9, the command line arguments.$1 to $9, the command line arguments.

Page 74: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:74

Positional Variables

There a major difference between other There a major difference between other variables and positional variables.variables and positional variables.

Positional parameters are assigned values Positional parameters are assigned values through command line arguments.through command line arguments.

Example:Example:

$ cat emp.sh$ cat emp.sh

echo “\program name : $0 number of arguments :echo “\program name : $0 number of arguments :$# “$# “

$ emp.sh manager director$ emp.sh manager director

Program name : emp.shProgram name : emp.sh

Number of arguments : 2Number of arguments : 2

Page 75: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:75

Comparison of BOURNE AND ‘C’ Shell

Bourne and ‘C’ Shells are two major Bourne and ‘C’ Shells are two major types of shells supported by mostly all types of shells supported by mostly all UNIX systems.UNIX systems.

These have much in common like the These have much in common like the redirection, filename expansion, redirection, filename expansion, command substitution features.command substitution features.

Some details like shell variables are Some details like shell variables are handled differently in ‘C’ shell.handled differently in ‘C’ shell.

Page 76: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:76

The shell script programming commands The shell script programming commands are different.are different.

C shell has additional features not available C shell has additional features not available in Bourne shell ie command in Bourne shell ie command history and history and aliasalias. .

An An aliasalias system is for setting up shorthand system is for setting up shorthand notations for commands. notations for commands.

HistoryHistory provides a more extensive job provides a more extensive job control system that allows processes to be control system that allows processes to be moved from background to foreground and moved from background to foreground and vice versa.vice versa.

Page 77: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:77

Aliases

Korn, Bash and C shell accept aliases. For C Korn, Bash and C shell accept aliases. For C shellshell

ExamplesExamples

% alias l ls –l% alias l ls –l Sets l as an alias for ls –l

% alias l wc ‘ls | wc –l’% alias l wc ‘ls | wc –l’ Quoting is necessary when using

characters which have special meaning for shell.

Page 78: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:78

Command History

You can store , edit and execute You can store , edit and execute previous commands by setting the size previous commands by setting the size of history list by using the following of history list by using the following command:command:

% set history =100% set history =100 This stores last 100 commands in

memory. As History the list is stored in

memory.

Page 79: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page: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.

Page 80: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:80

To run the previous commands specify To run the previous commands specify the event number or a string:the event number or a string:

!g!g Runs the last command Runs the last command beginning beginning with g.with g.

!!!! Repeats the last command.Repeats the last command. !5!5 Runs the event number 5.Runs the event number 5. !5:p!5:p Prints event number 5, without Prints event number 5, without

executing it.executing it.

Page 81: Copyrights© 2008 BVU Amplify DITM Basics of OS,UNIX/Linux and Shell Programming Page:1 Lesson-4 The Shell as Interpreter By Simi.

Copyrights© 2008 BVU Amplify DITM

Basics of OS,UNIX/Linux and Shell ProgrammingBasics of OS,UNIX/Linux and Shell Programming

Page:81

Thank YouThank You