Shells ppt

64
Shells

Transcript of Shells ppt

Page 1: Shells ppt

Shells

Page 2: Shells ppt

What is Shell?

• The shell is a program used to interface btw user and linux kernel.

• The shell is a command language interpreter , has its own set of built in shell commands , makes use of all Linux utilities & application programs available on the system.

Page 3: Shells ppt

The relationship between the user and the shell.

Page 4: Shells ppt

Command interpretation by the shell.

The cp and rm are separate executable programs that exist in one of the directories in the filesystem.

Page 5: Shells ppt

How the shell gets started?

• The shell is started after you successfully log into the system & it continues to be the main method of interaction between the user and the kernel until you logout.

• Each user on your system has a default shell, which is specified in the system password file called /etc/passwd

• System password file contains each person’s user ID, an encrypted copy of each user password & the name of the program to run after a user logs into the system which is mainly one of th linux shells

Page 6: Shells ppt

The Most Common Shells

• The Bourne shell ( called sh)

• The C shell ( called csh)

• The Korn shell ( called Ksh)

Page 7: Shells ppt

Comparison of Shells

Bourne shell C shell Korn shell

written by Steven Bourne. Bill Joy Dave Korn

User interaction Does not handle Much more responsive

Very interactive

Supports No such features available

Several features like command-line completion

Has features of Bourne & C shell

Programming interface

Good Not as good as Bourne shell

Good

Drawback The way it handles user input ( typing )

Page 8: Shells ppt

Three New shells

• The Bourne Again Shell ( bash)

• The Public Domain Korn Shell(pdksh)

• The tcsh

Page 9: Shells ppt

Comparison of shells

bash pdksh TcshExtesion of The Bourne

shellThe Korn shell The C shell

Fully backward compatible

Yes , with Bourne shell

Does not support all features of Korn shell

Yes with csh

Key binding feature

Not available Available Available

Customization bash initialization file

pdksh initialization file

Requires 2 files

1. login file

2. cshrc file

Spell correction feature

Not available Not available Available

Page 10: Shells ppt

Features bash pdksh tcsh

Command completion

cd t<tab>

Press enter

vi s\

Press enter

lpr s

Press enter

Wild cards *, ? , […]

( cd, lpr)

We use these with vi editor

Same

Command history

.bash_history

history , fc

.ksh_history

Fc

.history

history

Input redirection Same Same Same

O/p redirection Same Same Same

Pipelines Same Same Same

Prompts 2 levels 3 levels 3 levels

Job control Same Same Same

Key bindings Not available Available Available

Spelling correction

Not available Not available Available

Page 11: Shells ppt

Feature bash pdksh tcsh

Variables Same Same Different

Commands Same Same Some are different

By default command line completion

It does have Does not have default command completion

It does have

history

command

history command exists

No history command exist, an alias is made

history command exists

Page 12: Shells ppt

The Bourne Again shell1. The COMMAND-LINE COMPLETION

Page 13: Shells ppt
Page 14: Shells ppt

2. WILD CARDS (* , ? , […])

Page 15: Shells ppt
Page 16: Shells ppt
Page 17: Shells ppt
Page 18: Shells ppt

3. COMMAND HISTORY

Bash stores previous commands in the history list .

(a) Use of up and down arrow keys

Commands displayed on the command line through the history list

can be edited simply by right and left arrow keys and delete and

backspace keys

(b) Display n edit the hisory list using history and fc ( fix commands) commands built into bash

b.1 history // displays list of 1000 previous commands

b.2 history [n] // n can be any integer

// only last n lines in the history list is to be shown

eg history [20]

Page 19: Shells ppt

b.3 history [-r|w|a|n] [filename] where

-r : read contents of history file & use them as current history list -w : write the current history list to the history file ( overwrite what is

currently in the file ) -a : appens the current history list to the end of history file -n : causes the lines in the history file to be read into the current history list

Note ; if no filename is specified , the history command will use the value of the HISTFILE shell variable .

b.4 fc [-e editor_name] [-n] [-l] [-r] [first] [last]

all the options given are optional -e: used to specify the text editor to be used for editing the commands

Page 20: Shells ppt

first , last : used to select a range of commands to take out of the history list , which canbe a string or a number

-n : used to supress command numbers when listing history commands

-r : lists the matched options in reverse order

-l : lists the matched commands to the screen

Note1: when –l option is not used , the matching command will be loaded into the text editor .

Note2: The text editor used by fc is found by taking the value of editor name if the -e editor name option is used. If this option was not used, fc uses the editor specified by the variable FCEDIT. If this variable does not exist, fc will use the value of the EDITOR variable. Finally, if none of these variables exists, the editor that will be chosen is vi, by default.

Page 21: Shells ppt

4. Aliases

Command aliases are commands that the user can specify. Alias

commands are usually abbreviations of other commands, designed to

save keystrokes.

Now, until you exit from bash, the goconfig command will cause the

original, longer command to be executed as if you had just typed it.

unalias command to delete the alias:

Page 22: Shells ppt

Examples of aliases are: • alias ll='ls -l' • alias log='logout' • alias ls='ls -F' • alias dir='ls' • alias copy='cp' • alias rename='mv' • alias md='mkdir' • alias rd='rmdir‘

If you enter the alias command without any arguments, it will display all ofthe aliases that are already defined on-screen.

alias dir='ls‘alias ll='ls -l'alias ls='ls -F'alias md='mkdir'alias net='term < /dev/modem > /dev/modem 2> /dev/null&'alias rd='rmdir'

Page 23: Shells ppt

5. Input redirection

Input redirection is used to change the source of input for a command

Eg 1.

the rm command requires arguments on the command line. You must

tell rm the files that you want it to delete on the command line, or it will

issue a prompt telling you to enter rm -h for help.

Eg 2.

the wc (word count) command counts the number of characters,

words, and lines in the input that was given to it. If you just type wc

<enter> at the command line, wc waits for you to tell it what it should be

counting.

the wc command is collecting input for itself.

If you press Ctrl-D, the results of the wc command will be written to the

screen.

Page 24: Shells ppt

(A) If you enter the wc command with a filename as an argument,it will

return the number of characters, words, and lines that are contained in

that file:

Eg. wc test

11 2 1

(B)To pass the contents of the test file to wc is to redirect the input of the wc command from the terminal to the test file. This will result in the same output. The < symbol is used by bash to mean "redirect the input to the current command from the specified file."

So, redirecting wc input from the terminal to the test file can be done by entering the following command:

Eg. wc < test

11 2 1

Page 25: Shells ppt

6. Output Redirection

Output redirection is more commonly used than input redirection.

Output redirection enables you to redirect the output from a command

into a file

the > symbol is used.

you can redirect the output of the ls command into a file named

directory.out using the following command:

ls > directory.out

Page 26: Shells ppt

7. Pipelines

(A) Pipelines are a way to string together a series of commands.output from the first command in the pipeline is used as the input to thesecond command in the pipeline.The output from the last command in the pipeline is the output that you will actually see displayed on-screen

(B) two or more commands separated by the vertical bar or pipe character, |.

Things to do today:Low: Go grocery shoppingHigh: Return movieHigh: Clear level 3 in Alien vs. PredatorMedium: Pick up clothes from dry cleaner

cat sample.text | grep "High" | wc -l

2

Page 27: Shells ppt

8. Prompts

bash has two levels of user prompt.

The first level is what you see when bash is waiting for a command to be typed.

The default first-level prompt is the $ character.

To customize your default first level prompt, set the value of the PS1 bash variable.

PS1="Please enter a command“

sets the bash shell prompt to the specified string.

Page 28: Shells ppt

The second level of prompt is displayed when bash is expecting more input from you in

order to complete a command.

The default for the second level prompt is >.

To customize your default second level prompt , set the value of the PS2 variable, as in

PS2="I need more information"

Page 29: Shells ppt

. Prompt special character codes.

Character Meaning \! Displays the history number of this command. \# Displays the command number of the current command. \$ Displays a $ in the prompt unless the user is root. When

the user is root, it displays a #. \\ Displays a backslash. \d Displays the current date. \h Displays the host name of the computer on which the

shell is running. \n Prints a newline character. This will cause the prompt to

span more than one line. \nnn Displays the character that corresponds to the octal value

of the number nnn. \s The name of the shell that is running. \t Displays the current time. \u Displays the username of the current user. \W Displays the base name of the current working directory. \w Displays the current working directory.

Page 30: Shells ppt

9. Job control

Job control refers to the ability to control the execution behavior of a

currently running process.

Specifically, you can suspend a running process and cause it to

resume running at a later time.

bash keeps track of all of the processes that it started (as a result of

user input), and you can suspend a running process or restart a

suspended one at any time during the life of that process.

Pressing Ctrl-Z suspends a running process.

Page 31: Shells ppt

The bg command restarts a suspended process in the Background

The fg command restarts a process in the foreground.

When a command is started in the foreground, it locks the shell from any further user interaction until the command completes execution

if you started this command in the foreground, find / -name "test" > find.out which will scan the entire filesystem for files named test and store the results in a file called find.outIf u wanted to continue executing in the background so you could use the system again, you would enter the following: control-zbg

This would first suspend the find command, then restart it in the background. The find command would continue to execute, and you would have bash back again.

Page 32: Shells ppt

Customising bash Until now, the changes that you made affected only the current bash session. As soon as you quit, all of the customizations that you made will be lost.

You can make the customizations more permanent by storing them in bash initialization file.

You can put any commands each time bash is started into this initialization file.

Commands that are typically found in this file are alias commands and variable initializations.

The bash initialization file is named profile. Each user who uses bash has a .profile file in his home directory. This file is read by bash each time it starts, and all of the commands contained within it are executed. .

Page 33: Shells ppt

bash Commands • alias• bg• cd• exit• export: Causes the value of a variable to be made visible to all • subprocesses that belong to the current shell. • fc• fg: Foreground command. • help: Displays help information for bash built-in commands. • history• kill. • pwd• unalias

Page 34: Shells ppt

bash VariablesEDITOR, FCEDIT: The default editor for the fc bash command. HISTFILE: The file used to store the command history. HISTSIZE: The size of the history list. HOME: The HOME directory of the current user OLDPWD: The previous working directory (the one that was current

before the current directory was entered). PATH: The search path that bash uses when looking for executable

files PS1: The first-level prompt that is displayed on the command line PS2: The second-level prompt that is displayed when a command is

expecting more input PWD: The current working directory SECONDS: The number of seconds that have elapsed since the

current bash session was started

Page 35: Shells ppt

pdksh1. COMMAND LINE COMPLETION pdksh does not default to allowing the user to perform command-line completion. You must enter a command to tell pdksh that you want to be able to use command-line completion. In order to enable command-line completion, enter one of the following commands: set -o emacs

OR set -o vi

This causes pdksh to accept command editing that is similar to emacs or vi.

Page 36: Shells ppt

you can perform command-line completion by pressing the Esc key

twice (when using emacs command-line editing), or by pressing \

(when using vi command-line editing).

If the sample.text file is not the only file in the directory that begins with the

letter s, pdksh completes the command as far as it can and then beeps,

indicating that it needs more information to complete the command

Page 37: Shells ppt

2. Wildcards ( * , ? ,[…])

Page 38: Shells ppt

.

Page 39: Shells ppt

3. COMMAND HISTORY

(a)

Emacs

Scroll up by ctrl-p

Scroll down by ctrl-n

Vi

Scroll up by k or –key

Scroll down by j or +

In vi editor , u enter command mode by pressing esc key

Page 40: Shells ppt

(B) Using history file is to display and edit it using fc (fix command), the built-in pdksh shell command.

The history command was left out of the pdksh shell because all of its functionality could be provided by the fc command.

An alias to the fc -l command is history which is used in pdksh

The fc command is used to edit the command history. It has a number of options,

fc [-e ename] [-nlr] [first] [last]

All options given in braces are optional. -e : used to specify the text editor that is to be used for editing the commands in the command history.

Page 41: Shells ppt

first , last : used to select a range of commands to take out of the history list , which canbe a string or a number

-n :used to suppress command numbers when listing the history commands that matched the specified range.

-r:lists the matched commands in reverse order. -l :lists the matched commands to the screen.

Note1: In all cases except for the -l option, the matching commands are loaded into a text editor.

The text editor used by fc is found by taking the value of ename if the –e option was used. If this option was not used, fc uses the editor specified by the variable FCEDIT. If this variable does not exist, fc uses the value of the EDITOR variable. Finally, if none of these variables exists, the editor chosen is vi.

Page 42: Shells ppt

If you enter the fc command with no arguments, it loads the last command that was entered into the editor. fc loads the last command into the default editor.

fc -l lists the last 16 commands that were entered.

fc -l 5 10 lists the commands with history numbers between 5 and 10, inclusive.

fc 6 loads history command number 6 into the default editor.

fc mo loads into the default editor the most recent command that starts with the string mo.

Page 43: Shells ppt

4. Aliases

vi things-to-do-today.txt

alias ttd='vi things-to-do-today.txt‘

unalias ttd

Page 44: Shells ppt

5. Input Redirection

6. Output Redirection

7. Pipelines

Page 45: Shells ppt

8. Shell Prompts

pdksh has three levels of user prompts.

The first level is what the user sees when the shell is waiting for a command to be typed.

The default prompt is the $ character. You can customize it by setting the value of the PS1 pdksh variable. PS1="! Tell me what to do"

The pdksh shell keeps track of how many commands have been entered since it was started. This number is stored into the shell variable called !. Whenyou include the ! in the prompt, it displays the current command number in the prompt. The previous prompt command causes the command number followed by the string Tell me what to do to be displayed on the command line each time pdksh is expecting you to enter a command.

Page 46: Shells ppt

The second level of prompt is displayed when pdksh is expecting more input from you in order to complete a command.

The default for the second level prompt is >.

You can cutomize it by setting the value of the PS2 pdksh variable.PS2=" I need more information"

pdksh does not support the advanced prompt options that bash supports.

There is not a predefined set of escape codes that you can put in a pdksh prompt variable to display such items as the time or current working directory. You can, however, put other pdksh variables into a prompt variable. For example, the following two prompts are valid:

PS1="(LOGNAME) "

PS1='($PW 'The first example causes your prompt to be equal to your UNIX user name. The second example causes your prompt to be the current working directory.

Page 47: Shells ppt

9. Job control

10. Key bindings

This feature enables you to change the behavior of key combinations for the purpose of command-line editing.

bind <key sequence> <command> // emacs

This binding is typically found in your .kshrc file, which is the startup file for the shell (it is read whenever the shell starts). The bind commands that are needed to create these bindings are as follows: bind '^[['=prefix-2bind '^XA'=up-historybind "^XB'=down-historybind '^XC'=forward-charbind '^XD'=backward-char

Page 48: Shells ppt

list gives some of the most useful editing commands that you can use for binding keys, along with the default binding

bind : get a listing of all of the editing commands that pdksh supports

abort (^G) is used to abort another editing command. It is most commonly used to stop a history list search.

backward-char (^B) moves the cursor backward one character. This command is often bound to the left arrow key.

backward-word (^[b) moves the cursor backward to the beginning of a word.

beginning-of-line (^A) moves the cursor to the beginning of the command line. complete (^[^[) tells pdksh to try to complete the current command.

copy-last-arg (^[_) causes the last word of the previous command to be inserted at the cursor position.

Page 49: Shells ppt

delete-char-backward (ERASE) deletes the character that is to the left of the cursor.

delete-char-forward deletes the character to the right of the cursor. delete-word-backward (^[ERASE) deletes the characters to the left ofthe cursor back to the first white space character that is encountered.

delete-word-forward (^[ deletes the characters to the right of the cursor up to the first character that occurs after a whitespace character.

down-history (^N) moves down one line in the history list. This command is often bound to the down arrow key.

end-of-line (^E) moves the cursor to the end of the current line.

forward-char (^F) moves the cursor forward one character. This command is often bound to the right arrow key.

Page 50: Shells ppt

forward-word (^[F) moves the cursor forward to the end of a word.

kill-line (KILL) deletes the current line.

kill-to-eol (^K) deletes all of the characters to the right of the cursor on the current line.

list (^[?) causes pdksh to list all of the possible command names or filenames that can complete the word in which the cursor is currently contained.

search-history (^R) searches the history list backward for the first command that contains the inputted characters.

transpose-chars (^T) exchanges the two characters on either side of the cursor. If the cursor is at the end of the command line it switches the last two characters on the line.

up-history (^P) moves up one command in the history list. This command is often bound to the up arrow key.

Page 51: Shells ppt

Cutomization of pdksh

export ENV=$HOME/.kshrc

EDITOR=emacs

Page 52: Shells ppt

The tcsh

1.Command completion

emacs hello // this is how we search the file in current directory in tcsh

emacs /urs/bin/hello // will search this file in bin directory

// we generally do what is mentioned above

Page 53: Shells ppt

With command completion , we can do as :

Page 54: Shells ppt

2. Wildcards ( * , ? , […])

Page 55: Shells ppt

3. Command history

The shell remembers the last history commands that have been

entered into the shell (where history is a user-definable tcsh

variable).

The default filename for the history file is .history, but you can change

it using the histfile tcsh variable. This file is located in your home

directory.

(a) Use of up and down arrow keys

Commands displayed on the command line through the history list

can be edited simply by right and left arrow keys and delete and

backspace keys

Page 56: Shells ppt

The history command can be invoked by any one of three different methods. The first method is :

history [-hr] [n]

It displays the history list to the screen. -n:used to specify the number of commands to display. If the n option is not used, the history command will display the entire history list.-h:causes history to remove the command numbers and timestamps that are usually present in the output of the history command. -r: tells history to display the commands in reverse order, starting with the most recent command. The following command displays the last five commands that were entered:

history 5

Page 57: Shells ppt

The second method is used to modify the contents of the history file or

the history list.

history -S | -L | -M [filename]

-S : writes the history list to a file.

-L :appends a history file to the current history list.

-M :merges the contents of the history file with the current history list

and sorts the resulting list by the timestamp contained with each command.

All of the options for the second form of the history command use the

filename option as the name of the history file. If no filename is specified, the

history command will use the value of the histfile variable. If the histfile variable

isn't set, it will use the ~/.history (home directory) file.

The history command using the -c option clears the current history list.

Page 58: Shells ppt

In addition to the history command and its options, tcsh also contains

many history navigation and editing commands. The following

commands are used to navigate through the history list:

• !n re-executes the command with the history number of n.

• !-n re-executes the command that is n commands from the end of the history list.

• !! re-executes the last command that was entered.

• !c re-executes the last command in the history list that begins with the letter c.

• !?c? re-executes the last command in the history list that contains the letter c.

Page 59: Shells ppt

Prompts

tcsh has three levels of user prompts.

The first-level prompt is what you see when tcsh is waiting for you to type a command. The default prompt is the % character.This prompt can be customized by assigning a new value to the prompt tcsh variable:

set prompt="%t$“ // change the first-level prompt to the current time followed by a dollar sign.

The second-level prompt is displayed when tcsh is waiting for input when in a while or for loop The default for the second-level prompt is %R?, where %R is a special character sequence that displays the status of the parser. You can change the second-level prompt by setting the value of the prompt2 tcsh variable. For example: set prompt2="?“ //changes the second-level prompt to a question

mark.

Page 60: Shells ppt

The third-level prompt is used when tcsh displays the corrected

command line when automatic spelling correction is in effect. This

prompt is set using the prompt3 variable, and it has a default value of CORRECT>%R (y|n|e)?.

tcsh supports special character codes in its prompt variables.

These codes are similar to the codes that bash supports in its

prompts. The main difference between the two is that the syntax for

using them is different.

Page 61: Shells ppt

tcsh prompt special character codes.

Character Code Meaning %/ Displays the current working directory. %h, %!, ! These codes all display the current history number. %t, %@ These codes both display the time of day. %n Displays the username. %d Displays the current day of the week. %w Displays the current month. %y Displays the current year.

The following is an example of setting the prompt variable:

set prompt="%h %/"

This command sets the prompt to display the history number of the current command, followed by the current working directory.

Page 62: Shells ppt

Key bindings

Like the pdksh, tcsh provides the ability to change and add key

bindings.

The tcsh implementation of key bindings is more powerful than

the way key bindings are done in pdksh.

With tcsh you can bind to things other than the built-in editor

commands.

tcsh also enables you to bind vi editing commands, whereas

pdksh only allows the binding of emacs editing commands.

Page 63: Shells ppt

Key bindings can be very useful, especially if you're using a favoriteeditor other than emacs or vi. The basic syntax for defining key bindings is

bindkey [option] <instring or keyname> <outstring or command>

The most useful editing commands you can use to bind key sequences to, along with the default key binding for that command.

bindkey: list all the bindings that are defined in tcsh (without any arguments)

beginning-of-line (^A): Moves the cursor to the beginning of the command line. backward-char (^B): Moves the cursor back one character. end-of-line (^E): Moves the cursor to the end of the command line. forward-char (^F): Moves the cursor forward one character. backward-delete-char (^H): Deletes the character to the left of the cursor.

Page 64: Shells ppt

kill-line (^K): Deletes all of the characters to the right of the cursor.

clear-screen (^L): Removes all of the text from the shell window.

down-history (^N): Moves down one command in the history list.

up-history (^P): Moves up one command in the history list.

kill-whole-line (^U): Deletes all of the characters on the current line.

All of these commands are the same whether you're in emacs or vi

insert mode.

examples of setting key bindings:

bindkey ^W kill-whole-line

bindkey ^S beginning-of-line