510 likes | 607 Views
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
E N D
Chapter 5 The Shell Overview
Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion
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
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 ( – )
Command-Line Syntax • Multiple options can be included after the dash • ls –ld • Or separated by a space with a dash • ls –l –d
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
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
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
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
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
Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion
There are Three Streams • Standard Input (0) <<<<<<<<<<<<<<<<<<<<<<<<< • Standard Output (1) >>>>>>>>>>>>>>>>>>>>>>>>> • Standard Error (2) >>>>>>>>>>>>>>>>>>>>>>>>>
Standard Input (sub- in some instances) • Is INPUT: • Gathered / Collected / Extracted YES • From a: • File / Keyboard / Device YES
Standard Output • Is OUTPUT: • Written / Displayed / Discarded • To a: • File / Display / Device YES
Standard Error • Is OUTPUT: • Written / Displayed / Discarded • To a: • File / Display / Device YES
The Terminal Display • Standard Output by default • Represented By the device file associated with your login terminal /dev/tty06
The Terminal Keyboard • Standard Input by default • Represented By the device file associated with your login terminal /dev/tty06
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
Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion
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
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
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
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
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 <<
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
Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion
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
So, want me to clean your Pipes? • cat x >tmpfile;more<tmpfile;rm tmpfile • cat x | more command|command|command| …
Examples • who | wc -l • ps | wc -l • cat wordlist | tr a A • who | tee temp • who | sort > temp
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)
Filters continued • A filter: • reads input stream • transforms the data • writes an output stream • can read/write to any file or device
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
What this???? …]$ ls | tee pot 1> dome …]$
Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion
Foreground tasks • Normally command line processes run in the foreground • When the command starts executing the shell waits (sleeps) until the command is complete
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
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.
Background via Foreground • …astud]$ls –l | lpr &[1] 14170 • When the process completes[1]+ Done ls –l | lpr &
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.
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
Topics • The command line • Standard Input and Output • Redirection • Pipes • Running in the Background • Filename Generation / Pathname Expansion
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)
Filename Generation • The shell provides filename generation • Utilities never see the filenames generated by the shell
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
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
• Splat! • ls * • ls *e* • ls *.* • ls .*
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
? • Question? • ls ? • ls ?e? • ls ?.? • ls *.???
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
Filename Generation • [a12c4Fx]substitutes for one character within the brackets • [a-c1-4F-X]substitutes for one character within the ranges inside the brackets