220 likes | 354 Views
Advanced Unix Commands. Chapter 10 & 11. Shells. What is a shell? Bourne shell Developed by Steve Bourne at AT&T Korn shell Developed by David Korn at AT&T C-shell Developed by Bill Joy for Berkeley Unix EZ-shell Developed by somebody at UWM. How the shell works.
E N D
Advanced Unix Commands Chapter 10 & 11
Shells • What is a shell? • Bourne shell • Developed by Steve Bourne at AT&T • Korn shell • Developed by David Korn at AT&T • C-shell • Developed by Bill Joy for Berkeley Unix • EZ-shell • Developed by somebody at UWM
How the shell works • Shell displays a prompt • You type in a command • You press the return key • The shell interprets the commands you typed and tries to find the correct programs to run • The kernel runs the requested programs and returns the results to the shell • The shell displays the command prompt again
Standard input stdin The place the program normally looks for input. The keyboard. Standard output stdout The place where the program normally sends its output. The screen. Standard error stderr Used by programs to display error messages. Also the screen. Standard Input, Output and Error
Redirection <, >, >> • < • Redirects the standard input • [command] < [file name] • The command will open the file and use its content as its source of input
Redirection <, >, >> • > • Redirects the standard output • [command] > [file name] • The results of the command will be sent to the specified file • Will create or overwrite the destination file • cat june july aug > summer2000
Redirection <, >, >> • >> • Also redirects the standard output • [command] >> [file name] • The results of the command will be sent to the specified file • Will append the results of the command to the existing file
Wildcards • Typing in Unix can be tedious • Unix supports three wild-card characters: • Asterisk (*): matches any string of characters including blanks • Question mark (?): matches single characters • Square brackest ([]): Tells the shell to match any characters that appear inside the brackets • Quoting special characters
Grouping commands • Executing one command at at time can be tedious • Unix allows for grouping of commands by separating commands with a semi-colon (;) • pwd; cal 1 2000; date • Though they are all on the same line, this is still 3 commands
Pipes & Filters • You can construct powerful Unix command lines by combining several Unix commands • Unix commands alone are powerful, but when you combine them together, you can accomplish complex tasks with ease
| (pipe) • Similar to redirection and grouping combined • Used to link commands together • [command] | [command] etc. • The output of the first command is sent as the input to the second command, and so on, and so on … • who | more
Using a pipe • A pipe sends the standard output of the command to the left of the pipe to the standard input of the command to the right of the pipe • This is similar to the > symbol used to redirect the standard output of a command to a file • However, the pipe is different because it is used to pass the output of a command to another command, not a file
Using a filter • A filter is a Unix command that does some manipulation of the text of a file • Some simple filters include wc, sort & more • One of the most commonly used filters is grep
wc • word count • Used to display a word count of a file • wc [-c l w] [file name(s)] • The output you will see will be a line showing the number of lines, words and characters • Limit display with the flags
sort • Sorts the contents of a file • sort [-b f n r u] [file name(s)] • Takes the contents of a file and displays it in sorted order • Flags: • -b: ignores blanks • -f: folds upper- and lowercase letters together • -n: numeric sort • -r: reverse usual order • -u: prints duplicate entries only once
Here is an example: • alpha2: cat apple.txtcore worm seed jewelalpha2: cat apple.txt | wc 2 4 21alpha2: • After the first shell prompt, we see the contents of the file apple.txt • In the next shell prompt, the cat command displays the contents of the applex.txt file • The contents are displayed, not to the screen, but through a pipe to the wc (word count) command • The wc command then does its job and counts the lines, words, and characters of its input
grep • search for a string in a a file, display the line in which it appears • alpha2: cat apple.txtcore worm seed jewelalpha2: grep jewel apple.txtseed jewel • alpha2: cat apple.txt | grep jewel seed jewel
Job control • Unix works via jobs or processes • Every command or program is a separate job/process executed by a user • Jobs are usually run in the foreground, but can be made to run in the background • Jobs can be killed by the user who created them
Job control • ctrl-c: cancels a command/job • ctrl-z: suspends a command/job • jobs • Lists the jobs (programs) that you currently have running.
bg • Forces a job to the background • First, type a ctrl-z to suspend the job • Then type bg and the job is forced to the background • Use the jobs command to see it • You can force a job to the background immediately with the &
fg • Brings a job to the foreground • Use the jobs command to see the jobs you have running • Type fg %[number] and that job will be brought to the foreground
kill • Kills a job that you have running • Use the jobs command to see what you have running • Type kill %[number] • Not the most graceful way out, but it works