1 / 51

Chapter 5

Chapter 5. The Shell Overview. Topics. The command line Standard Input and Output Redirection Pipes Running in the Background F ilename Generation / Pathname Expansion. The command line. The command line causes the shell to execute a program or a script. Programs: Utilities

wilmer
Télécharger la présentation

Chapter 5

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5 The Shell Overview

  2. Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion

  3. The command line • The command line causes the shell to execute a program or a script. • Programs: • Utilities • Applications • User-written programs • Scripts: • An ordered combination of Utilities/Applications/Programs

  4. Command-Line Syntax command [arg 1] …[arg n] Enter • command – the name of a program or shell script • arg1 – the first argument. Generally the command options.Arguments are usually optional Options are preceded with adash ( – )

  5. Command-Line Syntax • Multiple options can be included after the dash • ls –ld • Or separated by a space with a dash • ls –l –d

  6. Processing the Command Line • All keystrokes are stored in the command line buffer until the action key is pressed. • Line control characters modify only the buffer. Enter Cntl-H Cntl-U Cntl-W

  7. Processing the Command Line First word iscommand name Display prompt & wait No New Line? save aspart of command Yes Program Exist? No Execute Program Yes Display not found

  8. Execution is a Process • When a command is valid: • The Shell spawns a new process • The Shell may then: • WAIT for the process to complete • CONTINUE to accept new commands

  9. Command Line errors • [astro@linux1 astro]$ uraloon bash: uraloon: Permission denied • [astro@linux1 astro]$ ruherebash: ruhere: No such file or directory WOOF! • [astro@linux1 astro]$ execthisbash: execthis: command not found

  10. Why Can’t I execthis • [astro@linux1 astro]$ ls -l -rwxrwxrwx 1 astro astro 20 Sep 13 08:50 execthis • [astro@linux1 astro]$ execthisbash: execthis: command not found • [astro@linux1 astro]$ ls -l -rwxrwxrwx 1 astro astro 20 Sep 13 08:50 execthis • The Shell has tunnel vision! • It looks for commands only in the $PATH • [astro@linux1 astro]$ echo $PATH/usr/local/bin:/bin:/usr/bin:/home/astro/bin

  11. Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion

  12. There are Three Streams • Standard Input (0) <<<<<<<<<<<<<<<<<<<<<<<<< • Standard Output (1) >>>>>>>>>>>>>>>>>>>>>>>>> • Standard Error (2) >>>>>>>>>>>>>>>>>>>>>>>>>

  13. Standard Input (sub- in some instances) • Is INPUT: • Gathered / Collected / Extracted YES • From a: • File / Keyboard / Device YES

  14. Standard Output • Is OUTPUT: • Written / Displayed / Discarded • To a: • File / Display / Device YES

  15. Standard Error • Is OUTPUT: • Written / Displayed / Discarded • To a: • File / Display / Device YES

  16. The Terminal Display • Standard Output by default • Represented By the device file associated with your login terminal /dev/tty06

  17. The Terminal Keyboard • Standard Input by default • Represented By the device file associated with your login terminal /dev/tty06

  18. Can You Read & Write? • What device did login assign you? • tty – will echo the device file you entered the command from • You can read from or write to the device file directly. • STDIN, STDOUT, STDERR • Abstractions of the device file

  19. Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion

  20. Redirect Mr. Mason • Redirection is the process of changing temporarily one or more of:STDIN, STDOUT, or STDERR • Remains in effect until the next command line is processed

  21. The Linux/Unix Streams • All programs are connected to 3 Streams • 0 Standard Input (kb is default) • 1 Standard output (screen is default) • 2 Standard Error (screen is default) • Redirection refers to a way in which you can get the shell to alter these defaults

  22. Redirect Syntaxes (output) • command [arguments] > filename • Redirects stdout to create or overwrite 1>filename • command [arguments] >> filename • Redirects stdout to create or append to 1>>filename Note: The stream number

  23. Redirect Syntaxes (error) • command [arguments] 2> filename • Redirects stderr to create or overwrite • Can stderr be redirected to somewhere other than stdout? 2>filename • command [arguments] 2>> filename • Redirects stderr to create or append to 2>>filename

  24. Redirect Syntaxes (input) • command [arguments] < filename • Redirects stdin to be taken from filename <0 • command [arguments] << [delimiter] • Redirects stdin to be taken as inline data from the current command or file • The “here” document <<

  25. To clobber or noclobber • Redirection by default will overwrite an existing file without warning! warning! warning! warning! warning! warning! warning! warning! warning! warning! warning! warning! warning! warning! warning! warning! • The noclobber variable can be set to prevent this. • set –C • Shell issues an error message: bash: more: cannot overwirte existing file

  26. Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion

  27. So, want me to clean your Pipes? • A pipe is similar to redirecting the output of a command to a file, except that it redirects it to ANOTHER COMMAND • The vertical bar | is used to designate a pipe • The Shell establishes the connection • Data in the pipe may be buffered in memory • Commands connected with pipes are known as jobs

  28. So, want me to clean your Pipes? • cat x >tmpfile;more<tmpfile;rm tmpfile • cat x | more command|command|command| …

  29. Examples • who | wc -l • ps | wc -l • cat wordlist | tr a A • who | tee temp • who | sort > temp

  30. Filters • A filter is a command that expects input directly from the standard input and sends output to the standard output • ls is not one .. • It does not read data from standard in or a file. • wc however, is a filter (as are many of the commands we will talk about)

  31. Filters continued • A filter: • reads input stream • transforms the data • writes an output stream • can read/write to any file or device

  32. A spot of tee would be nice • The tee utiltity • Splits input into two outputs • One to a file • The other to stdout • Provides a copy …]$ ls | tee pot

  33. What this???? …]$ ls | tee pot 1> dome …]$

  34. Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion

  35. Foreground tasks • Normally command line processes run in the foreground • When the command starts executing the shell waits (sleeps) until the command is complete

  36. Background tasks • Command line processes run in the background • The shell spawns a new process and continues to accept commands • When the command finishes execution the shell displays the process number and the command line

  37. Background via Foreground • Commands and jobs can run in the foreground or the background • Use the & (ampersand) symbol to send a command to the background command & • The shell assigns a job number followed by the system process number.

  38. Background via Foreground • …astud]$ls –l | lpr &[1] 14170 • When the process completes[1]+ Done ls –l | lpr &

  39. Stop the world ! • Foreground jobs • Ctrl-Z – usually will suspend processing at the next command and frees keyboard • bg %1 – will send the foreground job to the background.

  40. Stop the world ! • Background jobs • fg %1 – will bring the background job to the foreground • kill %1 –will terminate the process • %1 can be either the job number or the process number

  41. Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion

  42. Formal Definition • Filename generation refers to the process of generating ambiguous file references • The process the shell performs on these filenames is called pathname expansion also referred as globbing (but only by Linux nerds who want you to think they are wonderful)

  43. Filename Generation • The shell provides filename generation • Utilities never see the filenames generated by the shell

  44. Filename Generation • Linux will use metacharacters to construct filename lists • metacharacters are single characters that have special meanings to the shell • There are 3 wildcard characters used to expand the filenames * ? [] • Ambiguous filename references

  45. Filename Generation • Asterisk ( *) wildcard substitutes for zero or more characters • Applies to all files except those that start with a period • Can be used in combination with other metacharacters

  46. • Splat! • ls * • ls *e* • ls *.* • ls .*

  47. Filename Generation • Question Mark ( ?) wildcard substitutes for one character • Applies to all files except those that start with a period • Can be used in combination with other metacharacters

  48. ? • Question? • ls ? • ls ?e? • ls ?.? • ls *.???

  49. Filename Generation • Character Class [ ]substitutes for one character within the brackets • Applies to all files except those that start with a period • Can be used in combination with other metacharacters • Individual characters or ranges of character can be included

  50. Filename Generation • [a12c4Fx]substitutes for one character within the brackets • [a-c1-4F-X]substitutes for one character within the ranges inside the brackets

More Related