shell
play

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


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

  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

  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)

  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

  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)

  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

  7. Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •

  8. Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •

  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

  10. Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •

  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

  12. Regular expression (cont) > ls -l file* > ls file1 file2 Shell rc.conf myconf.txt > ls -l file1 file2

  13. Regular expression (cont) > ls -l *conf* > ls file1 file2 Shell rc.conf myconf.txt > ls -l rc.conf myconf.txt

  14. Regular expression (cont) > ls - l ‘*conf*’ > ls file1 file2 Shell rc.conf myconf.txt > ls - l ‘*conf*’

  15. Regular expression (cont) > find – name *config* > ls file1 file2 Shell rc.conf myconf.txt > find – name

  16. Regular expression (cont) > find - name ‘*config*’ > ls file1 file2 Shell rc.conf myconf.txt > find -name *config*

  17. Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •

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

  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

  20. Shell Characteristics • Completion • Regular expression • I/O redirection • Pipeline • History • Aliasing Process management • • Scripting Variables •

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

  22. I/O redirection using pipe • command1 | command2 • pipe between the two commands • Example: • ls -la | more

  23. Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •

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

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

  26. Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •

  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

  28. Examples • alias dir=ls • alias tgz=“tar czvf” • alias ll=“ ls – la – color ”

  29. Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing • Process management • Scripting Variables •

  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

  31. batch command1 >command1 >command2 command2

  32. Concurrent execution command1 > command1 & > command2 command2

  33. Process interruption command1 CTRL-Z fg

  34. Processes States • Foreground execution • The 3 channels connected to the terminal • Background execution • Without stdin • Blocked

  35. Process state command Run Shell fg command & fg fg CTRL-Z Run Blocked bg bg

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

  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

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

  39. End of a process • Is possible to force the end of a process: • kill -9 pid • kill -9 %job-id

  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

  41. nohup • Run a command immune to hangups, with output to a non-tty • Example: • nohup commmand > log.txt &

  42. Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting Variables •

  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)

  44. Shell Script #!/bin/bash date who

  45. Shell Characteristics • Completion • Regular expression • I/O redirection Pipeline • • History • Aliasing Process management • • Scripting • Variables

  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

  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

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

  49. Environment variable • HISTFILE • History file name • HOME • home directory LOGNAME • • username • PATH • SHELL • Used shell

Recommend


More recommend