Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview.

Similar presentations


Presentation on theme: "1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview."— Presentation transcript:

1

2 1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview

3 2 © 2001 John Urrutia. All rights reserved. Topics The command line Standard Input and Output Redirection Pipes Running in the Background F ilename Generation / Pathname Expansion

4 3 © 2001 John Urrutia. All rights reserved. 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

5 4 © 2001 John Urrutia. All rights reserved. Command-Line Syntax command …[arg n] Enter  [arg 1] 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 a dash ( – )

6 5 © 2001 John Urrutia. All rights reserved. Command-Line Syntax Multiple options can be included after the dash  ls –ld Or separated by a space with a dash  ls –l –d

7 6 © 2001 John Urrutia. All rights reserved. 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-UCntl-HCntl-W

8 7 © 2001 John Urrutia. All rights reserved. Processing the Command Line First word is command name New Line? save as part of command Progra m Exist? Display not found Execute Program Display prompt & wait No Yes No Yes

9 8 © 2001 John Urrutia. All rights reserved. Execution is a Process When a command is valid:  The Shell spawns a new process  WAIT for the process to complete  CONTINUE to accept new commands The Shell may then:

10 9 © 2001 John Urrutia. All rights reserved. Command Line errors [astro@linux1 astro]$ uraloon bash: uraloon: Permission denied [astro@linux1 astro]$ ruhere bash: ruhere: No such file or directory [astro@linux1 astro]$ execthis bash: execthis: command not found WOOF !

11 10 © 2001 John Urrutia. All rights reserved. Why Can’t I execthis [astro@linux1 astro]$ ls -l -rwxrwxrwx 1 astro astro 20 Sep 13 08:50 execthis [astro@linux1 astro]$ execthis bash: 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

12 11 © 2001 John Urrutia. All rights reserved. Topics The command line Standard Input and Output Redirection Pipes Running in the Background F ilename Generation / Pathname Expansion

13 12 © 2001 John Urrutia. All rights reserved. There are Three Streams Standard Input <<<<<<<<<<<<<<<<<<<<<<<<< Standard Output >>>>>>>>>>>>>>>>>>>>>>>>> Standard Error >>>>>>>>>>>>>>>>>>>>>>>>> (0) (1) (2)

14 13 © 2001 John Urrutia. All rights reserved. Standard Input (sub- in some instances) Is INPUT:  Gathered / Collected / Extracted Y E EE E S EY E SEY E S From a:  File / Keyboard / Device

15 14 © 2001 John Urrutia. All rights reserved. Standard Output Is OUTPUT:  Written / Displayed / Discarded To a:  File / Display / Device EY E SEY E S

16 15 © 2001 John Urrutia. All rights reserved. Standard Error Is OUTPUT:  Written / Displayed / Discarded To a:  File / Display / Device EY E SEY E S

17 16 © 2001 John Urrutia. All rights reserved. The Terminal Display Standard Output by default  Represented By the device file associated with your login terminal /dev/tty06

18 17 © 2001 John Urrutia. All rights reserved. The Terminal Keyboard Standard Input by default  Represented By the device file associated with your login terminal /dev/tty06

19 18 © 2001 John Urrutia. All rights reserved. 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

20 19 © 2001 John Urrutia. All rights reserved. Topics The command line Standard Input and Output Redirection Pipes Running in the Background F ilename Generation / Pathname Expansion

21 20 © 2001 John Urrutia. All rights reserved. Redirect Mr. Mason STDIN, STDOUT, or STDERR 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

22 21 © 2001 John Urrutia. All rights reserved. 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

23 22 © 2001 John Urrutia. All rights reserved. Redirect Syntaxes command [arguments] > filename  Redirects stdout to create or overwrite command [arguments] >> filename  Redirects stdout to create or append to 1> filename 1>> filename Note: The stream number (output)

24 23 © 2001 John Urrutia. All rights reserved. Redirect Syntaxes command [arguments] 2> filename  Redirects stderr to create or overwrite command [arguments] 2>> filename  Redirects stderr to create or append to 2> filename 2>> filename (error) Can stderr be redirected to somewhere other than stdout?

25 24 © 2001 John Urrutia. All rights reserved. Redirect Syntaxes command [arguments] < filename  Redirects stdin to be taken from filename command [arguments] << [delimiter]  Redirects stdin to be taken as inline data from the current command or file  The “here” document <0 << (input)

26 25 © 2001 John Urrutia. All rights reserved. To clobber or noclobber Redirection by default will overwrite an existing file without The noclobber variable can be set to prevent this.  set –C Shell issues an error message: bash: more: cannot overwirte existing file

27 26 © 2001 John Urrutia. All rights reserved. Topics The command line Standard Input and Output Redirection Pipes Running in the Background F ilename Generation / Pathname Expansion

28 27 © 2001 John Urrutia. All rights reserved. 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

29 28 © 2001 John Urrutia. All rights reserved. So, want me to clean your Pipes? cat x >tmpfile;more<tmpfile;rm tmpfile cat x | more command | command | command | …

30 29 © 2001 John Urrutia. All rights reserved. Examples who | wc -l ps | wc -l cat wordlist | tr a A who | tee temp who | sort > temp

31 30 © 2001 John Urrutia. All rights reserved. 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)

32 31 © 2001 John Urrutia. All rights reserved. Filters continued A filter:  reads input stream  transforms the data  writes an output stream  can read/write to any file or device

33 32 © 2001 John Urrutia. All rights reserved. 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

34 33 © 2001 John Urrutia. All rights reserved. What this???? …]$ ls | tee pot 1> dome …]$

35 34 © 2001 John Urrutia. All rights reserved. Topics The command line Standard Input and Output Redirection Pipes Running in the Background F ilename Generation / Pathname Expansion

36 35 © 2001 John Urrutia. All rights reserved. Foreground tasks Normally command line processes run in the foreground  When the command starts executing the shell waits (sleeps) until the command is complete

37 36 © 2001 John Urrutia. All rights reserved. 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

38 37 © 2001 John Urrutia. All rights reserved. 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.

39 38 © 2001 John Urrutia. All rights reserved. Background via Foreground …astud]$ls –l | lpr & [1] 14170 When the process completes [1]+ Done ls –l | lpr &

40 39 © 2001 John Urrutia. All rights reserved. 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.

41 40 © 2001 John Urrutia. All rights reserved. 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

42 41 © 2001 John Urrutia. All rights reserved. Topics The command line Standard Input and Output Redirection Pipes Running in the Background Filename Generation / Pathname Expansion

43 42 © 2001 John Urrutia. All rights reserved. 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)

44 43 © 2001 John Urrutia. All rights reserved. Filename Generation The shell provides filename generation Utilities never see the filenames generated by the shell

45 44 © 2001 John Urrutia. All rights reserved. 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

46 45 © 2001 John Urrutia. All rights reserved. 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

47 46 © 2001 John Urrutia. All rights reserved.  Splat!  ls *  ls *e*  ls *.*  ls.*

48 47 © 2001 John Urrutia. All rights reserved. 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

49 48 © 2001 John Urrutia. All rights reserved. ? Question?  ls ?  ls ?e?  ls ?.?  ls *.???

50 49 © 2001 John Urrutia. All rights reserved. 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

51 50 © 2001 John Urrutia. All rights reserved. Filename Generation [a12c4Fx] substitutes for one character within the brackets [a-c1-4F-X] substitutes for one character within the ranges inside the brackets

52 51 © 2001 John Urrutia. All rights reserved. [ ] Class anyone?  ls [123]  ls [a12c]e[fabulos]  echo [who-z]?.*  ls *.[0-9]?


Download ppt "1 © 2001 John Urrutia. All rights reserved. Chapter 5 The Shell Overview."

Similar presentations


Ads by Google