Intro Shell Common Unix 2022
Intro Shell Common Unix 2022
SEEM 3460 1
Introduction to Unix Shell
A shell executes all of the commands
that you enter. Some popular shells:
Bourne shell (Bourne Again shell in
Linux)
Korn shell
C shell (TC shell in Linux)
All of these shells share a similar set of
core functionality, together with some
specialized properties.
SEEM 3460 2
Shell (con’t)
Each shell has its own programming language.
Why would you write a program in a shell
language rather than a language like C or Java?
The answer is that shell languages are tailored to
manipulating files and processes in the UNIX
system, which makes them more convenient in
many situations.
SEEM 3460 3
Shell Operations
A typical shell operation:
Shell starts up and initializes itself
Shell types a prompt character (percent or dollar sign)
and waits for user to type a command line
User types a command line
Shell extracts first word and assumes the name of a
program to be run
Shell searches for this program
If it finds it, runs the program
SEEM 3460 4
Shell Operations
When a shell is invoked automatically
during a login (or manually from a
keyboard or script), it follows a preset
sequence:
1. It reads a special start-up file, typically located
in the user’s home directory (e.g. ~/.cshrc), that
contains some initialization information. Each
shell’s start-up sequence is different.
2. It displays a prompt and waits for a user
command.
3. If the user enters a Control-D character on a line
of its own, this is interpreted by the shell as
meaning “end of input” and causes the shell to
terminate; otherwise, the shell executes the
user’s command and returns to step 2.
SEEM 3460 5
Shell Operations
SEEM 3460 6
Shell Operations
Commands range from simple utility
invocations, such as
cuse93:> ls
SEEM 3460 7
Shell Operations
If you ever need to enter a command that is longer than a
line on your terminal, you may terminate a portion of the
command with a backslash (\) character, and the shell
then allows you to continue the command on the next
line:
cuse93:> _
SEEM 3460 8
Executable Files Versus Built-in
Commands
SEEM 3460 9
Showing Your Shell’s Current
Working Directory: pwd
Every UNIX process has a location in the directory
hierarchy, termed its current working directory.
When you log into a UNIX system, your shell starts
off in a particular directory called your home
directory.
In general, every user has a different home
directory assigned by the system administrator.
To show your shell’s current working directory, use
the pwd utility.
SEEM 3460 11
Absolute And Relative Pathnames
Two files in the same directory may not have the same
name, although it’s perfectly OK for several files in
different directories to have the same name.
For example, this diagram shows a small hierarchy that
contains a “ksh” process and three files called “myFile”.
A /home/glass/myFile
B /home/myFile
C /bin/myFile
SEEM 3460 13
Absolute And Relative Pathnames
(con’t)
We can specify a pathname relative to the home
directory of an account using the character “~”
Pathname Meaning
SEEM 3460 14
Absolute And Relative Pathnames
(con’t)
A pathname can be specified relative to its current
working directory.
The UNIX file system supports the special fields
shown below that may be used when supplying a
relative pathname.
Field Meaning
. current directory
.. parent directory
SEEM 3460 15
Absolute And Relative Pathnames
(con’t)
For example, the following table shows the
pathnames of the three instances of “myFile”
relative to the “ksh” process located in the
“/home/glass” directory.
Note that the pathname “myFile” is equivalent to
“./myFile,” although the second form is rarely used
because the leading “.” is redundant.
SEEM 3460 16
Unix Programmer Support
UNIX caters very well to programmers.
Unix is an example of an “open” system
the internal software architecture is well
documented and available in source code form,
either free of charge or for a relatively small
fee.
The features of UNIX, are all easily
accessible from a programming language
such as C via a set of library routines
known as “system calls.”
such as parallel processing, inter-process
communication and file handling
SEEM 3460 17
End Of Input : Control-D
Many UNIX utilities take their input from either a file or
the keyboard. If you instruct a utility to do the latter, you
must tell the utility when the input from the keyboard is
finished. To do this, type Control-D on a line of its own
after the last line of input.
Control-D means “end of input.” For example, the mail
utility allows you to send mail from the keyboard to a
named user:
SEEM 3460 18
Unix - Listing The Contents Of A
Directory: ls
Suppose that we use an editor to edit
a file named “heart” with the following
content
I hear her breathing,
I’m surrounded by the sound.
Floating in this secret place,
I never shall be found.
SEEM 3460 20
Unix - Listing The Contents Of A
Directory: ls (con’t)
SEEM 3460 21
Unix - Listing A File: cat and more
We can use the cat utility to display the content of a
text file.
An example:
cuse93:> cat heart … list the contents of the “heart” file.
I hear her breathing,
I’m surrounded by the sound.
Floating in this secret place,
I never shall be found.
cuse93:>
SEEM 3460 22
Listing A File:
cat/more/page/head/tail
To check the contents of the “heart” file that I had created in my
home directory, “/home/glass”, I directed its contents to the
screen using the cat utility. Notice that I supplied cat with the
name of the file that I wanted to display:
SEEM 3460 26
Filename Substitution (Wildcards)
The wildcards and their meaning are shown in
the following table
Wildcard Meaning
* Matches any string, including the empty string.
? Matches any single character.
Matches any one of the characters between the
[..] brackets. A range of characters may be specified
by separating a pair of characters by a dash.
SEEM 3460 27
Filename Substitution (Wildcards)
Some examples of wildcard usage
cuse93:> ls *.c
file1.c axe.c go.c queue.c KIT.c on.c
cuse93:> ls ??.c
go.c on.c
cuse93:> ls [ac]* … any pattern beginning with a or c
axe.c c.txt abc
cuse93:> ls [a-z]* … any pattern beginning with small letter
file1.c axe.c c.txt abc axe.c go.c on.c
SEEM 3460 28
Renaming A File: mv
Now that I’d created the first draft of my lyrics,
I wanted to create a few more experimental
versions. To indicate that the file “heart” was
really the first generation of many versions to
come, I decided to rename it “heart.ver1” by
using the mv utility.
Here’s how I renamed the file using the first
form of the mv utility:
SEEM 3460 29
Renaming A File: mv (con’t)
Utility: mv -i oldFileName newFileName
mv -i {fileName}* directoryName
mv -i oldDirectoryName newDirectoryName
The first form of mv renames oldFileName as
newFileName. If the label newFileName already exists, it
is replaced.
The second form allows you to move a collection of files to
a directory.
The third allows you to move an entire directory None of
these options actually moves the physical contents of a
file if the destination location is within the same file
system as the original; instead, they just move labels
around the hierarchy.
mv is therefore a very fast utility. The -i option prompts
you for confirmation if newFileName already exists.
SEEM 3460 30
Making A Directory : mkdir
Rather than clog up my home directory with the many
versions of “heart,” I decided to create a subdirectory
called “lyrics” in which to keep them all. To do this, I used
the mkdir utility. Here’s how I did it:
cuse93:> mkdir lyrics ...create a directory called “lyrics”
cuse93:> ls -lF ...confirm.
-rw-r--r-- 1 glass 106 Jan 30 23:28 heart.ver1
drwxr-xr-x 2 glass 512 Jan 30 19:49 lyrics/
cuse93:>
SEEM 3460 32
Moving To A Directory : cd
Although I could remain in my home directory and access
the various versions of my lyric files by preceding them
with the prefix “lyrics”, doing this would be rather
inconvenient. For example, to edit the file “heart.ver1”
with the UNIX vim editor, I’d have to do the following:
cuse93:> vim lyrics/heart.ver1 …invoke the vim editor
SEEM 3460 34
Moving To A Directory : cd (con’t)
Since “.” and “..” refer to your shell’s current working
directory and parent directory, respectively, you may
move up one directory level by typing “cd ..”. Here’s an
example:
SEEM 3460 35
Copying A File : cp
After moving into the “lyrics” directory, I decided to work on
a second version of my lyrics. I wanted to keep the first
version for posterity, so I copied “heart.ver1” into a new file
called “heart.ver2” and then edited the new file. To copy the
file, I used the cp utility, which works as shown in.
SEEM 3460 37
Copying A File : cp (con’t)
After creating five versions of my song’s lyrics, my work
was done. I moved back to my directory and created a
subdirectory called “lyrics.final” in which to store the final
version of the lyrics. I also renamed the original “lyrics”
directory to “lyrics.draft” which I felt was a better name.
The commands to do all this are as follows:
cuse93:> cd …move back to my home directory.
cuse93:> mkdir lyrics.final …make the final lyrics directory.
cuse93:> mv lyrics lyrics.draft …rename the old lyrics dir.
SEEM 3460 38
Deleting A Directory : rmdir
I decided to remove the “lyrics.draft” directory, to avoid
exceeding my modest disk quota. Before I removed it, though, I
hived its contents using the pico utility. To remove the directory,
I used the rmdir utility.
Utility: rmdir {directoryName}+
The rmdir utility removes all of the directories from the list of
directory names. A directory must be empty before it can be
removed. To remove a directory and all of its contents recursively,
use the rm utility with the -r option (described shortly).
I tried to remove the “lyrics.draft” directory while it still contained
the draft versions, and I received the following error message:
SEEM 3460 39
Deleting A File : rm
The rm utility allows you to remove a file’s label
from the hierarchy. When no more labels reference
a file, UNIX removes the file itself. In most cases,
every file has only one label, so the act of removing
the label causes the file’s physical contents to be
deallocated.
However, in some occasions wherein a single file has
more than one label. In these cases, a label may be
removed without affecting the file that it refers to.
SEEM 3460 40
Deleting A File : rm (con’t)
Utility: rm -fir {fileName}*
The rm utility removes a file’s label from the directory
hierarchy. If the filename doesn’t exist, an error
message is displayed. The -i option prompts the user
for confirmation before deleting a filename; press y to
confirm your request and n otherwise. If fileName is a
directory, the -r option causes all of its contents,
including subdirectories, to be recursively deleted. The
-f option inhibits all error messages and prompts.
To remove every file in the “lyrics.draft” directory, I
moved into the “lyrics.draft” directory and used rm:
cuse93:> cd lyrics.draft …move to “lyrics.draft” directory
cuse93:> rm heart.ver1 heart.ver2 heart.ver3 heart.ver4 heart.ver5
cuse93:> ls …nothing remains.
cuse93:>
SEEM 3460 41
Deleting A File : rm (con’t)
Now that all the files were erased, I moved back to my
home directory and erased the
draft directory:
cuse93:> cd …move to my home directory.
cuse93:> rmdir lyrics.draft …this time it works.
cuse93:>
I could have written the following instead:
cuse93:> cd lyrics.draft …move into “lyrics.draft” directory.
cuse93:> rm * …erase all files in current directory.
Even better, I could have used the more advanced -r
option of rm to delete the “lyrics.draft” directory and all of
its contents with just one command:
cuse93:> cd …move to my home directory.
cuse93:> rm -r lyrics.draft …recursively delete directory.
cuse93:>
SEEM 3460 42
Printing A File: lpr/lpq/lprm
The commands above are on most UNIX systems
and come from the System V family of UNIX. BSD
UNIX provided its own print commands, which are
still supported in many versions of UNIX with a
strong BSD background as well as in Linux. These
commands cover the same basic functions of
printing, checking the queue, and canceling the job,
but have different names and arguments.
To print my file by using such a system, I employ
the lpr utility. lpr causes a numbered print job to be
started for the specified files. You may find the
status of a particular job or printer by using the lpq
utility.
SEEM 3460 43
Printing A File: lpr/lpq/lprm (con’t)
Utility: lpr -m [-Pprinter] [-#copies] {fileName}*
lpr prints the named files to the printer specified by
the -P option. If no printer is specified, the printer in
the environment variable $PRINTER is used. If no
files are specified, standard input is printed instead.
By default, one copy of each file is printed, although
this may be overridden using the -# option. The -m
option causes mail to be sent to you when printing is
complete.
SEEM 3460 44
Printing A File: lpr/lpq/lprm (con’t)
Utility: lpq -l [-Pprinter] {job#}* {userld}*
lpq displays the status of jobs on the printer
specified by the -P option. If no printer is specified,
the printer in the environment variable $PRINTER is
used. lpq displays information pertaining to the
specified jobs or the jobs of the specified users. If no
jobs or users are specified, the statuses of all jobs
on the specified printer are displayed. The -l option
generates extra information.
If, for some reason, you wish to cancel a print job,
you may do so by using the lprm utility. You may
obtain a list of the printers on your system from
your system administrator.
SEEM 3460 45
Printing A File: lpr/lpq/lprm (con’t)
Utility: lprm [-Pprinter] [-] {job#}* {userid}*
lprm cancels all of the specified jobs on the printer
specified by the -P option. If no printer is specified,
the printer in the environment variable $PRINTER is
used. The
— option cancels all of the print jobs started by you.
If you are the superuser, you may cancel all of the
jobs owned by a particular individual by specifying
his or her user id.
SEEM 3460 46
Printing A File: lpr/lpq/lprm (con’t)
As in our previous example, I started by ordering a printout
of “heart.final” from the “lwcs” printer. I then decided to
order two more copies and obtained a printer status. Finally, I
changed my mind and canceled the last print job. The
commands are as follows:
cuse93:> lpr –Plwcs heart.final …order a printout.
cuse93:> lpq -Plwcs glass … look at the printer status.
lwcs is ready and printing
Rank Owner Job Files Total Size
active glass 731 heart.final 213 bytes
cuse93:> lpr -#2 -Plwcs heart.final …order two more copies.
cuse93:> lpq -Plwcs glass …look at the printer status again.
lwcs is ready and printing
Rank Owner Job Files Total Size
active glass 731 heart.final 213 bytes
active glass 732 heart.final 426 bytes
cuse93:> lprm -Plwcs 732 ….remove the last job.
centaur: dfA732vanguard dequeued
centaur: cfA732vanguard. utdallas.edu dequeued
cuse93:> SEEM 3460 47
Printing A File: lpr/lpq/lprm (con’t)
In the next example, I used the keyboard to compose a quick
message for the printer and requested that I be notified by e-mail
upon completion of the job:
cuse93:> lpr -m -Plwcs …print from standard input.
Hi there,
This is a test of the print facility.
- Graham
^D …end of input.
cuse93:>
SEEM 3460 48
Counting The Words In A File : wc
I was quite interested in finding out how many characters,
words, and lines were in the “heart.final” file (even though
printing it gave me a byte count). To do this, I used the
wc utility. Here’s an example of wc:
SEEM 3460 50
Determining Your Terminal’s Type:
tset (con’t)
Before vi (or vim) or emacs can work correctly, your
shell’s TERM environment variable must be set to your
terminal type. Common settings for this variable include
“vtl00” and “vt52.” There are several ways to set TERM:
SEEM 3460 52
Editing A File: emacs
Starting emacs
To start emacs with a blank file, enter the command
emacs with no parameters. To edit an existing file, specify
its name as a command line parameter. Assuming that
you supply no parameters, your screen will initially look
something like the figure below, depending on your
version of emacs.
GNU Emacs 19.34.1
SEEM 3460 53
Editing A File: emacs (con’t)
I’ll draw screens that are only about six lines long to
conserve space. The second-from bottom line is called the
mode line and contains information in the following left-
to- right order:
If the first three dashes contain a then the current file has
been modified.
The name that follows “Emacs:” is the name of the current
file. If no file is currently loaded, the name *scratch* is used
instead.
The current editing mode is then shown between
parentheses. In this case, it’s Fundamental, which is the
standard editing mode.
The next entry indicates your relative position in the file as a
percentage of the entire file. If the file is very small and fits
completely on the screen, then All is displayed. If you’re at
the top or the bottom of a file, then Top and Bot are
respectively displayed.
SEEM 3460 54
Editing A File: emacs (con’t)
emacs Commands
Unlike vim, emacs doesn’t distinguish between text entry
mode and command mode. To enter text, simply start
typing. The initial emacs welcome banner automatically
disappears when you type the first letter. Long lines are
not automatically broken, so you must press the Enter key
when you wish to start a new line. Lines longer than the
width of the screen are indicated by a \ character at the
end of the screen, with the remainder of the line “wrapped”
onto the next line, as shown below.
This is a very long line that illustrates the way that unbroken lines a \
re displayed.
Control-H t
means “Press and hold the Control key and then press the H key.
(For control sequences, it doesn’t matter whether you use
uppercase or lowercase, so I suggest that you use lowercase, as
it’s easier.) Then release both keys and press the t key on its
own.” Similarly, metasequences use the Esc key. For example,
the sequence
Esc x
means “Press the Esc key (but don’t hold it) and then press the x
key.” The next few sections contain many examples of emacs
command sequences. If you ever accidentally press Esc followed
by Esc, emacs warns you that you’re trying to do something
advanced and suggests that you press the n key to continue.
Unless you’re a seasoned emacs user, it’s good advice.
SEEM 3460 56
Editing A File: emacs (con’t)
Getting Out of Trouble
Whenever you’re learning a new editor, it’s quite
easy to get lost and confused. Here are a couple of
useful command sequences to return you to a sane
state:
The command sequence Control-G terminates
any emacs command, even if it’s only partially
entered, and returns emacs to a state in which
it’s waiting for a new command.
The command sequence Control-X 1 closes all
emacs windows except your main file window.
This action is useful, as several emacs options
create a new window to display information, and
it’s important to know how to close them once
you’ve read their contents.
SEEM 3460 57
Editing A File: emacs (con’t)
Getting Help
There are several ways to obtain help information
about emacs. One of the best ways to get started
with emacs is to read the self-describing help
tutorial. I suggest that you do this before anything
else. To read the tutorial, use the command
sequence Control-H t. The tutorial will appear and
give you directions on how to proceed.
Leaving emacs
To leave emacs and save your file, use Control-X
Control-C. If you haven’t saved your file since it was
last modified, you’ll be asked whether you want to
save it.
SEEM 3460 58
Editing A File: emacs (con’t)
emacs Modes
emacs supports several different modes for entering text.
including Fundamental, Lisp Interaction, and C. Each
mode supports special features that are customized for
the particular kind of text that you’re editing. emacs starts
in Fundamental mode by default; I’ll use that mode during
my description of emacs. For more information about
modes, consult the emacs tutorial.
Entering Text
To enter text, simply start typing. For example, the
following shows a short four-line poem. The next section
describes the editing features of emacs that allowed me
to change this poem to something a little better.
There is no need for fear in the night,
You know that your Mommy is there,
To watch over her babies and hold them tight,
When you are in her arms you can feel her sigh all night.
—Emacs: *scratch* (Fundamental) ---All -------
Entering text in emacs.
SEEM 3460 59
Editing A File: emacs (con’t)
Common Editing Features
The most common emacs editing features can be grouped into the
following categories:
moving the cursor
deleting, pasting, and undoing text
searching through text
searching and replacing text
saving and loading files
miscellaneous
These categories are described and illustrated in the subsections
that follow, using the
sample poem that I entered at the start of this section.
Moving the Cursor
The table shows the common cursor movement commands. For
example, to insert the words “worry or” before the word “fear” on
the first line, I moved the cursor to the first line of the file by
typing Esc < and then moved forward several words by using the
Esc f sequence. I then typed in the words, which were
automatically inserted at the current cursor position.
SEEM 3460 60
Editing A File: emacs (con’t)
Movement Key sequence
Up one line Control-P (previous)
Down one line Control-N (next)
Right one character Control-F (forward, wraps around)
Left one character Control-B (backward, wraps around)
To start of line Control-A (a is first letter)
To end of line Control-E (end)
Back one word Esc b (back)
Forward one word Esc f (forward)
Down one screen Control-V
Up one screen Esc v
Start of file Esc <
End of file Esc>
SEEM 3460 61
Editing A File: emacs (con’t)
Deleting, Pasting, and Undoing
The following table shows the common deletion
commands. Whenever an item is deleted, emacs
“remembers” it in an individual “kill buffer.” A list of kill
buffers is maintained so that deleted items may be
retrieved long after they have been removed from the
display. To retrieve the last killed item, use Control-Y.
After you have typed Control-Y, you may type Esc y to
replace the retrieved item with the previously deleted
item. Every time you type Esc y, the retrieved item moves
one step back through the kill buffer list.
You may append the next deleted item onto the end of
the last kill buffer, rather than create a new buffer, by
typing Esc Control-W immediately prior to the delete
command. This tack is useful if you wish to cut different
bits and pieces out of a file and then paste them all
together back into one place.
SEEM 3460 62
Editing A File: emacs (con’t)
Item to delete Key sequence
Character before cursor <delete> key
Character after cursor Control-D
Deleting, pasting, and
Word before cursor Esc <delete> undoing in emacs.
Word after cursor Esc d
To end of current line Control-K
Sentence Esc k
You may undo editing actions one at a time by typing Control-X u for each
action that you wish to undo.
The following table shows a summary of the kill buffer and undo
commands.
Action Key sequence
Insert last kill buffer. Control-Y The kill buffer in
Retrieve previous kill. Esc y emacs.
Append next kill. Esc Control-W
Undo. Control-X u
SEEM 3460 63
Editing A File: emacs (con’t)
Searching
emacs allows you to perform something called an incremental
search. To search for ward from your current cursor position for a
particular sequence of letters, type Control-S. The prompt “I-
search:” is displayed on the bottom line of the screen, indicating
that emacs wants you to enter the string that you wish to search
for. As you enter the character sequence, emacs searches to find
the string nearest to your initial cursor position that matches
what you’ve entered so far; in other words, partial sub strings are
found as you enter the full string. To terminate the search and
leave your cursor at its current position, press Esc. If you delete
characters in the full string before pressing the Esc key, emacs
moves back to the first match of the remaining sub string. To
repeat a search, don’t press Esc, but instead press Control-S to
search forward or Control-R to search backward.
Action Key sequence
Search foward for str. Control-S str
Search backward for str. Control-R str
Repeat last search forward. Control-S
Repeat last search backward. Control-R
Leave search mode. Esc
SEEM 3460 64
Editing A File: emacs (con’t)
Search and Replace
To perform a global search and replace, type Esc x,
followed by the string “repl s”, followed by Enter. emacs
will prompt you for the string to replace. Enter the string
and press Enter. emacs will prompt you for the
replacement string. Enter the string and press Enter.
emacs then performs the global text substitution.
Miscellaneous
To redraw the screen, type Control-L. To place
emacs into auto-wrap mode, which automatically
inserts line breaks when words flow past the end of
a line, type Esc x auto-fill-mode and press Enter. To
leave this mode, repeat the command.
SEEM 3460 66