Transcript Slide 1

Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• The Shell
• The agency that sits between the user and the UNIX system
• All the wonderful things that we can do with UNIX are possible
because the Shell does a lot of work on our behalf
• Shell looks for special symbols in the command line, perform the
tasks associated with them, and then executes the command.
• for example, it opens a file to save command ouput whenever it
sees the > symbol.
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• The Shell
• Unique and Multifaceted program
• It is a command interpreter and a programming language rolled into
one
• It is a process that creates an environment to work in
• focus is on the shell’s basic interpretive activities
• rm * or ls | more
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• The Shell as a Command Processor
• When you login to a UNIX machine, you first see a prompt
• A UNIX command starts running at the terminal when you login
• It starts functioning when we log in
• It withers away when we log out
• This command is the UNIX shell
• run the ps command (shows processes); you will see it running;
• $ ps
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• The Shell as a Command Processor
• When we key in a command, it goes as input to the shell
• The shell first scans the command line for metacharacters
• Metacharacters are special characters that mean nothing to the
command, but means something special to the shell
• echo date > date.sh
• rm *
• ls | more
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• The Shell as a Command Processor
• When the shell sees the metacharacters like the >, |, *, etc. in its
input, it translates these symbols to their respective actions before the
command is executed.
• It replaces the * with all filenames in the current directory so that rm
ultimately runs with these names as arguments.
• When the shell sees >, it opens the file date.sh and connects echo’s
output to it
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• The Shell as a Command Processor
• Basically the shell recreates the command line by removing all
metacharacters and finally passes on the command to the kernel for
execution
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• The Shell as a Command Processor
• Following activities are typically performed
• Shell issues a prompt and waits for you to enter a command
• It scans the command line for metacharacters and expands
abbreviations to create a simplified command line
• It then passes on the command line to the kernel for execution
• The shell waits for the command to complete and normally can’t
do any work while the command is running
• After the command execution is complete, the prompt reappears
and the shell returns to its waiting role to start the next cycle.
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Shell offerings
• UNIX system offers a variety of shells for you to choose from. The
shells we consider can be grouped into two categories:
• The Bourne family comprising the Bourne shell (/bin/sh) and
its derivatives – the Korn shell (/bin/ksh) and Bash (/bin/bash)
• The C Shell (/bin/csh) and its derivatives, Tcsh (/bin/tcsh)
• To know the shell you are using invoke $SHELL
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Pattern Matching – The Wild Cards
• The * and ?
• The * metacharacter is one of the characters of the shell’s
special set. It matches any number of characters including none.
When it is appended to the string chap, the chap* matches
filenames beginning with the string chap – including the file chap
• ls -l chap*
• echo *
• rm * -- bad news
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Pattern Matching – The Wild Cards
• The * and ?
• The ? Matches a single character.
• ls chap?
• chapx chapy chapz
• ls chap??
• chap01 chap02 chap03 chap04 chap15 chap16 chap17
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Pattern Matching – The Wild Cards
•The Character Class
• It comprise a set of characters enclosed by the brackets [ and ],
but it matches a single character in the class
• ls chap0[124]
• chap01 chap02 chap04
• ls chap0[1-4] range 1,2,3,4
• ls chap[x-z]
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Pattern Matching – The Wild Cards
• The Mystery of the find Command
• find / -name “*.[hH][tT][mM][lL]” –print
• will display html and HTML files
• find . –name “note??” –print
• wild cards in the feature of find and not the shell
• quotes around the pattern, ensures that the shell can’t even
interpret this pattern.
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Pattern Matching – The Wild Cards
• Matching the Dot
• The * and ? Don’t match all filenames beginning with a . (dot)
• to matche hidden files in your directory having at least 3
characters after the dot,
• ls .???*
• to match files with the dot anywhere but the beginning
• ls *c
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• What is a Terminal?
• a generic name that represents the screen, display or keyboard
(or even an X window that emulates a terminal)
• We see command output and error messages on the terminal
(display), and we sometimes provide command input through the
terminal (keyboard)
• The shell associates three files with the terminal
• two for display and one for keyboard
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• What is a Terminal?
• even though our terminal is also represented by a specific device
name, commands don’t usually read from or write to this file.
• All terminal related activity is performed with the three files that
the shell makes available to every command
• These files are actually streams of characters which many
commands see as input and ouput
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• What is a Terminal?
• stream is simply a sequence of bytes
• when a user logs in, shell makes available 3 files representing
three streams
• each stream is associated with a default device, and
generally speaking the device is the terminal
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• Standard Input
• The file (or stream) representing input, which is connected to the
keyboard
• Standard Output
• The file (or stream) representing output, which is connected to
the display
• Standard Error
• The file (or stream) representing error messages that emanate
from the command or shell.This is also connected to the display
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• A group of UNIX commands read and write to these files
• A command is designed to send output to this file not to the
terminal
• similarly, it is not designed to accept input from the keyboard, but
from the standard file which it sees as a stream
• All commands using streams will always find these files open and
available
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• Even though the shell associates each of the files with a default
physical device, can unhook a stream from its default device
• when it see some special characters in the command line
• we as users of UNIX have to instruct the shell to do that by using
symbols like > and < in the command line
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• Standard Input
• This file can represent three input sources
• The keyboard, the default source
• A file using redirection with the < symbol ( metacharacter)
• Another program using a pipeline
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• Standard Input
• wc without an argument and have no special symbols like < and |
in the command line; wc obtains its input from the default source
apache[kkhan:301] wc
Standard input can be redirected
It can come from a file
or a pipeline
3
14
71
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• Standard Input
• wc with the filename as argument
apache[kkhan:302] wc /etc/passwd
18
44 732 /etc/passwd
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• Standard Input
• wc with a character
apache[kkhan:302] wc < /etc/passwd
18
44 732
• On seeing the <, the shell open disk file. /etc/passwd, for reading
• It unplugs the standard input file from its default source and
assigns it to /etc/passwd
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.
Your UNIX: The Ultimate Guide
second edition
UNIX – The Shell
• Redirection
• Standard Input
• wc reads from standard input which has earlier been reassigned
by the shell to /etc/passwd
• wc has no idea where stream came from; it is not even aware
that shell had to open the file on its behalf!
• Input from both file and Standard Input
• cat – foo
• cat foo – bar
Das
© 2006 The McGraw-Hill Companies, Inc. All rights reserved.