+ All Categories
Home > Documents > Shells. A program that is an interface between a user at a terminal and the computers resouces...

Shells. A program that is an interface between a user at a terminal and the computers resouces...

Date post: 12-Jan-2016
Category:
Upload: colin-parrish
View: 216 times
Download: 0 times
Share this document with a friend
35
Shells
Transcript
Page 1: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Shells

Page 2: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

shell basics

Page 3: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Shells

•A program that is an interface between a user at a terminal and the computers resouces▫The terminal may be real or an emulator

•The shell is not a programming language▫However, most shells do support scripting

•The shell may:▫Process commands (a specific action)▫Process scripts (list of commands)

•Shells may be CLI or GUI

Page 4: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Shells

•Command Processor▫Commands typed on a terminal

Executed by the shell Boune family C family many others…

▫Note: examples will center on the bash shell bash – Bourne Again SHell Shells do vary on implementation of some

features

Page 5: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Shell preliminaries

Page 6: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Pattern Matching: Wild CardsFilenames Wild Card Examples

• Replace parts of names with arbitrary matching character(s)

• Combination of known and unspecifed characters▫ ?, *, […], {…}

Wild Card Matches

* any number of chars including none

? exactly one character

[aml] single character from list – either a, m, or l in this case

[a-l] single character – in the range a to l

[!a-l] single character – not in the range a to l

{ pat1, pat2…}

matches specifed patterns

Page 7: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Standard Files: Input/Ouput/Error• Standard input (std in)

▫ Where the data is coming from File Stream

▫ Usually the keyboard or terminal• Standard output (std out)

▫ Where the data is going to File Stream

▫ Usually the display or terminal• Standard error (std err)

▫ Where error messages are sent File Stream

▫ Usually the display or terminal Default is same as Standard output

Page 8: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Data Sources/Redirection• Important: In Unix/Linux everything is a file• Terminal

▫ Keyboard - source▫ Screen or printer – sink

• File redirection ▫ > and >>

Send the standard data to a file▫ <

Receive the standard data from a file• Piping

▫ | Send the output of one program to the input of another

• Different utilities and programs will handle defaults for std in and std out differently

Page 9: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Resume 8/27

Page 10: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Shell Basics

Page 11: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Shell Example – wc command• wc

▫ word count reads the input and counts and reports the number of:

lines words characters

http://www.unix.com/man-page/posix/1posix/wc/ • Examples of data source and sink using wc:

▫ No parameters: data will flow from std in: wcThis is a line of dataThis is another<ctrl-d>

2 9 34▫ File as a parameter

wc /dir/file8 50 1084 /dir/file

Note it has the filename in the output Can work on a list of files

▫ Redirection from a file wc < /dir/file

8 50 1084▫ Piped from another program

ls | wc 6 6 60

Page 12: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

File Descriptor• Standard files have numeric representations

▫0 – Standard input▫1 – Standard output▫2 – Standard error

• Example:▫#cat bad.filecat: bad.file: No such file or directory#cat bad.file 2> err.msg#wc err.msg 1 7 41 err.msg#more err.msgcat: bad.file: No such file or directory

Page 13: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Problem: want std out and std err to go to same destination

• Example: file1 exists, file2 does not▫ #cat file1 file2file 1 data cat: file2: No such file or directory

• redirect std out▫ #cat file1 file2 1> reportcat: file2: No such file or directory#more reportfile 1 data

• redirect std err▫ #cat file1 file2 2> reportfile 1 data#less reportcat: file2: No such file or directory

• cannot consistanly redirect both streams to the same file, need another method▫ Can use >> for one or replicating descriptors▫ May show order differently or not work at all

Page 14: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Replicating Descriptors

•1>&2▫send std out to destination of std err

•2>&1▫send std err to destination of std out

•redirect std err, std out▫#cat file1 file2 2> report 1>&2#less reportfile 1 data cat: file2: No such file or directory

Page 15: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Special files

•/dev/null▫“bit bucket”

•/dev/tty▫your default terminal▫useful when different users log on▫everyone can use /dev/tty as their terminal▫Remember: everything in Unix/Linix is a

file!

Page 16: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Pipes

•Send output of one command as the input to another command or program

•Examples▫#ls | wc

31 32 530 Sent the short directory listing to wc

▫#ls –l | wc32 282 2121

Sent the long directory listing to wc

Page 17: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Tee

•Sends data to std out and a file▫Not part of the shell

•Example:▫#who | tee users.txtajk tty72013-02-16 22:14 (:0)ajk pts/0 2013-03-01 08:45 (:0.0)#cat users.txtajk tty72013-02-16 22:14 (:0)ajk pts/0 2013-03-01 08:45 (:0.0)

Page 18: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Command substitution

• Command arguments can be obtained or substituted from the std out of a program:▫ Use backticks ` to denote

That is not the single quote ' AKA Accent Gravé

• Examples:▫ #echo This year is `date +%Y`This year is 2013

▫ #ls –la `cat filelist`ls: cannot access file2: No such file or directoryls: cannot access file4: No such file or directory-rw-r-r- 1 ajkombol ajkombol 14 Mar 3 16:34 file1-rw-r-r- 1 ajkombol ajkombol 14 Mar 3 16:34 file3

#cat filelistfile1 file2file3 file4

Page 19: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Shell variables• Can assign values to variables

▫myvar=myvalue Important: no spaces! Note: no $ is needed to assign Case sensitive

• Use by putting $ in front▫$myvar

• Example:▫#ext=.sh#name=doit#compname=$name$ext#echo $compnamedoit.sh

Page 20: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Scripts Intro

Page 21: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Scripts Intro

•Shells can execute groups of commands in a file▫Scripts

•Script basics▫They have basic control sequences

e.g. conditionals and looping if, for, while

▫They may contain commands▫They must have execute permission

Page 22: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Environment previewUsing the shell to modify your runtime environment

Page 23: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Shell variables - Environment• Two shell variable types:

▫Environment Used by a user in general

PATH HOME SHELL Etc…

Note: By convention environment variables are UPPER CASE

▫Local Used for specific purposes

Typically available to current process only

• Case sensitive

Page 24: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Local Variables

•Easy to create for temporary use▫my_var=value

•Restricted in scope

Page 25: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Environment Variables

•Export to create an Environmental Variable▫export ENV_VAR

•Can apply value at creation or later•Good for creating a set of common

definitions

Page 26: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

customizing the environmentUsing the shell

Page 27: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Setting the default shell• What shell is set as your default?

▫echo $SHELL▫Returns something like:

/bin/bash-or-

/usr/bin/bash▫Depends on distro

• Popular shells▫bash▫csh▫korn▫bourne

Page 28: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Environmental Variables• Available in the user’s total environment

▫Sub-shells Scripts they run Editors Mail commands etc.

• Local variables▫Only available to the current process

• set▫displays all variables in the current shell

• env▫shows only environment variables

Page 29: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

export

•Turns a local variable into an environmental variable▫Basically makes a variable visible to all

child processes▫Needs only to be done once for a session▫Needs to be redone every time a new

session is started•Form:

▫export ENV_VAR

Page 30: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Common Environment VariablesVariable Significance

HOME users home directory

PATH list of directories to search for a command

LOGNAME login name of user

USER login name of user

MAIL absolute pathname to users mail file

MAILCHECK how long to wait before checking for new mail

TERM type of terminal

PWD absolute name of users current directory

PS1 primary prompt string

PS2 secondary prompt string

SHELL users login shell

Page 31: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Aliases• Use alternate names

▫"Shorthand" for commonly used commands #alias vi='vim'

will always run vim when vi is typed can run vi with \vi

▫Assign common options #alias cp="cp -i"

will change cp to always be interactive▫Asks if duplicate names found

#alias cp will return the current assignment

#unalias cp will get rid of the alias

Page 32: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Command history

•Allows recalling previous commands▫Shows all the previous commands with event

numbers holds about 500 events

Will vary by distro

▫#history n shows last n command

▫#!n executes event n

▫#!n:p prints event n (does not execute)

Page 33: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Tilde

•Shorthand for the current users home directory

•Notes:▫#cd ~

changes to current user’s home directory▫#cd ~userid

changes to userid’s home directory

Page 34: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Using set

•By default, set shows all variables•Interesting options

▫#set –o noclobber prevents accidental overwriting of existing

files with redirection (the > and >> symbols)

▫#set –o ignoreeof prevents accidental termination of script

(<ctrl>d)▫#set –o notify

allows completed background jobs to notify immediately when done

Page 35: Shells. A program that is an interface between a user at a terminal and the computers resouces ▫The terminal may be real or an emulator The shell is.

Initialization scripts• login script

▫ runs once at log in▫ varies by distro

.bash_profile .profile .bash_login

▫ usually in users home directory• rc script

▫ runs every time an interactive sub-shell is created▫ varies by distro, usually has rc in its name

.bashrc• Used to customize the users environment

▫ The prompt is usually set in one of these


Recommended