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 execution • In UNIX the shell is not included in the kernel • It is a normal user process
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)
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
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)
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
Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •
Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •
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
Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •
Regular expression • The shell automatically expands the regular expressions • The regular expressions are replaced by the list of file name that satisfy the expressions
Regular expression (cont) > ls -l file* > ls file1 file2 Shell rc.conf myconf.txt > ls -l file1 file2
Regular expression (cont) > ls -l *conf* > ls file1 file2 Shell rc.conf myconf.txt > ls -l rc.conf myconf.txt
Regular expression (cont) > ls - l ‘*conf*’ > ls file1 file2 Shell rc.conf myconf.txt > ls - l ‘*conf*’
Regular expression (cont) > find – name *config* > ls file1 file2 Shell rc.conf myconf.txt > find – name
Regular expression (cont) > find - name ‘*config*’ > ls file1 file2 Shell rc.conf myconf.txt > find -name *config*
Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •
I/O redirection • Each process has 3 I/O standard channel: stdin stdout process stderr • Each channel can be redirected toward: • A file • Another channel using a pipe
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
Shell Characteristics • Completion • Regular expression • I/O redirection • Pipeline • History • Aliasing Process management • • Scripting Variables •
Pipe Definition stdin stdin stdout stdout proc proc stderr stderr • The connection between stdout-stdin is defined as pipe • It creates in the memory a communication channel between the two processes
I/O redirection using pipe • command1 | command2 • pipe between the two commands • Example: • ls -la | more
Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •
History : • shows the commands executed before • history: shows the history executes the n th command stored in the buffer • !n: !-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
C-shell: history example 25% cc -g prog.c 26% vi iop.c 27% cc prog.c iop.c 28% a.out word word2 • rm !$ rm word2 • !-1 a.out word word2 • !c cc prog.c iop.c • !25 cc -g prog.c • rm !* rm word word2
Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •
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
Examples • alias dir=ls • alias tgz=“tar czvf” • alias ll=“ ls – la – color ”
Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing • Process management • Scripting Variables •
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
batch command1 >command1 >command2 command2
Concurrent execution command1 > command1 & > command2 command2
Process interruption command1 CTRL-Z fg
Processes States • Foreground execution • The 3 channels connected to the terminal • Background execution • Without stdin • Blocked
Process state command Run Shell fg command & fg fg CTRL-Z Run Blocked bg bg
Processes management commands • jobs list the jobs the job is forced in background • bg %job-id the job is forced in foreground • fg %job-id
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
ps • ps allows to list the processes with their state • -e list all the processes • -f full listing • -l long listing % 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
End of a process • Is possible to force the end of a process: • kill -9 pid • kill -9 %job-id
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
nohup • Run a command immune to hangups, with output to a non-tty • Example: • nohup commmand > log.txt &
Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •
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)
Shell Script #!/bin/bash date who
Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting • Variables
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
shell variables • Most used: • home = login directory • path = programs directory • prompt = the shell prompt • pwd = current directory • status = the result of the last command
Environment • The environment of a process is composed of a list of couple: (variable , value) • export variable[=value] assign value to the variable • printenv [ variablename ] print the value of variablename • env print all the variable
Environment variable • HISTFILE • History file name • HOME • home directory LOGNAME • • username • PATH • SHELL • Used shell
Recommend
More recommend