Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
15 views68 pages

TUG3e Slides

The document provides a comprehensive overview of UNIX/Linux operating systems, detailing the role of the operating system, how programs run, and the architecture of UNIX including the kernel and shell. It explains key concepts such as file types, command structure, and the hierarchical file system, along with command usage and permissions. Additionally, it covers the importance of the PATH variable and the use of the 'man' command for documentation.

Uploaded by

Mohammed Alyami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views68 pages

TUG3e Slides

The document provides a comprehensive overview of UNIX/Linux operating systems, detailing the role of the operating system, how programs run, and the architecture of UNIX including the kernel and shell. It explains key concepts such as file types, command structure, and the hierarchical file system, along with command usage and permissions. Additionally, it covers the importance of the PATH variable and the use of the 'man' command for documentation.

Uploaded by

Mohammed Alyami
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 68

Your UNIX/Linux

The Ultimate Guide


Third Edition

Sumitabha Das

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Why an Operating System (OS)
• OS interacts with hardware and manages
programs.
• Programs not expected to know which
hardware they will run on.
• Must be possible to change hardware without
changing the programs.
• Programs can’t manage themselves.
• OS provides a safe environment for programs
to run.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
How a Program Runs on a Computer
• OS loads program from disk and allocates
memory and CPU.
• Instructions in program are run on CPU, and OS
keeps track of last instruction executed.
• If program needs to access the hardware, OS
does the job on its behalf.
• OS saves the state of the program if program has
to leave CPU temporarily.
• OS cleans up memory and registers after process
has completed execution.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Running Multiple Programs
• Multiprogramming
– Multiple programs can be in memory.
• Multiuser
– Multiple users can run programs.
e.g. vi (or emacs) is actually using the CPU for a very
small fraction of time
• Multitasking
– One user can run multiple programs.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Key Concepts
• Everything in the system is represented as a file.
• Work gets done by processes.
• Files/Users have places and processes have life
• Workload is shared by two separate programs
(kernel and shell).
• Kernel uses system calls to do most of the work.
• All UNIX systems use the same system calls.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
UNIX Architecture: The Kernel
• Program always resides in memory.
• Has direct access to the hardware.
• Handles file I/O.
• Manages processes.
• Only one copy shared by all users.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
UNIX Architecture: The Shell
• A program or command invoked only when the
user logs in.
• Accepts user input, examines and rebuilds the
command line (interpreter before execution).
Cat foo*
• Makes calls to the kernel for all other functions.
• At least one shell is invoked by every user.
• User has a choice of shells.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
UNIX Fragmentation
• System V from AT&T (SVR4)
• BSD UNIX from Berkeley
• Linux with help from GNU

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Why UNIX Commands Are
Noninteractive
• A command may take input from the output of
another command.
• May be scheduled to run at specific times (e.g. at,
cron).
• User input can also be provided through
command line arguments.
• Command arguments need not be known in
advance.
• Allows designing of applications that determine
their own behavior by reading configuration files.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Structure of a Command
e.g. ls -l -u -t chap01
• Command filenames need no specific extensions.
• A command’s behavior is determined by its arguments
and options.
• Command and arguments must be separated by
whitespace.
• Generally possible to combine multiple options into a
single one
(like ls -l -u -t == ls -lut)
• Order of combining is generally not important (like ls -
lut == ls -utl)

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Types of Commands
• External program on disk which could be
– a binary executable (written in C, C++).
– a script file (like a shell or perl script).
• Internal command of the shell which could be
– a builtin (like cd, pwd, etc.)
– an alias defined by the user that invokes the disk
or internal version in a specific manner.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
How the Shell Determines the
Command to Run
• If command is invoked with a pathname (like
/bin/echo), the shell runs program at the
specified location.
• If command is invoked without a pathname, the
shell first checks whether it is an alias or builtin:
• If alias or builtin, the shell runs it without looking
in disk.
• If not, the shell looks at the PATH variable for
directories where the command may reside.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
The PATH
• A shell variable (or environment variable) that specifies
a list of directories to search.
• Shell looks at PATH only when command is not used
with a pathname and is also not a shell builtin.
• Command can still be executed if not in PATH by
– Using a pathname.
– Modifying PATH to include the directory containing the
command.
• PATH can be modified in an absolute or relative
manner:
PATH=/usr/bin:. (Absolute)
PATH=$PATH:/usr/local/bin (Relative)
• Modified setting is lost after user has logged out unless
saved in a startup file.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Flexibility of Command Usage
• Run multiple commands by specifying them in the same
line:
date ; echo $PATH
• Split a command into multiple lines:
$ echo “Hello
> Dolly”
• Save command output in a file:
date > foo
• Use output of one command as input of another:
date | cut -d” “ -f2
• Run a command in the background with &:
ls -lRa / > $HOME/ls-lRa.txt &
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Command Classification: A Different
Approach
• Utilities that are generally used in standalone
mode (vi, stty, bc).
• Commands that do useful work but produce no
output (mkdir, cp, rm).
• Commands that produce output which may need
further processing (date, who, ls).
• Commands specially designed to accept output of
other commands as their input and vice versa
(grep, sort, head).

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Using man
• Displays documentation of commands, configuration
files, system calls and library functions.
• Organized in a number of sections. Commands are
found in Section 1.
• May need to use section number when entry exists in
multiple sections (e.g. man passwd and man -s 5
passwd).
• man documentation not available for most internal
commands of the shell.
• Use man man first to know how man should be used.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Understanding a man Page
• Example: wc Syntax/Synopsis
wc [ -c | -m | -C ] [ -lw ] [ file ... ]
• Most useful information available in SYNOPSIS and
DESCRIPTION.
• When options grouped in [ ] without a |, one or more of
them can be used. (-l, -w and -lw are valid.)
• The | signifies an OR condition. (Only one of -c, -m or -C can
be used.)
• The ... means that multiple occurrences of the preceding
item are possible. (wc can be used with multiple files.)
• EXIT STATUS indicates values returned on error.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
The UNIX File
• Filename limited to 255 characters. Can’t
contain / or NULL character.
• Filenames are case-sensitive; chap and Chap
are two different filenames.
• Group of filenames held together in a
directory.
• Directory contains name of the file.
• Both files and directories are subject to access
control.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
File Types
• Ordinary or regular file
– Contains data. This file can be a
• text file (program sources, configuration files).
• binary file (executables, graphic and multimedia files).
• Directory
– Contains the filename and a number (inode number).
• Device file
– Contains no data whatsoever.
• Symbolic link
– Contains the location of another file.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
The Hierarchical Structure of the File
System
• A single hierarchical structure that contains all
files.
• Top signified by root (/).
• Existence of a parent-child relationship.
• Parent of any file must be a directory.
• Files accessed with pathnames (like
/etc/passwd).

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Pathnames: Two Types
• Absolute pathname
Specifies location with reference to the file
system top (like cat /etc/passwd).
• Relative pathname
Specifies location with reference to the user’s
current location (like cd ../include).
• Both commands and filename arguments can
be represented in either form.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Absolute Pathname
• Begins with a / (like /etc/passwd).
• First / signifies the root directory.
• System configuration files that normally don’t
change location should be addressed in
absolute manner.
• Used with a command that
– doesn’t feature in PATH.
– resides in two or more directories of PATH.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Relative Pathname
• Uses . to signify the current directory.
• Uses .. to signify the parent directory.
• Used to refer to files that are either impossible or
inconvenient to access in an absolute manner.
• Has a synonym for a filename argument that
doesn’t have a /. (cat foo is the same as cat
./foo.)
• Same synonym doesn’t automatically exist for
commands. (cat foo MAY NOT be the same as
./cat foo.)

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
The Home Directory
• Directory where user is placed on login.
• Determined by sixth field in /etc/passwd:
romeo:x:500:100:romeo
vincent:/home/romeo:/bin/bash
• Can also be referred to by
– the shell variable $HOME (e.g. cat $HOME/foo).
– tilde expansion in most shells: ~ (e.g. cat ~/foo).
• cd command used without arguments returns user to
home directory.
• User can create and remove files in their home
directory but not in other directories.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
File Attributes Stored in Inode
• Type Whether ordinary, directory, device, etc.
• Permissions Determines who can read, write or execute a file.
• Links Number of names a file can have. A program can be
designed to behave differently depending on the name by which it
is invoked.
• Owner A file is owned by a user, by default its creator. The owner
can change many file attributes and set the permissions.
• Group Owner The group which owns the file. The owner by
default belongs to this group.
• File Size Number of bytes of data contained.
• File Time Stamps
– Date and time of last modification
– Date and time of last access

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
File Permissions
• A file has three types of permissions (read, write and
execute).
• Available to three categories of users (user, group and
others).
• Only file owner or superuser can change file
permissions.
• Permissions can be assigned or removed in
– relative manner (e.g. chmod +x foo.sh).
– absolute manner (e.g. chmod 744 foo.sh).
• Significance of permissions different for file and
directory.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Directory Permissions
• Read permission
– Whether filenames in directory can be listed by a
program (like ls).
• Write permission
– Whether files and directories can be created in the
directory.
• Execute or search permission
– Whether one can pass through directory to search for
filenames.
• Desirable permission setting: 755
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
How a Directory Influences File Permissions

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
An Ownership-Permissions Problem
Assumption: romeo and juliet belong to the users group.

Ownership and Permissions of File foo and its Directory


$ who am i
romeo
$ ls -l foo
-r-x-w-r-x 1 juliet users 7017 2004-11-14 13:53 foo
$ ls -ld .
drwxr-xr-x 21 romeo users 8192 2004-11-28 11:40 .

Note: foo is owned by juliet but directory is owned by romeo.

juliet:
– can’t edit foo without changing the permissions.
– can change permissions (as owner) and then edit foo.
– can’t delete foo (directory write-protected for group).
romeo:
– can edit or delete foo.
– can’t change permissions of foo.
– can’t display or copy foo.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
The User Mask
• Reassigns default file and directory permissions.
• Default permissions before applying mask are
completely insecure:
– 666 for files
– 777 for directories
• Systemwide default changed by umask (a shell
builtin).
• umask statement placed in a startup script
(typically, /etc/profile).
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
File Systems
• System of organizing files into multiple
manageable units.
• Each file system has a separate directory
structure with at top.
• For a file to be visible, its file system must be
attached to the main file system.
• Two files in two file systems may have the same
inode number.
• Not easy to understand whether a directory
structure comprises multiple file systems.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
The Inode Revisited
• System of organizing file attributes separately
from content.
• Identified by inode number but inode doesn’t
contain this number.
• Inode number displayed by ls -i.
• Both inode and directory entries are looked
up by inode number.
• Possible to consume all inodes even when
there is adequate disk space.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Making a Program Behave Differently

• Using arguments and options.


• Manipulating a configuration file read by a
program on startup.
• Using different names for the same file.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Links or Hard Links
• Mechanism by which a file is allowed to have
multiple names.
• Linked filenames share inode but have
separate directory entries.
• Each link increments link count in inode by 1
and adds an entry to the directory.
• File considered to be deleted and inode freed
only when link count drops to 0.
• Linked filenames equivalent in all respects.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Advantages of Hard Links
• Backup
– Prevention from accidental deletion.
• Allows the same file to be executed as two
similar but separate programs.
• Takes care of old programs that accesses a file
whose name or location has changed.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Limitations of Hard Links

• Can’t link directories.


• Can’t link across file systems.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Symbolic Links or Symlinks
• Separate file type and having its own inode.
• Contains the pathname of another file or
directory.
• Can link across file systems.
• Link and file pointed to are not equivalent.
• Pathname may be stored either in inode or in
a separate file.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Understanding Ownership and Group Ownership
Entry for romeo in /etc/passwd:
romeo:x:1003:101::/export/home/romeo:/usr/bin/ksh
Entry for romeo’s group (101) in /etc/group:
users::101:
• A file has a UID (name and number).
• Both UID parameters maintained in /etc/passwd (1st and
3rd field).
• A file has a GID (name and number).
• Both GID parameters maintained in /etc/group (1st and 3rd
field).
• The numeric GID also maintained in /etc/passwd.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Ownership in SVR4 and BSD

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
The Shell
• Program that constantly runs at terminal after
a user has logged in.
• Interprets command line and makes
arrangements for its execution.
• Generally waits for command to complete
execution.
• Some commands are built into the shell.
• Killed on logging out.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
The Shell Metacharacters

• Wild-card characters like the *, ?, etc.


• Redirection characters like >, <, etc.
• The pipe character, |.
• Command substitution characters (` `).
• The $ as a variable prefix.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Examples of Shell Behavior
cat chap*
– Shell expands * to match all filenames in the current directory
that begin with chap.
date > foo
– Shell sees the > first, opens the file foo and connects the date
output to it.
who | sort
– Shell understands the strings on either side of the | as two
separate programs and connects them.
ls `cat foo`
– Shell first runs cat and supplies the output as arguments to ls.
echo $HOME
– Evaluates $HOME as a variable before running echo.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Wild Cards
• Set of metacharacters used in an expression to
match multiple but similar filenames.
• Shell creates list of filenames before allowing
command to run.
• Expansion can be prevented by quoting and
escaping.
• Feature also found in find and shell’s for and case
constructs.
• Filenames must not contain wild-card characters.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Wild-card Set
* Any number of characters including none
ls *.lst Lists all files with extension .lst.
? A single character
rm ??* Removes all files comprising at least 2
characters.
[ch] A single character that is either a c or h
cp *.[ch] cprogs Copies all files with .c or .h extension.
[!ch] A single character that is not a c or h
rm *[!a-zA-Z]* Removes files not containing at least one
letter.
ls .??* Lists all filenames beginning with a dot and
comprising at least two more
characters.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Escaping (Using a \ before a character)
• Reverses usual meaning of metacharacter
following it. (rm \* removes a file named *.)
• Can also protect itself. (echo \\ prints a \.)
• Protects space and [Enter]. (cd My\ Documents
will work.)
• Inconvenient to use when command line contains
too many metacharacters that need to be
escaped.
• Principle also used by commands in their
expressions. (grep “\.” foo looks for a dot in foo.)

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Quoting
• Protects most metacharacters from interpretation by
the shell. (echo “*” prints a *.)
• More convenient than escaping when protecting a
group of metacharacters.
• Quoted string understood as a single argument by
shell and C programs. (a.out foo “My Documents” has
2 arguments and not 3.)
• Double quotes and single quotes are not equivalent.
(echo “$SHELL” not the same as echo ‘$SHELL’)
• Quoting doesn’t protect the \; escaping is also
required.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Single Quotes or Double Quotes?
• Single quotes protect all characters except \.
(echo ‘\’ won’t work.)
• Double quotes protect all characters except
the \, $ and ` (echo “$” doesn’t print a $.)
• Single quotes protect the “.
• Double quotes protect the ‘.
• Double quotes permit variable evaluation and
command substitution.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
How the Shell Handles Files
• A file is opened by accessing it by its
pathname.
• Opening returns a file descriptor (an integer).
• Subsequent read/write operations on the file
use the descriptor.
• Kernel allocates lowest available number as
descriptor.
• First three descriptors (0, 1 and 2) are always
allocated.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Redirection
• Shell feature for manipulating command input
and output.
• Commands designed to write output to a generic
file.
• Commands designed to take input from a generic
file when used without a filename as argument.
• Shell can assign these files to disk files.
• Commands themselves have no knowledge of the
reassignment.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Standard Output
• Uses file descriptor 1.
• By default, assigned to the terminal.
• When command line contains > or >>,
standard output file reassigned to a disk file.
• Can also be used as input to another program.
• Commands continue to write to descriptor 1
and have no knowledge of this manipulation.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Standard Input
• Uses file descriptor 0.
• By default, assigned to the terminal.
• When command line contains <, standard
input file reassigned to a disk file.
• Can also be obtained from standard output of
another program.
• Commands continue to read descriptor 0 and
have no knowledge of this manipulation.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Standard Error
• Uses file descriptor 2.
• By default, file is assigned to the terminal.
• When command line contains 2>, standard
error file reassigned to a disk file.
• But standard error output can’t be used as
input to another program.
• Commands continue to write error messages
to descriptor 2 and have no knowledge of this
manipulation.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Possible Destinations of Standard Output
• Allow it to come to the terminal.
• Redirect it with > and >>.
• Merge it with standard error using 1>&2.
• Force output to /dev/null.
• Force output to /dev/tty.
• Connect it to standard input of another
program.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Pipes
• Connects standard output on one command
to standard input of another.
• Takes care of flow control; reader and writer
work in unison.
• No temporary file is created.
• Used to solve text manipulation problems.
• Commands have no knowledge that they are
reading from or writing to pipe.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Command Substitution
• Allows command arguments to be obtained from
standard output of another command.
• In command1 `command2`, command2 is run
first and its standard output used as arguments
to command1.
• Command enclosed by ` ` must write to standard
output.
• Convenient mechanism for running commands
whose arguments are known only at runtime.
• Enabled within double quotes but not in single
quotes.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Shell Variables
• Defined as variable=value.
• Variables have no type and need no
declaration before they are used.
• Used by UNIX system to store state
information.
• Evaluation enabled in double quotes but not
in single quotes.
• Variable defined in current shell MAY NOT be
available in programs run from the shell.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
How to Use Shell Variables
• Set a variable x to 5 and display the value:
x=5 ; echo $x
• No whitespace on either side of =:
• x = 5 is illegal; x is interpreted as a command to run.
• Can be set from command substitution:
directory=`pwd`
• No operator needed for concatenation; curly braces and
quotes often useful:
z=$x$y or z=${x}${y}
z=$file’01' or z=${file}01
• Store pathnames that are repeatedly used in a shell script:
addressbook=$HOME/data/addressbook

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
The Process
• An instance of a program in execution.
• Identified by a unique PID (Process-id).
• Created by another process as its child.
• One process can be parent of multiple
children.
• Can be killed or stopped by sending it a signal.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
The Process Cycle
• Parent forks a child by first replicating its own process
image.
• Child execs (overwrites) this image with that of
another program.
• While child is running, parent may
– wait for child to complete execution (foreground
execution).
– continue with its other tasks (background execution).
• Process terminates and parent picks up exit status of
child.
• Kernel removes entry for dead child from process
table.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Process Attributes Not Inherited by
Child
• PID
• PPID

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Process Attributes Inherited by Child
• Real UID and GID
• Effective UID and GID
• Current directory
• File descriptors
• umask value
• Environment variables

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
How a Variable is Transformed to an Environment Variable

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
When the Child Dies Before the Parent
• Child leaves behind exit status in process
table.
• Child turns to zombie state.
• Parent may
– pick up exit status; child is now completely dead.
– may not wait; child continues to remain in zombie
state.
• Zombies can’t be killed; shown as <defunct> in
ps output.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
When the Parent Dies Before the Child

• Child adopted by init.


• PPID of child changes to 1.
• When child dies, init picks up the exit status.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Signals
• Notification of occurrence of an event.
• Every signal associated with a default action
(disposition).
• Process may
– perform the default action.
– ignore the signal.
– catch the signal and invoke a signal-handling function.
• Two signals (SIGSTOP and SIGKILL) can’t be
ignored or caught.
• The keyboard and kill command generate signals.

© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Running a Job in Background
• Job is run in background with &.
• If nohup is also not used, job could be
terminated on logout.
• Job run with nohup and & continues to run
even after logging out.
• init acquires parentage of job whose parent
dies before the job.
• Background jobs can’t be killed by using
interrupt key.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
Job Control
• Job accessed by job number, name or a string
embedded in name.
• Apart from background execution, job control allows
– suspending a job ([Ctrl-z]).
– bringing a background job to foreground (fg %1).
– moving a suspended job to foreground or background (bg
%find).
• Job control scheme also includes a built-in kill
command.
• Built-in kill command also works with a job identifier
as argument (kill %find).
• Job control supported by most shells but not Bourne.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.
When a Process is Created and When
It Is Not
• External command executed in a separate
process.
• Internal shell command executed without
creating a process.
• Shell script executed in separate sub-shell which
runs commands in script.
• Startup shell script (like .profile) executed
without creating a process for script.
• Alias executed without creating a separate
process for alias.
© 2013 by McGraw-Hill Education. This is proprietary material solely for authorized instructor use. Not authorized for sale or distribution in any
manner. This document may not be copied, scanned, duplicated, forwarded, distributed, or posted on a website, in whole or part.

You might also like