You are on page 1of 62

THE UNIX SHELLS

SHELL
 Shellis the user interface to the operating system
 Functionality:

Manage files (wildcards, I/O redicrection)


Manage processes (build pipelines, multitasking)
 Most popular shells:
The Bourne shell (sh)
The Korn shell (ksh)
The C shell (csh)
The Bourne Again shell (bash)
A BIT OF HISTORY
 The Bourne shell /bin/sh ( S. R. Bourne, 1977)
powerful syntactical language
strong in controlling input and output
expression matching facilities
 interactive use: the use of shell functions.
 The C-shell /bin/csh (Bill Joy, UCB, 1978)

new concepts: job control and aliasing


much better for interactive use
 different input language:
 out went the good control of input and output

 too buggy to produce robust shell scripts


A BIT OF HISTORY
 /bin/tsch (Ken Greer, Carnegie Mellon University, late
1970s)
User –oriented command line editing
Out most of the bugs
 Korn shell /bin/ksh (David Korn, AT&T, early 1980s)
Bourne shell language
C shell’s features for interactive work
 You had to pay AT&T for it!
 GNU project: a free shell=> /bin/bash (the Bourne again
shell)
SELECTING A SHELL
CHANGING A SHELL
 To
change your default shell use the chsh utility
which requires full pathname of the new shell
CORE SHELL FUNCTIONALITY
SHELL OPERATIONS
 A shell is invoked, either
 automaticallyupon login, or
 manually from the keyboard or script

1. Reads a special startup file (.cshrc for csh) (.bashrc for


bash) and executes all the commands in that file
2. Displays a prompt and waits for a user command

3. If user enters CTRL-D (end of input) the shell


terminates, otherwise it executes the user command(s)
COMMANDS

 Simple utility invocations: who


 Long pipelines

 Several-lines commands: backslash(\)


PS UTILITY
 Information about processes ( you need to have
appropriate privileges).
 By default, selects all processes with the same effective
user ID as the current user and the same controlling
terminal as the invoker
-f : generates full listing
-l: generates a long listing
-e : lists information about running processes
-a: lists information about all most frequently
requested processes(except processes not associated
with a terminal)
OUTPUT OF PS UTILITY
OUTPUT OF PS UTILITY
UNIX COMMANDS VS BUILT-IN SHELL
COMMANDS
 Most Unix commands invoke utility programs that are
stored in the file hierarchy.
 The shell

locates the executable program for this utility (using


PATH variable, “/bin”)
executes it
Example: ls
 They might vary in syntax and usage, but they are shell
independent.
 Shells have built-in commands:

it executes internally


Example: cd, echo
ECHO
 Built-inshell command
 Displays its arguments to standard output

 By default, appends a newline to the output

 echo {arg}*

 There is also utility /bin/echo

Subtle behavior may vary among different shells


METACHARACTERS
 Metacharacters are characters that specially
processed by a shell.
 When you enter a command, shell

scans for metacharacters,


specially processes them,
then executes command.
 To turn off the meaning of a metacharacter: \
SHELL METACHARACTERS
OUTPUT REDIRECTION
 command > file
Creates file if it doesn’t exist
Overwrites if file exists
If file exists and hasn’t write permission, an error
occur
 command >> file

Creates file if it doesn’t exist


Appends to file if file exists
INPUT REDIRECTION
 command < file
If file doesn’t exist or hasn’t read permission, an
error occur
 command << word

Copies its standard input up to (not including) the


line starting with word into buffer which is used as
input

 mail alex@msn.com < text.txt


WILDCARDS: FILENAME SUBSTITUTION
 Globbing is process of replacement of the pattern with
metacharacters by an alphabetically sorted list of all
matching filenames.

 Ifstring is surrounded by single (double) quotes, shell


doesn’t process wildcards in the string.
WILDCARDS EXAMPLE
WILDCARDS: FILENAME SUBSTITUTION
 A character / in filename must be match explicitly.
PIPES
 Pipe metacharacter : |
 command1 | command 2 | command 3

 Such sequence of commands is called pipeline


Large problem can be often solved by a chain of
smaller processes,
each performed by a relatively small, reusable utility
 The standard error channel is not piped through a
standard pipeline (some shell supports it).
 ls | wc -w
PIPELINE EXAMPLE
TEE
 Copies standard output of a pipe to a file while still
allow sing that output to flow down the pipeline

 tee
–ia {fileName}+
-a: appends the input to the file instead of
overwriting
-i: ignores interrupts
TEE EXAMPLE
COMMAND SUBSTITUTION
 A command in grave accents (`) is executed
 Its standard output is inserted in the command in its
place
 Any newlines in the output are replaced with spaces.
COMMAND SUBSTITUTION
SEQUENCES
 One-liner sequence of commands or pipelines:
 separatedby semicolons.
 Executed from left to right
CONDITIONAL SEQUENCES
 Every Unix process terminates with an exit value:
0 means successful completion
 Non zero means failure
 Built-in shell commands return 1 if they fail.
 &&: executes the next commands only if the previous
command returns 0.
 ||: executes the next command only if the previous
command returns a nonzero exit code
CONDITIONAL SEQUENCES
GROUPING COMMANDS
 Commands can be grouped by placing them between
()
 Grouped commands are executed by a child shell
(subshell).
 Grouped commands share standard input, standard
output and standard error channels.
 Group can be redirected and piped.
GROUPING EXAMPLE
BACKGROUND PROCESSING
 Command, or pipeline, or sequence, or group of
commands is followed by &:
A subshell is created to executes the commands
as a background process
Runs concurrently with a parent shell
Does not take control over keyboard
 Shell displays some information that may be used
to control the process (shell specific)
REDIRECTING OUTPUT AND INPUT
 Output:
 find . – name ‘*.c’ –print > find.txt &
 find . – name ‘*.c’ –print |mail iraa@cs.gsu.edu &
 Input:
 When a background process attempts to read from
terminal, the terminal automatically sends an error signal
=> terminates.
NOHUP
 The Bourne and Korn shell automatically terminates
background process when you log out.
 C and Bash shells allow them to continue.

 nohup utility executes command and makes it immune to the


hangup (HUP) and terminate (TERM) signals.
 Standard output and error channels of command are
automatically redirect to a file named “nohup.out”.
 Process’s priority value is increased by 5 (reducing its priority)
EXAMPLE
SHELL PROGRAMMING: SCRIPTS
 Script is a text file containing series of shell commands
for later execution.
 Execute permission (chmod +x scriptName)
scriptName
 When script is run
System determines which shell should be used
Executes the shell with script as input
 If the first line is

#: current shell


#!pathName: the executable program pathName
otherwise: Bourne shell
SHELL PROGRAMMING: SCRIPTS
# in any line other than first: comment
 Good practice: always specify the shell

#!/bin/csh
# A simple C-shell script
echo -n The date today is #in csh –n omits newline
date

#!/bin/ksh
# A simple Korn shell script
echo “The date today is \c” #in ksh \c omits newline
date
SUBSHELLS
 Several ways a parent shell can creates a child shell
(subshell)
Group of commands are executed
Script is executed
Background job (concurrent execution)
 Parent shell sleeps till child shell terminates (execution
in background can be useful)
 A subshell has its own working directory
SHELL’S DATA AREAS
Parent shell
Child shell
Environment
Environment
Local
Local: clean
VARIABLES
 Both local and environmental variables are hold data in string
format.
 Every shell has a set of

predefined environment (start-up files)


 $HOME : full pathname of home directory

 $PATH : list of directories to search for commands

 $MAIL : the full pathname of mailbox

 $USER : your username

$SHELL : the full pathname of login shell

$TERM : the type of your terminal

predefined local variables.


ASSIGNING VARIABLES
 Differs among shells
 Access the variable: append prefix $ to the name of a
variable: $variableName
 To create a variable, simply assign it a value
 Bourne, Korn and Bash shells: varName=value
BUILT-IN VARIABLES
 $$ the process ID of the shell

 $0 the name of the shell script

 $1..$9 $n refers to the nth command line argument

 $* a list of all command-line arguments


EXAMPLE
QUOTING
 Single quotes(‘) inhibit wildcard replacement, variable
substitution and command substitution.
 Double quotes (“) inhibit wildcard replacement only.

 When quotes are nested, only the outer quotes have any
effect.
JOB CONTROL
 ps : allows to monitor a status of processes.
 kill: allows you to terminate a process on the basis of its ID
number
 wait: allows a shell to wait for one of its child processes to
terminate

 sleep seconds: sleeps for the specified number of seconds, then


terminates
SIGNALING PROCESSES: KILL
 Korn and C shell: built-in command kill
 Bourne shell: Unix utility

 kill [signalId] {pid}+

 kill –l

 Sends the signal with code signalId to the list of numbered


processes.
 signalId is the number or name of the signal (TERM,
or number 15)
 -l: list valid signal names
SIGNALING PROCESSES: KILL
 Processes
may protect themselves from all signals
except KILL
Process wouldn’t be allowed to clean up and
terminate normally, as it would do when it receives
TERM

 Thekill utility allows you to specify 0 as pid


All processes associated with shell would be
terminated.
EXAMPLES
WAITING FOR A CHILD PROCESS: WAIT
 wait pid
 Causes the shell to suspend operation until the child
process with the specified process ID terminates.
 If no arguments are specified, shell waits for all of its
child processes to terminate.
FINDING A COMMAND: $PATH
 Shellchecks if command is built-in.
If yes, executes it directly.
 Otherwise, checks whether it begins with /
If yes, shell assumes that it is the absolute pathname
of a command and tries to execute the file with such
absolute pathname.
If there is no such file or it’s not executable, an error
occur.
FINDING A COMMAND: $PATH
 Else, shell searches the directories (from left to right)
in the PATH environment variable.
 If match is found, the file is executed.

 If match is not found, or it’s not an executable, an error


occurs.
 If PATH is not set or is empty string, only current
directory is searched.
OVERLOADING STANDARD UTILITIES
 Create subdirectory bin
 Place it before the traditional /bin in your PATH
settings
 It results in overloading of UNIX versions, since your
subdirectory would be searched before traditional
/bin
 Only current shell and its child shells would be
affected by the change to PATH.
 All other shells would be unaffected.
TERMINATION
 Bourne, Korn and Bash shells: special variable $?
Contains the value of the previous command’s exit code.
 C shell: $status holds the exit code.

 exit number
 Terminates the shell and returns the exit value number to
its parent process.
CORE BUILT-IN COMMANDS: EVAL
 Executesthe output of a command as a regular
shell command.
CORE BUILT-IN COMMANDS: EXEC
 Causes the shell’s image to be replaced with command in
the process’ memory space.
 The exec command does not spawn a new process.
Instead, exec command is executed in place of  the
current shell .  
 If commands is successfully executed, the shell that
performed the exec ceases to exist.
 If it was login shell, the login session is terminated when
command terminates.
EXAMPLE
CORE BUILT-IN COMMANDS: EXEC

 The most common usage is the replacement of login


shell when you have no power to change the setting:
exec bash
 The exec command is useful in pipes and can be used to
set redirection to file descriptors.
 The current shell process is modified but not overlaid.
In shell the exec command can be used to open, close,
and copy file descriptors.
CORE BUILT-IN COMMANDS: SHIFT
 Causes all positional parameters $2..$n to be renamed
$1..$(n-1) and $1 to be lost.

 Helpsto cycle through a series of command line


parameters in the script.

 If
no positional arguments are let to shift, an error
message is displayed.
CORE BUILT-IN COMMANDS: UMASK
 When a C program creates a file, it supplies the file’s
original permission settings as an octal number to the
system call open().
 fd=open (“file”, OCREAT |ORDWR, 0666);

 If you try to create file by using redirection (>), your


file would have different permission.
 Reason: any UNIX process contains a special
quantity called umask value
Which restricts the permission settings that are
requested when a file is created.
UMASK EXAMPLE
CORE BUILT-IN COMMANDS: UMASK
 If a file already exists before something is redirected to
it, the original file’s permission values are retained
(umask is ignored).
 umask [octalValue]

 Sets the shell’s umask value to the specified octal


number,
 or displays the current value if the argument is omitted.

 A shell’s umask value is retained until changed.

 Child processes inherit their umask value from their


parent.

You might also like