Shell Emanuele Valea <valea@lirmm.fr> LIRMM CNRS / Universit - - PowerPoint PPT Presentation

shell
SMART_READER_LITE
LIVE PREVIEW

Shell Emanuele Valea <valea@lirmm.fr> LIRMM CNRS / Universit - - PowerPoint PPT Presentation

Shell Emanuele Valea <valea@lirmm.fr> LIRMM CNRS / Universit de Montpellier Shell It is the external layer of the Operating System It provides a communication link between the O.S. and user Interactive execution Script


slide-1
SLIDE 1

Shell

Emanuele Valea <valea@lirmm.fr> LIRMM CNRS / Université de Montpellier

slide-2
SLIDE 2

Shell

  • It is the external layer of the Operating System
  • It provides a communication link between the O.S.

and user

  • Interactive execution
  • Script execution
  • In UNIX the shell is not included in the kernel
  • It is a normal user process
slide-3
SLIDE 3

Shell execution

  • A shell can be activated:
  • At the login (specified in /etc/passwd)
  • starting from another shell
  • To terminate a shell:
  • exit
  • The character EOF (usually CTRL-d)
slide-4
SLIDE 4

Special characters

/ directors separator in a path ? any character * a sequence of any character ~ login directory ~user login directory of the user [ ] any character between [] { } any word between {} (separated by ,) ‘…’ does not expand regular expression

slide-5
SLIDE 5

Shells

  • Several shells:
  • Bourne shell (sh): the original shell
  • C-shell (csh): Berkeley shell
  • Korn shell (ksh): Bourne shell rewritten by the AT&T
  • Tahoe C-shell (tcsh): improved C-shell
  • Bourne again shell (bash)
slide-6
SLIDE 6

Shell configuration files

  • When the shell is started, it executes in the login directory

the configuration files:

  • .login: commands executed during the login
  • .bashrc: commands executed when an interactive shell

starts

  • .bash_profile: commands executed during the login
slide-7
SLIDE 7

Shell Characteristics

  • Completion
  • Regular expression
  • I/O redirection
  • Pipeline
  • History
  • Aliasing
  • Process management
  • Scripting
  • Variables
slide-8
SLIDE 8

Shell Characteristics

  • Completion
  • Regular expression
  • I/O redirection
  • Pipeline
  • History
  • Aliasing
  • Process management
  • Scripting
  • Variables
slide-9
SLIDE 9

Completion

  • Able to expand the file name thanks to the character

stored in the filec variable (TAB or ESC)

  • For the file name corresponding to the executable, the

shell searches in the directories specified in the PATH variable

  • For the generic files, the shell expand the file name in

the current directory

slide-10
SLIDE 10

Shell Characteristics

  • Completion
  • Regular expression
  • I/O redirection
  • Pipeline
  • History
  • Aliasing
  • Process management
  • Scripting
  • Variables
slide-11
SLIDE 11

Regular expression

  • The shell automatically expands the regular

expressions

  • The regular expressions are replaced by the list of

file name that satisfy the expressions

slide-12
SLIDE 12

Regular expression (cont)

> ls file1 file2 rc.conf myconf.txt > ls -l file* Shell > ls -l file1 file2

slide-13
SLIDE 13

Regular expression (cont)

> ls file1 file2 rc.conf myconf.txt > ls -l *conf* Shell > ls -l rc.conf myconf.txt

slide-14
SLIDE 14

Regular expression (cont)

> ls file1 file2 rc.conf myconf.txt > ls -l ‘*conf*’ Shell > ls -l ‘*conf*’

slide-15
SLIDE 15

Regular expression (cont)

> ls file1 file2 rc.conf myconf.txt > find –name *config* Shell > find –name

slide-16
SLIDE 16

Regular expression (cont)

> ls file1 file2 rc.conf myconf.txt > find -name ‘*config*’ Shell > find -name *config*

slide-17
SLIDE 17

Shell Characteristics

  • Completion
  • Regular expression
  • I/O redirection
  • Pipeline
  • History
  • Aliasing
  • Process management
  • Scripting
  • Variables
slide-18
SLIDE 18

process stdin stdout stderr

I/O redirection

  • Each process has 3 I/O standard channel:
  • Each channel can be redirected toward:
  • A file
  • Another channel using a pipe
slide-19
SLIDE 19

I/O redirection from/to file

  • command < file

stdin from file

  • command > file

stdout to file (deleted if exists)

  • command >> file

stdout appended to file

  • command <<HERE stdin from “here document”

text HERE

  • command 2> file stderr to file
  • command &> file stdout+stderr to file
slide-20
SLIDE 20

Shell Characteristics

  • Completion
  • Regular expression
  • I/O redirection
  • Pipeline
  • History
  • Aliasing
  • Process management
  • Scripting
  • Variables
slide-21
SLIDE 21

Pipe Definition

  • The connection between stdout-stdin is defined as

pipe

  • It creates in the memory a communication channel

between the two processes

proc stdin stdout stderr proc stdin stdout stderr

slide-22
SLIDE 22

I/O redirection using pipe

  • command1 | command2
  • pipe between the two commands
  • Example:
  • ls -la | more
slide-23
SLIDE 23

Shell Characteristics

  • Completion
  • Regular expression
  • I/O redirection
  • Pipeline
  • History
  • Aliasing
  • Process management
  • Scripting
  • Variables
slide-24
SLIDE 24

History

  • :

shows the commands executed before

  • history:

shows the history

  • !n:

executes the nth command stored in the buffer

  • !-n:

executes the n - last command

  • !$:

the last parameter of previous command

  • !*:

all the parameters of the previous command

  • !string:

the last command having the first characters of the name matching with string

slide-25
SLIDE 25

C-shell: history example

  • rm !$

rm word2

  • !-1

a.out word word2

  • !c

cc prog.c iop.c

  • !25

cc -g prog.c

  • rm !*

rm word word2

25% cc -g prog.c 26% vi iop.c 27% cc prog.c iop.c 28% a.out word word2

slide-26
SLIDE 26

Shell Characteristics

  • Completion
  • Regular expression
  • I/O redirection
  • Pipeline
  • History
  • Aliasing
  • Process management
  • Scripting
  • Variables
slide-27
SLIDE 27

Aliasing

  • Is possible to define commands with new names
  • List the defined alias:
  • alias
  • Defines an alias:
  • alias name=“val”
  • Delete an alias:
  • unalias name
slide-28
SLIDE 28

Examples

  • alias dir=ls
  • alias tgz=“tar czvf”
  • alias ll=“ls –la –color”
slide-29
SLIDE 29

Shell Characteristics

  • Completion
  • Regular expression
  • I/O redirection
  • Pipeline
  • History
  • Aliasing
  • Process management
  • Scripting
  • Variables
slide-30
SLIDE 30

Processes

  • Is possible executes more than one process at a time
  • From the shell is possible to execute commands in

two ways:

  • batch: the user can executes a new command only after

the end of the previous

  • concurrent: the user can executes a new command even

if the others are not terminated

slide-31
SLIDE 31

batch

command1 command2

>command1 >command2

slide-32
SLIDE 32

Concurrent execution

command1 command2

> command1 & > command2

slide-33
SLIDE 33

Process interruption

command1 CTRL-Z fg

slide-34
SLIDE 34

Processes States

  • Foreground execution
  • The 3 channels connected to the terminal
  • Background execution
  • Without stdin
  • Blocked
slide-35
SLIDE 35

Process state

Run fg Run bg Blocked Shell

command command & fg fg bg CTRL-Z

slide-36
SLIDE 36

Processes management commands

  • jobs

list the jobs

  • bg %job-id

the job is forced in background

  • fg %job-id

the job is forced in foreground

slide-37
SLIDE 37

Processes

  • For each process:
  • pid

process id

  • uid

user id of the user that executed the process

  • stime

time corresponding to the begin of the execution

  • ...
  • The command ps shows the processes
slide-38
SLIDE 38

% ps -l S UID PID PPID TTY TIME COMD R 2103 1728 1676 ttys0 0:00 ps S 0 1675 110 ttys0 0:00 telnetd S 2103 1676 1675 ttys0 0:00 -csh

ps

  • ps allows to list the processes with their state
  • -e list all the processes
  • -f full listing
  • -l long listing
slide-39
SLIDE 39

End of a process

  • Is possible to force the end of a process:
  • kill -9 pid
  • kill -9 %job-id
slide-40
SLIDE 40

Process timing

  • at time filename
  • Executes the program at the specified time
  • at -l
  • List submitted jobs
  • at -r [jobname]
  • Remove from the job from the queue
slide-41
SLIDE 41

nohup

  • Run a command immune to hangups, with output to

a non-tty

  • Example:
  • nohup commmand > log.txt &
slide-42
SLIDE 42

Shell Characteristics

  • Completion
  • Regular expression
  • I/O redirection
  • Pipeline
  • History
  • Aliasing
  • Process management
  • Scripting
  • Variables
slide-43
SLIDE 43

Commands file (script)

  • Is possible to store a list of commands in a file and

then execute them by calling the file

  • Indirect execution:
  • source <scriptname> <args>
  • Direct execution, by executing the script
  • The file must have the execution right
  • The first row of the script has to be equal to #! followed

by the name of the shell (absolute path)

slide-44
SLIDE 44

Shell Script

#!/bin/bash date who

slide-45
SLIDE 45

Shell Characteristics

  • Completion
  • Regular expression
  • I/O redirection
  • Pipeline
  • History
  • Aliasing
  • Process management
  • Scripting
  • Variables
slide-46
SLIDE 46

Variables

  • The shell has a set of environment variables
  • variable=value
  • Assign a value to the variable
  • set
  • Show the values of all the variables
  • echo $variablename
  • Show the value of variablename
slide-47
SLIDE 47

shell variables

  • Most used:
  • home = login directory
  • path = programs directory
  • prompt = the shell prompt
  • pwd = current directory
  • status = the result of the last command
slide-48
SLIDE 48

Environment

  • The environment of a process is composed of a list
  • f couple:

(variable , value)

  • export variable[=value]

assign value to the variable

  • printenv [ variablename ]

print the value of variablename

  • env

print all the variable

slide-49
SLIDE 49

Environment variable

  • HISTFILE
  • History file name
  • HOME
  • home directory
  • LOGNAME
  • username
  • PATH
  • SHELL
  • Used shell