Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 146

UNIX Operating System

Level I

Purpose
Provide understanding of UNIX operating system.
Introduce various commands and shell scripting capability of UNIX.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

Product
Proper understanding of UNIX operating system architecture.

Proper understanding of shell scripting abilities of UNIX.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

Process
Explain UNIX features.
Hands on session.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

Agenda
Understanding unix.
File system
Commands.
Exercise & Assignment

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

Part I

What is UNIX?

What is Unix?
UNIX, in computer science, a powerful multi-user, multitasking operating system. Considered a very powerful
operating system, UNIX is written in the C.
Designed with Flexibility in mind.
Designed with Modularity in mind.
Everything is a file !

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

Structure

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

Processing model
Centralized

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

Components
Kernel
Shell

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

10

Components
Kernel
Core of the UNIX system. Loaded at system start up (boot). Memory-resident control
program.
Manages the entire resources of the system, presenting them to you and every other
user as a coherent system. Provides service to user applications such as device
management, process scheduling, etc.
Example functions performed by the kernel are:
managing the machine's memory and allocating it to each process.
scheduling the work done by the CPU so that the work of each user is carried out as
efficiently as is possible.
accomplishing the transfer of data from one part of the machine to another.
interpreting and executing instructions from the shell.
enforcing file access permissions.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

11

Components
Shell
Whenever you login to a Unix system you are placed in a shell program. The shell's prompt is
usually visible at the cursor's position on your screen. To get your work done, you enter commands
at this prompt.
The shell is a command interpreter; it takes each command and passes it to the operating system
kernel to be acted upon.
Several shells are usually available on any UNIX system, each with its own strengths and
weaknesses.
Different users may use different shells. Initially, your system administrator will supply a default shell,
which can be overridden or changed. The most commonly available shells are:
Bourne shell (sh)
C shell (csh)
Korn shell (ksh)
Bourne Again Shell (bash)
Each shell also includes its own programming language. Command files, called "shell scripts" are
used to accomplish a series of tasks.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

12

Part II

Log in

Log in
you will need to have a valid username and a password
Type your username at the login prompt
UNIX is case sensitive
When the password prompt appears, type in your password.
Your password is never displayed on the screen as a security measure.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

14

Login screen

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

15

Part III
Working with Unix

General commands
date - show date and time
whoami - show your userid
who - show who is logged onto the system
w - show who is logged onto the system
pwd - print the working directory's name
clear - clear the screen
finger
The finger command displays information about users on a given host. The host can be
either local or remote.
finger your userid

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

17

General commands

ls -a - list all files in current directory


ls -al - long list of current directory
cat .bashrc - display contents of .bashrc file
mkdir dir1 - make a directory called dir1
cd dir1 - change directory to dir1
cd .. - change to parent directory
rmdir dir1 - remove directory dir1
cp .login new.login - copy the .login file to new.login
wc new.login - count the lines, words and characters in the new.login file
wc -l new.login - count just the lines
rm new.login - remove the new.login file

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

18

Changing your password


passwd
Old password: - enter your current password
New password: - enter your new password
Retype new password: - re-enter your new password

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

19

Logging Off The System


To logout enter the command logout or exit.
If this does not, work press Ctrl-d.
If you have a .logout file in your home directory, the system will execute any commands contained there.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

20

Part IV
File system

Features of UNIX File system


All of the files in the UNIX file system are organized into a multi-leveled hierarchy called a
directory tree.
A family tree is an example of a hierarchical structure that represents how the UNIX file
system is organized. The UNIX file system might also be envisioned as an inverted tree or
the root system of plant.
At the very top of the file system is single directory called "root" which is represented by
a / (slash). All other files are "descendents" of root.
The number of levels is largely arbitrary, although most UNIX systems share some
organizational similarities.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

22

File types
Ordinary Files
Directory
Special Files
Pipes

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

23

File types
Ordinary Files
Used to store your information, such as some text you have written or an image you have drawn. This is the type of file that
you usually work with.
Always located within/under a directory file
Do not contain other files

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

24

File types
Directories
Branching points in the hierarchical tree
Used to organize groups of files
May contain ordinary files, special files or other directories
Never contain "real" information which you would work with (such as text). Basically, just used for organizing files.
All files are descendants of the root directory, ( named / ) located at the top of the tree.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

25

File types
Special Files
Used to represent a real physical device such as a printer, tape drive or terminal, used
for Input/Ouput (I/O) operations
Unix considers any device attached to the system to be a file - including your terminal:
By default, a command treats your terminal as the standard input file (stdin) from
which to read its input
Your terminal is also treated as the standard output file (stdout) to which a
command's output is sent
Stdin and stdout
Two types of I/O: character and block
Usually only found under directories named /dev

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

26

File types
Pipes
UNIX allows you to link commands together using a pipe. The pipe acts a temporary file which only exists to hold data from
one command until it is read by another

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

27

File Types
File type

Meaning

a normal file

a directory

symbolic link

block device file

character device file

a fifo or named pipe

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

28

File Names
UNIX permits file names to use most characters, but avoid spaces, tabs and characters
that have a special meaning to the shell, such as: & ; ( ) | ? \ ' " ` [ ] { } < > $ - ! /
Case Sensitivity: uppercase and lowercase are not the same! These are three different
files: NOVEMBER November november
Length: can be up to 256 characters
Extensions: may be used to identify types of files
libc.a - archive, library file
program.c - C language source file
alpha2.f - Fortran source file
xwd2ps.o - Object/executable code
mygames.Z - Compressed file

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

29

File Names
Hidden Files: have names that begin with a dot (.) For example: .cshrc .login .mailrc .mwmrc
Uniqueness: as children in a family, no two files with the same parent directory can have the same name. Files
located in separate directories can have identical names.
Reserved Filenames:

/ - the root directory (slash)


. - current directory (period)
.. - parent directory (double period)
~ - your home directory (tilde)

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

30

Pathnames
Specify where a file is located in the hierarchically organized file system
Must know how to use pathnames to navigate the UNIX file system
Absolute Pathname: tells how to reach a file begining from the root; always begins with / (slash). For
example: /usr/local/doc/training/sample.f
Relative Pathname: tells how to reach a file from the directory you are currently in ( current or
working directory); never begins with / (slash). For example:
training/sample.f
../bin
~/projects/report.001
For example, if your current directory is /usr/home/johnson and you wanted to change to the
directory /usr/home/quattro, you could use either of these commands:
cd ../quattro - relative pathname
cd /usr/home/quattro - absolute pathname

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

31

Automating

Automating logon tasks.


Naming: Depending upon the shell, you must name your initialization file(s) accordingly.
Executed during interactive login
.login - csh, tcsh
.profile - sh, ksh, bash
.bash_profile - bash (alternative 1)
.bash_login - bash (alternative 2)

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

33

Part V
Commands

Commands
cat
dumps the entire file to the screen without paging. This command is more useful for concatenating (hence the name "cat")
files together than it is for reading files.
Examples:
cat myprog.c - diplays entire file
cat -b myprog.c - shows line numbers
cat file1 file2 > file3 - adds file1 and file2 to make file3

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

35

File and Directory Commands


ls
ls - show contents of working directory
ls file - list file, if it exists in working directory
ls dir - show contents of the directory dir
ls -a - shows all your files, including hidden ones
ls -al - give detailed listing of contents
ls -F - mark directories with "/" and executable files with "*"
ls *.doc - show all files with suffix ".doc"
ls -lt - Time of last modification.
ls -lut - Time of last access

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

36

Commands
more
browses/displays files one screen at a time.
Use h for help,
spacebar to page,
b for back,
q to quit,
/string to search for string
Eg: more sample.f

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

37

Commands
head
displays the first n lines of a file
head sample.f - display first 10 lines (default)
head -5 sample.f - display first 5 lines
head -25 sample.f - display first 25 lines

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

38

Commands
tail
displays the last n lines or n characters of a file
Examples
tail sample.f - display last 10 lines (default)
tail -5 sample.f - display last 5 lines
tail -5c sample.f - display last 5 characters
tail -25 sample.f - display last 25 lines

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

39

Commands
cp
copies files. Will overwrite unless otherwise specified. Must also have write permission in the destination directory.
Examples:
cp sample.f sample2.f - copies sample.f to sample2.f
cp -R dir1 dir2 - copies contents of directory dir1 to dir2
cp -i file.1 file.new - prompts if file.new will be overwritten
cp *.txt chapt1 - copies all files with .txt suffix to directory chapt1
cp /usr/doc/README ~ - copies file to your home directory
cp ~betty/index . - copies the file "index" from user betty's home directory to current directory

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

40

Commands
mv
moves files. Will overwrite unless otherwise specified. Must also have write permission
in the destination directory.
Example:
mv sample.f sample2.f - moves sample.f to sample2.f
mv dir1 newdir/dir2 - moves contents of directory dir1 to newdir/dir2
mv -i file.1 file.new - prompts if file.new will be overwritten
mv *.txt chapt1 - moves all files with .txt suffix to directory chapt1

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

41

Commands
rm
deletes/removes files or directories if file permissions permit.
Example
rm sample.f - deletes sample.f
rm chap?.txt - deletes all files with chap as the first four characters of their name and with .txt as the last four characters of
their name
rm -i * - deletes all files in current directory but asks first for each file
rm -r / olddir - recursively removes all files in the directory olddir, including the directory

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

42

Commands
find
finds files.
The syntax of this command is: find pathname -name filename -print
The pathname defines the directory to start from. Each subdirectory of this directory will be
searched. The -print option must be used to display results.
You can define the filename using wildcards. If these are used, the filename must be placed in
'quotes'.
Example
find . -name mtg_jan92 -print - looks for the file mtg_jan92 in current directory
find ~/ -name README -print - looks for files called README throughout your home directory
find . -name '*.fm' -print - looks for all files with .fm suffix in current directory
find /usr/local -name gnu -type d -print - looks for a directory called gnu within the /usr/local
directory

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

43

Commands
diff
comparing two files or directories. Indicates which lines need be added (a), deleted (d) or changed (c). Lines in
file1 are identified with a (<) symbol: lines in file2 with a (>) symbol
diff file1 file2 - compares file1 to file2
diff -iw file1 file2 - compares two files ignoring letter case and spaces
diff dir1 dir2 - compares two directories showing files which are unique to each and also, line by line
differences between any files in common.
For example, if file1 and file2 are:
John erpl08@ed John erpl08@ed
Joe CZT@cern.ch Joe CZT@cern.ch
Kim ks@x.co Jean JRS@pollux.ucs.co
Keith keith@festival Jim jim@frolix8
Kim ks@x.co
Keith keith@festival
Using the diff command: diff file1 file2 Yields the output:
2a3,4
> Jean JRS@pollux.ucs.co
> Jim jim@frolix8
Which means that to make these files match you need to add (a) lines 3 and 4 (3,4) of file2 (>) after line 2 in
file1.
The information contained in this presentation is proprietary.
Copyright 2011 Capgemini. All rights reserved.

44

Commands
pwd
print working directory. Tells you which directory you are currently in.
mkdir
make directory. Will create the new directory in your working directory by default.
mkdir /u/training/data mkdir data2
rmdir
remove directory. Directories must be empty before you remove them.
rmdir project1
To recursively remove nested directories, use the rm command with the -r option:
rm -r directory_name

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

45

Agenda
File permissions
vi Editor
More commands

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

46

Part VI
Access Permissions

Access Permissions
UNIX is a multi-user system. Every file and directory in your account can be protected from or made accessible
to other users by changing its access permissions. Every user has responsibility for controlling access to their
files.
Permissions for a file or directory may be any or all of:
r - read
w - write
x - execute = running a program

Each permission (rwx) can be controlled at three levels:


u - user = yourself
g - group = can be people in the same project
o - other = everyone on the system

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

48

Access Permissions
File access permissions are displayed using the ls -l command.
The output from the ls -l command shows all permissions for all levels as three groups of three according to the
scheme:
owner read (r) owner write (w) owner execute (x)
group read (r) group write (w) group execute (x)
public read (r) public write (w) public execute (x)
which are displayed as: -rwxrwxrwx
Note: a directory must have both r and x permissions if the files it contains are to be accessed.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

49

Access Permissions
The chmod command is used to change access permissions for files which you own. The syntax is:
chmod permission_triads filename
[who][action][permissions]
where: who action permissions
u = user + = add r = read
g = group - = remove w = write
o = other x = execute
a = all
chmod a+r sample.f - Adds read permission for all users to the file sample.f.
chmod o-r sample.f - Removes read permission for others to the file sample.f.
chmod og+rx prog* - Adds read and execute permissions for group and others to all files which
contain "prog" as the first four characters of their name.
chmod +w * - Adds write permission for user to all files in current directory.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

50

Access Permissions
File access permissions can also be changed by a numerical (octal) chmod specification. Read
permission is given the value 4, write permission the value 2 and execute permission 1.
rwx
421
These values are added together for any one user category:
0 = no permissions
1 = execute only
2 = write only
3 = write and execute (1+2)
4 = read only
5 = read and execute (4+1)
6 = read and write (4+2)
7 = read and write and execute (4+2+1)

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

51

Access Permissions
So access permissions can be expressed as three digits.
For example:
user

group

chmod 640 file1

rw-

r--

---

chmod 754 file1

rwx

r-x

chmod 664 file1

rw-

rw-

others

Never set write permission for all other users on a file or directory which is in your home
directory. If you do other users will be able to change its content. This can represent a
serious security risk.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

52

Change file ownership


chown
Can be issued by owner of file ONLY.
Syntax: chown newowner filename
Ownership once given cannot be revoked.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

53

More on File permissions


Each user has default set of permissions which apply to all files/dir created by users,
unless explicitly set.
By default:
For files permission is -rw-r--r
For directory it is drwxr-xr-x
Command
umask -S
Will show you present setting of mask.
You mention those permissions that you do not want to be given by default to newly
created files and directories.
umask 0027
Default 0022

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

54

Part VII
Editors

vi editor
vi = visual
vi supplies commands for:
inserting and deleting text
replacing text
moving around the file
finding and substituting strings
cutting and pasting text
reading and writing to other files

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

56

vi editor
vi uses a "buffer"
While using vi to edit an existing file, you are actually working on a copy of the file that is
held in a temporary buffer in your computer's memory.
If you invoked vi with a new filename, (or no file name) the contents of the file only exist in
this buffer.
Saving a file writes the contents of this buffer to a disk file, replacing its contents. You can
write the buffer to a new file or to some other file.
You can also decide not to write the contents of the buffer, and leave your original file
unchanged.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

57

vi editor
vi operates in two different "modes":
Command mode
vi starts up in this mode
Whatever you type is interpreted as a command - not text to be inserted into the file.
The mode you need to be in if you want to "move around" the file.
Insert mode
This is the mode you use to type (insert) text.
There are several commands that you can use to enter this mode.
Once in this mode, whatever you type is interpreted as text to be included in the file.
You can not "move around" the file in this mode.
Must press the ESC (escape) key to exit this mode and return to command mode.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

58

Starting vi editor

vi filename
The filename can be the name of an existing file or the name of the file you want to create.

view filename
Starts vi in "read only" mode. Allows you to look at a file without the risk of altering its contents.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

59

vi editor
Exiting vi
:q
- quit - if you have made any changes, vi will
need to use one
of the other quits.
:w
- write edit buffer to disk

warn you of this, and you'll

:w filename - write edit buffer to disk as filename


:wq
- write edit buffer to disk and quit
:ZZ
:q!

- write edit buffer to disk and quit


- quit without writing edit buffer to disk

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

60

vi editor
Positioning within text
By character
left arrow - left one character
right arrow - right one character
backspace - left one character
space
- right one character
h
- left one character
l
- right one character
By word

w
nw
b
nb
e
ne

- beginning of next word


- beginning of nth next word
- back to previous word
- back to nth previous word
- end of next word
- end of nth next word

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

61

vi editor
By line
down arrow - down one line
up arrow

- up one line

- down one line

- up one line

- beginning of next line down

- first column of current line (zero)

- first character of current line

- last character of current line

- beginning of previous line up

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

62

vi editor
By block
(

- beginning of sentence

- end of sentence

- beginning of paragraph

- end of paragraph

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

63

vi editor
By screen

CTRL-f

- forward 1 screen

CTRL-b

- backward 1 screen

CTRL-d

- down 1/2 screen

CTRL-u

- up 1/2 screen

- top line on screen

- mid-screen

- last line on screen

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

64

vi editor
Inserting text

a
A
i
I
o

- append text after cursor *


- append text at end of line *
- insert text before cursor *
- insert text at beginning of line *
- open a blank line after the current
line for text input *
O
- open a blank line before the current
line for text input *
* Note: hit ESC (escape) key when finished inserting!

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

65

vi editor
Deleting text
x
- delete character at cursor
dh
- delete character before cursor
nx
- delete n characters at cursor
dw
- delete next word
db
- delete previous word
dnw
- delete n words from cursor
dnb
- delete n words before cursor
d0
- delete to beginning of line
d$
- delete to end of line
D
- delete to end of line
dd
- delete current line
d(
- delete to beginning of sentence
d)
- delete to end of sentence
d{
- delete to beginning of paragraph
d}
- delete to end of paragraph
ndd
- delete n lines (start at current line)

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

66

vi editor
Copying lines
yy
nyy
p

- "yank": copy 1 line into buffer


- "yank": copy n lines into buffer
- put contents of buffer after current
line
P
- put contents of buffer before current
line
Moving lines (cutting and pasting)
ndd
p
P

- delete n lines (placed in buffer)


- put contents of buffer after current
line
- put contents of buffer before current
line

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

67

vi editor
Searching / Substituting
/str
?str
n
N

- search forward for str


- search backward for str
- find next occurrence of current string
- repeat previous search in reverse
direction

The substitution command requires a line range


specification. If it is omitted, the default
is the current line only. The examples below
show how to specify line ranges.
:s/old/new

- substitute new for first occurrence


of old in current line

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

68

vi editor
:s/old/new/g - substitute new for all occurrences
of old in current line
:1,10s/old/new - substitute new for first occurrence
of old in lines 1 - 10
:.,$s/old/new - substitute new for first occurrence
of old in remainder of file
:.,+5s/old/new - substitute new for first occurrence
of old in current line and next 5 lines
:.,-5s/old/new - substitute new for first occurrence
of old in current line and previous
5 lines
:%s/old/new/g - substitute new for all occurrences
of old in the entire file
:%s/old/new/gc - interactively substitute new for all
occurrences of old - will prompt for
y/n response for each substitution.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

69

vi Options
You can change the way vi operates by changing the value of certain options which control specific parts of the
vi environment.
To set an option during a vi session, use one of the commands below as required by the option:
:set option_name
:set option_name=value
Some examples of the more common options are described below.
:set all

- shows all vi options in effect

:set ai

- set autoindent - automatically indents


each line of text

:set noai

- turn autoindent off

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

70

vi Options
:set nu

- set line numbering on

:set nonu

- turn line numbering off

:set scroll=n - sets number of lines to be scrolled


to n. Used by screen scroll commands.
:set sw=n

- set shiftwidth to n. Used by autoindent


option.

:set wm=n

- set wrapmargin to n. Specifies number


of spaces to leave on right edge of the
screen before wrapping words to next
line.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

71

vi Options
:set showmode - reminds you when you are inserting
text.
:set ic

- ignore case of characters when


performing a search.

Options can be set permanently by putting them in a file called .exrc in your home directory.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

72

Part VIII
More commands

Processes
Whenever you enter a command at the shell prompt, it invokes a program. While this program is running it is
called a process. Your login shell is also a process, created for you upon logging in and existing until you logout.
UNIX is a multi-tasking operating system. Any user can have multiple processes running simultaneously,
including multiple login sessions. As you do your work within the login shell, each command creates at least one
new process while it executes.
Process id: every process in a UNIX system has a unique PID - process identifier.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

74

Processes
ps - displays information about processes. Note that the ps command differs between different UNIX systems see the local ps man page for details.

To see your current shell's processes:


$ ps
PID TTY TIME CMD
26450 pts/9 0:00 ps
66801 pts/9 0:00 -csh

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

75

Processes
To see a detailed list of all of your processes on a machine (current shell and all other shells):
$ ps uc
USER
jsmith
jsmith
jsmith
jsmith

PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND


26451 0.0 0.0 120 232 pts/9 R 21:01:14 0:00 ps
43520 0.0 1.0 300 660 pts/76 S 19:18:31 0:00 elm
66801 0.0 1.0 348 640 pts/9 S 20:49:20 0:00 csh
112453 0.0 0.0 340 432 pts/76 S Mar 03 0:00 csh

To see a detailed list of every process on a machine:

$ ps ug

USER
PID %CPU %MEM SZ RSS TTY STAT STIME TIME COMMAND
root
0 0.0 0.0 8 8 - S Feb 08 32:57 swapper
root
1 0.1 0.0 252 188 - S Feb 08 39:16 /etc/init
root
514 72.6 0.0 12 8 - R Feb 08 28984:05 kproc
root
771 0.2 0.0 16 16 - S Feb 08 65:14 kproc
root
1028 0.0 0.0 16 16 - S Feb 08 0:00 kproc
root 60010 0.0 0.0 1296 536 - S Mar 07 0:00 -ncd19:0
kdr
60647 0.0 0.0 288 392 pts/87 S Mar 06 0:00 -ksh
manfield 60968 0.0 0.0 268 200 - S 10:12:52 0:00 mwm
kelly 61334 0.0 0.0 424 640 - S 08:18:10 0:00 twm
sjw
61925 0.0 0.0 552 376 - S Mar 06 0:00 rlogin kanaha
mkm
62357 0.0 0.0 460 240 - S Feb 08 0:00 xterm
ishley 62637 0.0 0.0 324 152 pts/106 S Mar 06 0:00 xedit march2
tusciora 62998 0.0 0.0 340 448 - S Mar 06 0:05 xterm -e
dilfeath 63564 0.0 0.0 200 268 - S 07:32:45 0:00 xclock
tusciora 63878 0.0 0.0 548 412 - S Mar 06 0:41 twm
S=Sleeping, R-Runnable, T-Stopped

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

76

Processes
Terminating a process
kill - use the kill command to send a signal to a process. In most cases, this will be a kill signal,
hence the command name. However, other types of signals are usually supported. Note that you
can only kill processes which you own. The command syntax is:
kill [-signal] process_identifier(PID)
Examples:
kill 63878 - kills process 63878
kill -9 1225 - kills (kills!) process 1225. Use if simple kill doesn't work.
kill -STOP 2339 - stops process 2339
kill -CONT 2339 - continues stopped process 2339
kill -l
- list the supported kill signals
You can also use CTRL-C to kill the currently running process.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

77

Processes
Suspend a process: Use CTRL-Z.
Background a process: Normally, commands operate in the foreground - you can not do
additional work until the command completes. Backgrounding a command allows you to
continue working at the shell prompt.
To start a job in the background, use an ampersand (&) when you invoke the command:
myprog & To put an already running job in the background, first suspend it with CRTL-Z
and then use the "bg" command:
myprog
- execute a process
CTRL-Z
- suspend the process
bg
- put suspended process in background

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

78

Unix Level II

Agenda
Shell
Shell programming
Introduction to awk

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

80

Part I
Shell

Shells
Whenever you login to a Unix system you are placed in a program called the shell. All of your work is done within
the shell.
The shell is your interface to the operating system. It acts as a command interpreter; it takes each command and
passes it to the operating system. It then displays the results of this operation on your screen.
There are several shells in widespread use. The most common ones are described below.
Bourne shell (sh)
Original Unix shell written by Steve Bourne of Bell Labs. Available on all UNIX systems. Does not have the
interactive facilites provided by modern shells such as the C shell and Korn shell. The Bourne shell does
provide an easy to use language with which you can write shell scripts.
C shell (csh)
Written at the University of California, Berkley. As it name indicates, it provides a C like language with which
to write shell scripts.
Korn shell (ksh)
Written by David Korn of bell labs. It is now provided as the standard shell on Unix systems. Provides all the
features of the C and TC shells together with a shell programming language similar to that of the original
Bourne shell.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

82

Shells
TC Shell (tcsh)
Available in the public domain. It provides all the features of the C shell together with
EMACS style editing of the command line.
Bourne Again Shell (bash)
Public domain shell written by the Free Software Foundation under their GNU initiative.
Ultimately it is intended to be a full implementation of the IEEE Posix Shell and Tools
specification. Widely used within the academic commnity. Provides all the interactive
features of the C shell (csh) and the Korn shell (ksh). Its programming language is
compatible with the Bourne shell (sh).
Your login shell is usually established by the local System Administrator when your
userid is created. You can determine your login shell with the command:
echo $SHELL

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

83

Shells
Each shell has a default prompt. For the 5 most common shells:

$ (dollar sign)
% (percent sign)

- sh, ksh, bash


- csh, tcsh

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

84

Part II
Shell programming

Shell programs
Contains sequence of commands
Shell reads the file on request and executes commands within.
Shell program is called as Shell Script.
Each script executes in a shell environment.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

86

Scripts:
Besides regular Unix commands we can insert more commands which are program oriented and write
interactive scripts also.
After creating a script, we have to change the mode of the file to x so that it can run as any other Unix
command.
If we have not done so then we have to submit the name of the script file to sh command for executing.
Script file can be created using vi editor.
It may or may not have an extension.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

87

Example 1
# This script demonstrates use of echo in original syntax
echo This is a demo of echo command.
echo Syntax is : echo argument
echo E.g.: echo Happy Learning.
# allows you to enter comments in a program. Anything to the right of # is ignored.
echo command allows you to echo/print the argument on screen.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

88

Handling special characters in script.


Character

Name

Action

'

single quote

the shell will ignore all special characters contained within a pair of single
quotes

"

double quote

the shell will ignore all special characters EXCEPT $ ` \ contained within a
pair of double quotes

backslash

the shell ignores any special character immediately following a backslash

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

89

Example 2
# This script explain how a command can be used in a shell script
echo Good Morning
echo Today is : `date`
` ` - is used to execute a command on the same line as echo.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

90

In our school, we use to do this normally


A=2
B=2
Hence A + B = 4
Here A and B are ?? !!

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

91

Variables
Variables gives a script ability to store data.
Data can be predefined or entered by user during execution.
= operator is used for to assign values to variable.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

92

Example 3
myname=Anup
echo Hello $myname
In the first line, variable myname is created and assigned a value i.e. Anup.
In the second line we are using echo command to print Hello. Alongwith we are using $ to extract the value of
variable and print it on the same line.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

93

Readonly variables
readonly command is used to make a shell variable readonly. Once you execute a command like
readonly my_variable

The shell variable my_variable can no longer be modified.


To get a list of the shell variables that are currently set to read only you run the readonly command without any
parameters.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

94

read command
Allows you to make your program interactive.
Syntax : read variable1 [variable2]
A variable declared in a script is only valid till the script is in execution.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

95

Example 4
# This script demonstrates the use of read command
echo Please enter your name:

#line 1

read name

#line 2

echo Have a nice day, $name

#line 3

Line 1 Ask a question


Line 2 wait for user input
Line 3 give the message and print the variables value.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

96

Example 5
# This script will execute a command if it exists.
echo Please type a command which you want me to execute:
read cmdname
$cmdname

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

97

Example 6
# This script is interactive.
echo I will help you in displaying contents of a file.
echo Please enter a file whose contents you want to display:
read fname
clear
cat $fname
echo
echo
echo
echo Thank you for using 6.script
Only echo command will print a blank line.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

98

Example 7
# This script copies file
echo This program copies a file
echo What file do yo wish to copy \?
read mainfile
echo What name do you desire to give to the new file\?
read newfile
cp $mainfile $newfile
echo $mainfile is copied as $newfile
echo Thank YOU !

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

99

Positional parameters
The most important concept in shell scripts is passing arguments to a script.
The number after the dollar sign indicates the position on the command line.
That is, "$1" indicates the first parameter, and "$2" indicates the second.
Positional Parameters are $1, $2, ..., $9
$0 - Script name

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

100

Example 8
# This program illustrate the use of positional parameters in conjunction with grep command.
clear
echo Program name: $0 #prints the name of the program
echo The number of arguments specified are $# #prints the total number of arguments supplied to the program
echo The arguments are $*

#Prints the actual arguments

grep $1 $2 $3
echo
echo Program over.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

101

Arithmetic !

Arithmetic
UNIX shells do not support any notion of numeric data types such as integer or real.
All shell variables are strings.
Try this:

count=1
Rcount=$count+1

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

103

expr command
The UNIX command expr is used to evaluate expressions. In particular it can be used to
evaluate integer expressions.
Examples:
expr 5 + 6
expr 10 / 5
expr 5 \* 10
expr 5 + 6 * 10
Note that the shell special character * has to be quoted.

The expr command only works with integer arithmetic.


If you need to perform floating point arithmetic have a look at the bc and awk commands.
the parameters for the expr command must be separated by spaces

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

104

Text handling

Agenda
grep
cut
paste
sort

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

106

grep command
grep
Searches through one or more files for lines containing a target and then prints all of the matching lines it finds.
$grep room resources
Will search pattern room in the file resources.

$grep North West partners


Here pattern is North West and is should be enclosed in quotes.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

107

grep - switches
$grep v North partners
Will display those records which do not contain North.

$grep i west partners


Will ignore case differences and display records.

$grep n Wool partners


List line numbers on which pattern is found.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

108

fgrep
Similar as grep.
$fgrep plastics
> jackets partners

$fgrep f patsource partners


-f allows you to input all the patterns in file patsource and fgrep will read from patsource and search each pattern in
partners.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

109

$egrep West|East|North partners


Similar to grep/fgrep.
Allows to specific multiple search patterns on same command line.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

110

Columns & fields


West

56

25000

East

23

13000

North

55

50000

South

78

34000

North East

10

12000

South West

00000

North West

10

11000

South East

22

45000

Tab is delimiter.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

111

$cut f1 resources
Will retreive field 1 from file resources.

$cut c1 resources
Will retreive column 1 from file resources.

$cut f1,4 resources > locationsales


Will retreive column 1 & 4 from file resources.

$cut f2-3 resources > staff


Will retreive column 2 to 3 from file resources.

$cut d: f2 newstaff
Delimiter is : and field 2 is picked.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

112

paste
Joins files together line by line.
Use it to create new tables by gluing together fields or columns from two or more files.
$paste country country_abbrv >> countries
-s option will paste the lines of one file at a time rather than one line from each file.
$paste s regions q3sales

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

113

Sorting
Orders or reorders the lines of a file.
$sort <filename>
$sort o
To replace the input file with the sorted version.
$sort o onefile onefile <E>
$sort f
Sort while ignoring cases.
$sort f partners
$sort t
To specify a new delimiter
$sort t: newstaff
sort -k 2 partners
Sort second field.

sort -t: -k 4 newstaff


Delimiter is : Sort field 4.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

114

Conditional Statements

If statement

Purpose of if statement

It takes two way decision depending on fulfillment of a certain condition.

Syntax
if condition is true
then
execute command
else
execute command
fi

Red indicates not to be included.

Blue indicates optional.

It is concerned with status of commands.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

116

Example 9
# This script illustrates if....else...fi statement
if grep 'Technology' catalog.txt
then

echo

Pattern found in file catalog.txt

echo

Pattern not found

else
fi

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

117

Example 10
# This script illustrates if....else...fi statement
# Input is taken from command line.
clear
echo Give me a pattern to search:
read pattern
echo Give me name of file where I should search:
read fname
if grep $pattern $fname
then
echo
Pattern found in file $fname
else
echo
Pattern not found
fi

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

118

test command
The test command allows you to:
test the length of a string
compare two strings
compare two numbers
check on a file's type
check on a file's permissions
combine conditions together
Syntax:
test an_expression
or
[ an_expression ]
Expression is a string that you want to evaluate.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

119

Example
if [ "$1" = "hello" ]
then
echo "hello to you too!"
else
echo "hello anyway"
fi

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

120

Operators with test command


-eq

equal to

-ne

not equal to

-gt

greater than

-ge

greater than equal to

-lt

less than

-le

less than or equal to

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

121

File related operators with test command


-e file True if file exists.
-f file - True if file exists and is a regular file.
-r file - True if file exists and is readable.
-w file - True if file exists and is writeable
-x file - True if file exists and is executable.
-d file - True if file exists and is directory.
-s file - True if file exists and its size is greater than zero.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

122

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

123

Example 11
# llustrates the use of relational operator used by if
if test $# -eq 0
then echo Cannot continue as no filename was supplied.
else
cat $1
fi

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

124

Example 12
# Relational operator test
echo Your age
read mage
if test $mage = 18
then echo Vote
else
echo cannot vote
fi

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

125

case
It matches an expression for more than one alternative and uses a compact construct to permit multi-way
branching.
It handles string tests in more efficient manner than if.
Syntax:
case expression in
pattern1) execute commands ; ;
pattern2) execute commands ; ;
...
esac

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

126

Example 14
# Illustrates case statement
clear
echo "
Command Menu"
echo " 1. List files."
echo " 2. Today's date."
echo " 3. Who all are working on Unix"
echo " 4. Exit"
read choice
case "$choice" in
1) ls -l ;;
2) date ;;
3) who ;;
4) exit ;;
esac

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

127

while
Loops let you perform a set of instructions repeatedly.
As long as condition is true, commands are executed.
Syntax
while condition is true
do
execute commands
done

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

128

Example 15
# Illustrates whle loop
ans=y
while [ "$ans" = "y" ]
do
echo "Enter the name and code of employee: "
read ename ecode
echo "$ename | $ecode" >> emplist
echo "More entry (y/n)? "
read respons
case $respons in
y*|Y*) ans=y;;
n*|N*) ans=n;;
*) ans=y;;
esac
done

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

129

for statement.
It checks for a value in a list.
A list of commands is executed for each value in the list.
Syntax
for variable in list
do
execute command # loop body

done

The loop body is executed as many times as there are items in the list.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

130

Example
for x in 1 2 4 5
do
echo The value of x is $x
done

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

131

Introduction to awk programming

Pre-requisites
You should already be familiar with basic system commands, such as cat and ls,
Basic shell facilities, such as Input/Output (I/O) redirection and pipes.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

133

There are three variations of AWK:


AWK - the original from AT&T
NAWK - A newer, improved version from AT&T
GAWK - The Free Software foundation's version

We will cover AWK.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

134

History of awk.
Three designers were involved :
Alfred V. Aho
Peter J. Weinberger
Brian W. Kernighan

The original version of awk was written in 1977 at AT&T Bell Laboratories.
The GNU implementation, gawk, was written in 1986 by Paul Rubin and Jay Fenlason, with advice from Richard
Stallman.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

135

Uses of AWK.
The AWK text-processing language is useful for such tasks as:

The basic function of awk is to search files for lines or other text units containing one or
more patterns.
Tallying information from text files and creating reports from the results.
Translating files from one format to another.
When you run awk, you specify an awk program that tells awk what to do.
Creating small databases.
Performing mathematical operations on files of numeric data.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

136

Limitations
It is not really well suited for extremely large, complicated tasks.
It is also an "interpreted" language -- that is, an Awk program cannot run on its own, it must be executed by the
Awk utility itself.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

137

Syntax:
awk PROGRAM inputfile(s)
awk -f PROGRAM-FILE inputfile(s)

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

138

Initial examples:
$ ls -l | awk '{ print $5 $9 }

ls command output is 9 columns

awk is printing from col 5 to 9.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

139

Example 1
Coins.txt
metal weight in ounces date minted country of origin description
gold
gold
silver
gold
gold
gold
gold
silver
gold
silver
silver
gold
gold

1
1
10
1
1
0.5
0.1
1
0.25
0.5
1
0.25
1

1986
1908
1981
1984
1979
1981
1986
1986
1986
1986
1987
1987
1988

USA
American Eagle
Austria-Hungary Franz Korona
USA
ingot
Switzerland
ingot
RSA
Krugerrand
RSA
Krugerrand
PRC
Panda
USA
Liberty dollar
USA
Liberty 5-dollar piece
USA
Liberty 50-cent piece
USA
Constitution dollar
USA
Constitution 5-dollar piece
Canada
Maple Leaf

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

140

Example 1
awk '/gold/' coins.txt
This tells Awk to search through the file for lines of text that contain the string "gold", and print them out.

awk '/gold/ {print $5}' coins.txt


Line containing gold will printed. $5 means fifth field i.e description.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

141

simplest general form of an Awk program:


awk <search pattern> {<program actions>}
Awk, makes no strong distinction between strings and numbers.
Not "strongly-typed.
All the fields are regarded as strings, but if that string also happens to represent a number, numeric
operations can be performed on it.
We can perform an arithmetic comparison on the date field.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

142

Format of AWK
awk 'BEGIN {<initializations>}
<search pattern 1> {<program actions>}
<search pattern 2> {<program actions>}
...
END {<final actions>}'
The BEGIN clause performs any initializations required before Awk starts scanning the input file.
The subsequent body of the Awk program consists of a series of search patterns, each with its own program action.
Awk scans each line of the input file for each search pattern, and performs the appropriate actions for each string found.
Once the file has been scanned, an END clause can be used to perform any final actions required.

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

143

Example 2
awk 'END {print NR,"coins"}' coins.txt
Explanation:
This example doesn't perform any processing on the input lines themselves. Its scans
through the file and perform a final action: print the number of lines in the file, which is given
by the "NR" variable. NR stands for Number of Records
awk 'END {print NF,"coins"}' coins.txt
NF gives the number of fields in a line

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

144

Example 3
Data file: revenues
Program file : total.awk.

{ total=total + $5 } { print "Send bill for " $5 " dollar to " $4 }


END { print "---------------------------------\nTotal revenue: " total }
$ awk f total.awk revenues

The information contained in this presentation is proprietary.


Copyright 2011 Capgemini. All rights reserved.

145

Thank you.

You might also like