Beruflich Dokumente
Kultur Dokumente
TCS Confidential
TCS Confidential
Contents :
1.
2.
File System
3.
Basic Utilities
4.
Shell Features
5.
Advanced Utilities
6.
Communication Features
7.
System Calls
TCS Confidential
Chapter 1
Introduction
of UNIX
and
History
TCS Confidential
Introduction and
of UNIX
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
Evolution
Development
Standard release- AT&T versions
BSD UNIX
5
Other implementations
of UNIX
Features of UNIX
UNIX Philosophy
UNIX operating system
UNIX Kernel
Programming Environment
Design Principles
Limitations
History
TCS Confidential
Evolution
645
Development
*
TCS Confidential
Version 6
Version 7
Version 8
Written in C
Moved to PDP - 11/45 and
PDP - 11/70
Released in 1975
8 use by universities only
For
Released in 1978
First commercial version but
primarly used in universities
32V - VAX version
UNIX Support Group(USG)
takes responsibility
Research Group still busy
TCS Confidential
8
System V
Release 1
System V
Release 2
System V
Release 3
System V
Release 4.0
Released in 1981
Commercial orientation
Over 100 manufacturers of UNIX-based
micro , mini and mainframes
UNIX9 users > 100,000
Released in 1983
Enhancement of System III
Released in 1984
Enhancements of System V , Release 1
Released in 1988
TCS Confidential
BSD UNIX
(from Berkeley Software
Distributions)
* UNIX's entry into University of California, Berkeley Campus during
1976-77
* Inputs of UCB Faculty/researchers incorporated in System V of
10
AT&T UNIX
UNIX 3BSD
Released in 1978
Developed by Bill Joy and Ozalp Baboglu
Added Virtual memory, Demand paging
to the VAX version 32V
UNIX 4BSD
Released in 1983
10
Xenix
IBM PC/IX
Zeus
DEC Ultrix
HP-UX
UNIX
Elxsi UNIX
Pyramid UNIX
Data General UNIX
Perkin Elmer UNIX
MassComp UNIX
NBI UNIX
Amdahl UNIX
UNIX Features
Multitasking
Performing tasks simultaneously
rather than
sequentially
e.g., While printing a document , start
editing
another document
Each task is considered to be a process
Multi-user
computer
12
simultaneously
i.e , more than one keyboard and
terminal
can be connected to one computer
Highly cost-effective
Portability
UNIX Features
Basic Operation of
13
system like
interpreter or Shell
Tools
TCS Confidential
13
*
*
*
*
*
*
Modular approach
i.e., be prepared to throw and rebuild
Usage of tools
15
Kernel
Schedules 16
tasks and manages data storage
Shell
A program that
interprets the user commands/requests
calls programs from memory and
executes them one at a time or in a
series
(called a pipe)
Tools &
OS
TCS Confidential
16
The Users
Shells and Commands
Compilers and Interpreters
System Libraries
System Call Interface to Kernel
Signal
File System
Scheduling
Terminal
Handling
Swapping
CPU
Page
17
Replacement
Character
I/O System
Block
I/O System
Demand Paging
Terminal
Drivers
Virtual
Memory
Device
Controller
Terminals
Memory
Controller
Physical TCS Confidential
Memory
17
Networking
Services
File Management
And Security
Input / Output
Services
18
UNIX
Signal
Handling
SYSTEM
KERNEL
Process
Scheduling
System
Memory
Administration
Management
and Accounting
TCS Confidential
18
19
19
*
*
*
*
*
*
*
20
Methods to Co-ordinate Process
Signal to communicate with processes
Files, Directories, Devices treated as files
Tree structured directories to hold files
Emphasis on program development facilities
Sources available on-line
Simplicity before efficiency
TCS Confidential
20
Drawbacks of UNIX
TCS Confidential
21
Chapter 2
22
File System
TCS Confidential
22
1.
2.
3.
4.
5.
6.
7.
8.
9.
23
Super
block
Inode
list
Data
block
24
Boot block
-
Super block
Inode list
Data block
TCS Confidential
24
Ordinary Files
Directory Files
Special Files25
Standard Files
TCS Confidential
25
Examples
Text Files
26
Commands
Sequence of commands
interpreted by UNIX text
Data
Executable
TCS Confidential
26
TCS Confidential
27
* Types :
Character file
by
Block file
or 1024
28
Terminal (tty0, tty1 )
Disk Drives (hd0,fd0)
TCS Confidential
File Names
*
30
Path Names
Absolute Path name
*
e.g.,
/usr/trg/c/test.c
where
31
e.g.,
If current directory is
/usr/trg
then the file could be just referenced as c/test.c
TCS Confidential
31
Features
* Hierarchical
* Security on each file
- Owner
- Group
- All others
*
Removable
File Independence -
TCS Confidential
32
Unix
dev
bin
lib
/
bin
console
lp0
tty0
src
sh
csh
--Libc.a
---
uch
33
tmp
man
---
bin
local
lib
---
usr
include
spool
etc
spell
troff
---
*.h
tmac
passwd
group
init
---
lib
troff
---
tmp
---
TCS Confidential
33
/dev
fd0, lp0
/etc
Administrative Programs
passwd, shutdown
/lib
Libraries used 34
by Unix
libc.a
/usr/bin
Unix utilities
cal, bc,spell
/usr/adm
/tmp
Administrative commands
and files
cp, mv, ln
adduser
34
/usr/games
Game Programs
/usr/include
procedure
/usr/lib
35
/usr/mail
Mail files
Example : mailbox
/usr/news
News files
/usr/spool
Spool files
/usr/tmp
Temporary files
/usr/src
TCS Confidential
35
Access Mode
Directory
Ordinary
Read
Examination36
of
File Contents
Listing of
files within
Directory
Write
Allows changing
of file contents
Creating new
files within
Directory
Execute
Executing file as
a command
allowed
Searching the
Directory
allowed
TCS Confidential
36
login :
User can type his name and password to
identify himself
login command can be used as
$ exec login
to log-on onto another user account after identifying yourself
in response to prompts for user name and password
su
setuser
37
$su - trg2 -c
TCSls
Confidential
Temporarily changes to trg2 and executes the command
-l
and
3
comes back to the original user
37
$ logout
or
$ exit
or
$ <ctrl-d>
TCS Confidential
38
Directory
Security
Management
Operation
cd
pwd
mkdir
rmdir
mvdir
File Comp.
cmp
comm
passwd
crypt
chown
chgrp
umask
chmod
39
File
contents
File
compression
Mountable
file
cat
ls
wc
file
pack
unpack
mount
umount
Copy, Move
Remove & Time
cp
ln
mv
rm
touch
TCS Confidential
39
mkdir
rm
removes a file
rmdir
removes a directory
du
df
touch
find
file
pwd
directory
40
TCS Confidential
40
Command Structure
General Structure:
Command Name
[Options]
[Arguments]
E.g.,
Command Name
Tells the shell what to do
(in this case list files)
Options
Control how the
41command will work
(in this case ask for a long listing)
Arguments
on which the command works
(in this case the directory
usr/trg )
ls
-l
/usr/trg
TCS Confidential
41
Directory Management
cd
cd..
cd.
e.g.,
$ cd /usr/trg/c
$ cd ..
$ cd ./c
or $ cd c
$ cd
mkdir
$ mkdir pathname
42
TCS Confidential
rmdir
*
rm -r
Remove a Directory
$ rmdir pathname
e.g.,
$ rmdir /usr/trg2
(removes directory
trg2)
pwd
Directory
Print Working
TCS Confidential
43
File Contents
cat
Options -
characters
-b
lines
e.g.,
$ cat try.c
$ cat
displays on
44
$ cat f1 > f2
$ cat f2 > f3
$ cat f4 >> f3
$ cat try[0-3] > final
$ cat test* > report
45
Is[Options]
Options
-1
-l
-a
-s
-i
inode no.
-t
TCS Confidential
46
$ Is -I
List the files along with the protection bits and the user
$ Is -a
$ Is -1
symtab.c
symtab.o
treegen
test
$ Is -I
$ Is -a
$ Is -iI
..
.profile
.cshrc
symtab.c
...
47
wc
Word Count
$wc [Options] filename
Options
e.g.,
nl
the file
Display
-I
Display
-w
Display
-c
Display
48
$ wc test.c
20 200 5678
20 - lines
200 - words
5678- characters
no.
no.
no.
no.
of
of
of
of
TCS Confidential
48
file
Options
-f
filelist
Normal File Types
C49program text
assembler program text
commands text
ASCII text
English text
e.g.,
$ file test.c
C Program test
TCS Confidential
49
cp
copy a file
-i
e.g.,
In
e.g.,
$ ln first.c second.c
The file is referenced by two different names
No Extra disk storage
TCS Confidential
50
mv
interactive mode
e.g.,
rm
$ mv old.c new.c
Renames the file old.c as new.c
Deletes the indicated file(s) files
rm
-i
-f
-r
touch
e.g.,
TCS Confidential
51
File Comparison
cmp
0
1
2
e.g.,
TCS Confidential
52
diff
convert
53
e.g.,
n1 a n3,n4
n1,n2 d n3
n1,n1 c n3,n4
where
*
*
respectively
TCS Confidential
53
comm
$comm -[123] f1 f2
Prints a three column output:
- lines that occur only in f1
- lines that occur only in f2
- lines that occur in both
comm -12
- prints
lines common to the two files
54
comm -23
- prints only lines in the first file but not in
the
second
comm -123
- prints nothing
e.g.,
$ comm test1 test2
54
File Compression
pack
$ pack <filename>
e.g.,
$ pack try
- Creates a file try.z which is packed
- Normally the executables are packed
- The size is reduced by 25 - 40 %
55
unpack
or
pcat
e.g.,
$ unpack try.z
or
$ pcat try.z
unpacks the file try.z
TCS Confidential
55
mount
e.g.,
umount
e.g.,
TCS Confidential
56
File Security
passwd
chown
file
To change
the ownership of the
57
$ chown owner filename
chmod
58
e.g.,
$ chmod a=rw test.c
* users, group, others have read
and write permissions
$ chmod u+r, g+w, o+x test.c
* read for users write for groups execute for others
$ chmod 777 test.c
* Sets read, write, execute Permissions
TCS Confidential
58
umask
$ umask nnn
(nnn set file creation mode)
umask can also be set as a shell variable
59
e.g.,
umask 022
- Files normally created with 777 mode is assigned
755 permission
The value of each digit is subtracted from the corresponding "digit"
specified by the system for the creation of a file.
TCS Confidential
59
File Operation
tail
head
split
lines
60
terminal
60
Chapter 3
61
BASIC
UTILITIES
TCS Confidential
61
Basic Utilities
1.
Line Editor
Ed
2.
Visual Editor Vi
3.
62
Debuggers
4.
Profiling Tools
5.
C-Beautification
TCS Confidential
62
$ ed filename
w
Exiting ed :
q
Quit
63from ed
Example :
$ ed test.c
? test.c
P
*a
<>
<>
<>
*w
*q
The edit buffer can be written to some other file also as *w filename
TCS Confidential
63
Pattern Matching
abc
abc
a*bc
abc,
aabc,
aaa...bc
aac,
acc,
axc,
abc,
axyc,
a$+pmc,
a[b2m]c
abc,
a2c,
amc
a[0-2b-d]c
adc
a0c,
a1c,
a2c,
a[^0-2]c
a3c,
a5c,
axc, ....
a[xy]*c
ac,
axc,
axxc,
a\{3,\}
aaa,
aaaa -
3 or more as
a\{3,5\}
aaa,
aaaa,
aaaaa
between 3 & 5 as
a.c
a.*c
abcdefgc
abc,
64
a$c
abc,
acc,
ayyyc
TCS Confidential
64
Explanation
print current line (s)
list current line(s)
(display invisible
65
print line(s) with
1,3p
2,4l
characters like tabs etc.)
number before it
1,5n
a
4a
3i
TCS Confidential
65
line
Command
Explanation
.=
Current line is n
+n
Examples
*-
*-
Previous
Current line
Move up by n lines
66
-n
/.../
*$= Prints
total no. lines
but does not
changecurrent
line
TCS Confidential
66
Modifying Text
Command
Explanation
Usage
d
2,5d
c
2,4c
2,3m5
2,3t5
j
u
67
2,3j
Substitute Commands
(n,n)s/oldstr/newstr
replaces oldstr with newstr (n,n) range of
lines - optional
s/oldstr/newstr/g all oldstrs' on the current line
are replaced with newstr
TCS Confidential
s/oldstr/newstr/p the replacement is only effected in print but not
executed
67
Finding Text
/^ pattern/
/pattern $/
line
/pattern/
//
??
68
forward search
backward search
TCS Confidential
68
Command Execution
Command
Explanation
69
TCS Confidential
69
Special ed Commands
Command
Explanation
w file2
read current
70 file
r file2
e file2
!shellcmd
TCS Confidential
70
The ex editor
*
an improved version of ed
*
less terse
*
provides display
options like
71
numbered
line
*
allows shorthand versions of
commands
*
clear response for error messages
TCS Confidential
71
editor
*
*
*
*
*
*
Invoking
Modes :
*
*
*
Command mode
Insert mode
From Insert mode Pressing
<ESC> remitts Command mode
TCS Confidential
72
Types of commands
* vi-commands (invisible)
Command mode
Commands can be categorised as :
* Cursor movement
* Text manipulations
73
- insert, delete, copy, change
* Marking/Selecting, Positioning
* Search
Objects of interest recognized by vi:
* characters
* words
* lines
* block
TCS Confidential
73
Format of commands
[count] command [operand]
Use . to repeat last command
Use u to undo last command
Cursor Movement
Line Oriented :
74
^ or (zero)
$
Character oriented
h
l
j
k
beginning of line
end of line
move
move
move
move
left
right
down
up
TCS Confidential
74
75
Block Oriented :
)
^g
& )
TCS Confidential
76
Text Manipulation
Insert mode :
a
A
i
I
o
O
esc
Delete mode :
u
nx
ndw
ndd
77
Changing Text :
mode
nr
s
replace char
delete current char, enter insert
S
cw
78
nyy
yank n line
Named Buffers : a to z ( 26 )
Unnamed Buffers : 1
print buffer
Join :
TCS Confidential
78
Search
Searching for a character :
fc
tc
similar to79
f, cursor placed 1 char left of c
Fc
Tc
continue
search
in
reverse direction
TCS Confidential
79
Search
File Related :
G
nG
80
TCS Confidential
80
Text Manipulation
Screen Adjustments :
^d
^u
^f
^b
Page backward
^e
^y
81
Text Manipulation
Searching for a string :
82
/string
?string
82
set noai
set nu
set wrapmargin = n
set bf
set
83
Text Manipulation
:w!
:q
:q!
quit normally
84
quit, no write
:n
:n!
:n args
84
Debuggers :
Why ?
*
for running a Program under the control of the programmer
*
for examining the values of various variables and stack
contents
Types
fsdb
kdb
Kernel Debugger
Resides in kernel,
Allows memory examination
Disassembles instructions
Executes programs
sdb
Role
Debugger - sdb
*
Invoked as
sdb <executable file>
e.g.,
$ cc -g test.c
$ sdb a.out
86
were
86
SDB Commands
Command
Explanation
*r <arguments>
*s
*20b
Explanation
*S
*c
*variable name
*variable name/x
*variable name/c
*variable name/o
*variable name/s
*variable name/a
*function:variable
88
TCS Confidential
88
Explanation
*p
*<line-no>p
*<ctr-d>
*w
*<Enter-key>
*!<command>
*k
*q
89
CTRACE - Debugger
TCS Confidential
90
CTRACE - Debugger
*
(Contd .)
Invoked as
ctrace [options] < source-file > traced-file
options :
-o
-x
-u
-e
-f <function-names>
-v <function-names>
except
91
e.g.,
$ ctrace < test.c > ctest.c
test.c ctest.c -
source file
traced file
TCS Confidential
92
PROFILING TOOLS :
PROF
and
GPROF
*
The program should
93have been compiled with -p
option
for prof and -g option for gprof
*
*
Gives an idea as to which function used up the
TCS Confidential
maximum
time
93
Example of prof
$ cc -p test.c
$ a.out
$ prof a.out
Generates an output like....
%time
msec/call
91.2
7.2
1.0
cumsecs
name
9.34
9.34
10.08
0.74
10.18
0.10
0.0
10.24
%time
cumsecs
seconds
0.06
seconds
20100
94
#calls
_func2
1
100
1
calls
name
Function name
acnt
_func1
_main
TCS Confidential
94
CB - C Beautification
* Beautifies the C-program code
* Indents all the statements
* Improves readability
* Invoked as
95
$ cb < source-file > target-file
e.g.,
$ cb < test.c > test1.c
test.c - source file
test1.c - Beautified version of test.c
TCS Confidential
95
Chapter 4
96
Shell Features
TCS Confidential
96
Shell Features
Shell Features
1.
2.
3.
4.
5.
6.
7.
8.
97
1.
Bourne Shell
sh
2.
C Shell
csh
98
3.
Korn Shell
4.
Restricted Shell
5.
Visual Shell
ksh
rsh
vsh
TCS Confidential
98
Shell Commands
Simple commands using shell Metacharacters
Exit status
Redirection
file
file
Pipeline
Default
Default
<
>
>>
n>&m
99
sequential execution
&
asynchronous execution
&&
returns
100
||
returns
$var
TCS Confidential
1
100
...
...
...
... and \
e.g.,
$ (sleep 5 ; echo The date is `date` ) & date
The Output of the above command will be
6345
Wed Sep 23 10:20:45 EDT 1991
$The date is Wed Sep 23 10:20:50 EDT 1991
TCS Confidential
1
101
PATH
*
*
e.g.,
PATH = :.:/usr/bin:/bin:
TCS Confidential
1
102
Executes .logout for C Shell when user logs out from C-Shell
103
Keeps track of the history mechanism In C Shell through the .history
TCS Confidential
1
103
Shell Variables
User defined
assignment : name = value
e.g., (on Bourne shell)
$
$
$
$
$
PATH=$PATH:/usr/games
export PATH
echo $PATH
:.:/usr/bin:/bin:/usr/games:
TCS Confidential
1
104
Positional Parameters
e.g.,
105
shellscript
arg2
$0
arg1
$1
$2
TCS Confidential
1
105
* ${var}
characters
e.g.,
$
$
var=hello 106
varx=goodbye
echo $var
hello
echo $varx
goodbye
echo ${var}x
hellox
TCS Confidential
1
106
107
echo ${junk?}
junk : parameter not set
${var-thing}
1
107
* ${var=thing}
thing
e.g.,
$
$
echo ${junk?}
Hi there
* $ {var+thing}
nothing
TCS Confidential
If var defined then thing Otherwise
1
108
Command Environment
*
109
Shell 1
Shell 2
Export A
e.g.,
export A
*
TCS Confidential
1
109
$?
$$
$*
$!
1
110
HOME
login dir
PATH
PS1
PS2
secondary
111
system prompt
IFS
TERM
terminal type
EXINIT
TCS Confidential
1
111
The C Shell
More helpful112interaction
Permits aliasing
TCS Confidential
1
112
% set x=date
% echo $x
Wed Sep 23 12:15:20 EDT 1991
TCS Confidential
1
113
History
execute previous
Command Format :
! <event> [ <word> <modifier> ]
<event>
- absolute
- relative
- name
expr
114
<word>-
!1
!-3
!c
!?su?
absolute
0, 1, ...n
range
a-e
last
$
All arguments *
<modifier>
- Substitute
:s/trg1/trg
- Print only
:s/trg1/trg/p
TCS Confidential
1
114
History Mechanism
Keeps track of commands
% history
% ^old^new
e.g.,
% cd /usr/trg/SRC
% ^SRC^src
% cd /usr/trg/src
% !15
% !c
beginning with c
Run command 15
Run previous command
TCS Confidential
1
115
Changing
Commands
:S/pattern/replacement
e.g.,
% !cd:s/trg/trg1
116
cd /usr/trg1/src
% !ls : p
Prints the previous command
beginning with ls
but does not execute it
TCS Confidential
1
116
Using Arguments
!$
last argument
!^
Begin argument
!*
All
arguments
117
!10:5-9
Arguments 5 to 9 for
command 10
e.g.,
%ls !15:2-4
TCS Confidential
1
117
Alias mechanism
Short names for frequently-used long commands
e.g.,
%
%
%
1
118
Filename Grouping
{
%cp
%ls
{/usr/bin,/bin}/{ls,more}
/usr/bin/ls : not found
/bin/ls
/usr/bin/more
/bin/more
: not found
TCS Confidential
1
119
Using C - Shell
e.g.,
% cc test.c
% vi test.c
% !c
% a.out
% !v
120
% !c - o test
% test
% car /usr/trg2/document/report
% !! : s/car/cat
or
% ^car ^cat
TCS Confidential
1
120
% set variable=value
histchars :
% set histchars=";,"
will thereafter use
; as execution character
121
, as substitution
character
history :
% set history=40
path:
% set path=:.:/bin:/usr/bin:
prompt :
% set prompt=\!>
TCS Confidential
1
121
setenv
filec
% set filec
Its usage :
:
file name completion
:
Predefined
% set variable
122
% unset variable
ignoreeof
% set ignoreeof
noclobber
:
redirection
% set noclobber
appends to
1
122
Test Conditions
TESTing Strings
test - z $string String length equal to zero
test - n $string String length not equal to zero
123
test $string
1
123
File related
$ test -f file
124
True if file exists and is an ordinary file
$ test -r file
TCS Confidential
1
124
Shell Programming
- The Bourne Shell
Control flows
if command
then
commands
[ elif commands ] ...
[ else commands ]
125
fi
case word in,
pattern) commands ;;
pattern) commands ;;
...
esac
TCS Confidential
1
125
1
126
break n
continue n
exit n
export [name..]
export
127
test expr
evaluate conditions
read [arg]
shift n
TCS Confidential
1
127
Examples
$
>
>
>
>
>
>
>
for i in *.dat
do
size=wc -c $i
if test $size -eq 0
then
rm $i
fi
done
128
1
128
Example
1
129
IF
Control Flows
if (expr) then
command;
[ else if ]
command;
[ else ]
command;
endif
SWITCH
switch (string)
130
command;
breaksw
case string1:
case string2:
command;
breaksw
default :
command;
breaksw
TCS Confidential
1
130
FOREACH
WHILE
while
131( expr )
commands
end
UNTIL
until ( expr )
commands
end
TCS Confidential
1
131
Chapter 5
132
Advanced Utilities
TCS Confidential
1
132
Advanced Utilities
1.
2.
3.
4.
5.
6.
7.
join, csplit
8.
9.
10.
11.
12.
13.
14.
15.
16.
Redirection
Pipes
Pipe fitting with tee utility
Filters
Find
Sort
Utilities that process tabular data- cut,
Ps and Kill133
Timely Execution- nohup, at, time
User backup utilities- tar, cpio, dd , doscp
Output related commands- pr, lpr,lp
Other utilities- sleep, sync, wait, clear, tr
Introduction to Make
Lint
Lex
Yacc
TCS Confidential
1
133
Redirection
e.g.,
Metacharacter
Performs
<
input
>
output write
>>
output append
$ who
trg1
trg2
tty00
tty02
Apr 8
Apr 8
09:33
11:10
134
Apr 8
Apr 8
09:33
11:10
$ cat date_out
Fri
Apr 8
14 : 30 :
10 est
14 : 30 : 10 est
Apr 8
09:33
Apr 8
11:10
1983
TCS Confidential
1
134
Pipes :
Metacharacter
Performs
e.g.,
piping
135
Without Pipe
With Pipe
1
135
A UNIX Pipeline
Standard Input
>Prgm #1
Prgm #2
Prgm #3
Prgm #4
136
>Prgm #5
Prgm #6
Standard Output<
TCS Confidential
1
136
1
137
FILTERS :
grep
fgrep
egrep
powerful
time
TCS Confidential
1
138
-c
-i
ignore case
-n
-v
139
TCS Confidential
1
139
e.g.,
$ grep -ni func1 *.c
that match
ignoring the case
$ grep * *
$ ls -l | grep ^d
1
140
e.g.,
$ egrep int|long test.c
Searches for all those lines
containing
either int or long in test.c
141
$ egrep (^[A-Z]) testfile Searches for all the lines which
start with a capital letter
$ cat exprfile
commas
,.*,.*,
exprfile
$ egrep -n -f exprfile testfile
TCS Confidential
1
141
Find
To locate one or more files
find path-list cond-list action-list
path-list
cond-list
-type x
142
-user name
-group name
-size n
-links n
-atime n
-mtime n
-ctime n
-perm nnn
-name fname
TCS Confidential
1
142
actionlist
- print
- exec command
- cpio device
143
- ok command
e.g.,
find / -atime 7 -print
find / -atime +7 -print
find / -atime -7 -print
1
143
Examples
$
$
$
$
$
find
find
find
find
find
TCS Confidential
1
144
Sort
*
*
line.
145
ignore case
n
r
tp
u
o filenameout
numeric sorting
reverse order
use p as separator
output only lines with unique keys
save output in filenameout
TCS Confidential
1
145
e.g.,
$ sort -t: +2n -3 /etc/passwd
Print the passwd file sorted by numeric user id
146
of the user
TCS Confidential
1
146
uniq
tsort
1
147
*
which
TCS Confidential
1
148
Cut (contd.)
cut -c list { file1 file2 ...}
e.g.,
cut -c 5-70 file1
cut would pass 5-70 characters from file1
cut -flist {-d char } {file1 file2....}
149
e.g.,
who | cut -d -f1
gives a list of user login names
cut -d: -f 1,5 /etc/passwd
gives a list of user ID and names
TCS Confidential
1
149
JOIN
*
*
files
150
join
on the mth field of file n
-on.m
-tc
1
150
e.g.,
$ join -j1 4 -j2 3 -o 1.1 2.1 1.6 -t: etc/passwd
etc/group
-
151
login
name
group
name
login
dir
TCS Confidential
1
151
csplit
context split
-k
xx
1
152
csplit(contd.)
e.g.,
$ csplit -f cobol inpfile /Procedure division/
/ Para5./ /Para6./ /Para 7./
*
*
*
1
153
PS, kill
$ somecommand &
5511 - pid
$ps
pid
tty
3432
5765
5511
2
2
2
$ kill 5511
$ ps
pid
3432
5985
time
0 : 24
154 0 : 03
0 : 51
tty
time
2
2
0 : 24
0 : 03
command
-sh
ps
somecommand
command
-sh
ps
TCS Confidential
1
154
$ stubborn-cmd &
pid
3432
6004
5995
$ kill 5995
tty
2
2
2
$ ps
pid
tty
3432 2
6004 2
5995 2
$ kill -9 5995
$ ps
pid
3432
6103
tty
2
2
155
time
0 : 24
0 : 03
0 : 44
command
-sh
ps
stubborn-cmd
time
0 : 24
0 : 03
0 : 44
command
-sh
ps
stubborn-cmd
time
0 : 24
0 : 03
command
-sh
ps
TCS Confidential
1
155
Timely Execution :
time
Time a command
TCS Confidential
1
156
nice
*
*
$ nice +n
Raise priority
$ nice -n
Lower priority
$ nice n
set
priority
157
at
$ at 5 pm
echo ^G ^G Time to logoff >/dev/tty04
^D
$
TCS Confidential
1
157
tar
tape archiver
Copies files on backup medium such as floppy
or tape in tar format
158
c
r
t
x
v
f
TCS Confidential
1
158
tar (example)
$ tar cvf /dev/fd096 *
directory
medium /dev/fd096
$ tar xvf /dev/fd096 Extracts all the files from the backup
159
medium
/dev/fd096 onto the
current directory
Creates necessary directories
tar xvf /dev/fd096 try.c
/dev/fd096
tar tvf /dev/fd096
1
159
cpio
*
Copy files archives in and out
*
Bundles all the files into one package
cpio [options] file list devicename
Options
-o
-i
-p
-v
-c
character information in ASCII for portability
considerations
-t
-O
-d
-k
TCS Confidential
1
160
cpio (Examples)
161
$ cpio -ocBv < filelst > /dev/rct/c0s0
Copies all the files mentioned in filelst onto the catridge tape
$ cpio -icBdv < /dev/rct/c0s0
Extracts all the files from device c0s0 and creates necessary required
directories
$ cpio -itv < /dev/rmt/0m
List the files from the tape /dev/rmt/0m
TCS Confidential
1
161
dd
dd {option = value }
Options
Values
if
of
ibs
obs
cbs
skip
seek
count
conv
e.g.,
$ dd if=/dev/rmt/0n of=x ibs=800 cbs=80 conv=ascii,lcase
TCS Confidential
1
162
doscp
163
1
163
pr
Options
-k
K col. Output
+k
from page k
-lk
-p
pause after164
each page
-h
-wk
-d
double space
e.g.,
$ pr -3 d h file list f1 f2
Generates a header as file list
3 Column output
Double spacing
$ pr -5 wordlist
Generates 5 column output
TCS Confidential
1
164
pr (contd.)
$ pr myfile
prepare myfile
165 1987 myfile page 1
nov 24 : 11 : 31
$ pr -t myfile
suppresses the header
This file can be printed using
$ pr myfile | lpr
TCS Confidential
1
165
lpr
$ lpr myfile
$ lpr -c myfile
$ lp myfile
1
166
Other Utilities
sleep
Suspend execution
$ sleep 5
$ sync
ensure file
$ wait
$du
1
167
df
clear
goes to
tr
e.g.,
TCS Confidential
1
168
make - An Introduction
Making & Maintaining large programs
What is make?
of programs
to-date
Characteristics
system and t
actions to
1
169
TCS Confidential
mod1
mod2
mod3
1
170
1
171
1
172
the
TCS Confidential
1
173
Contd...
Example :
test.c
#include <stdio.h>
main(argc,argv)
int argc;
char **argv;
{
int i,j,k,l,number,num2;
int int1, int2;
int arr[20];
174
scanf( %d,num2);
printf(The square root of %d is %f \n,
number , sqrt(number) );
i=arr[j++];
add(int1,int2);
}
add (int1,int2,res)
int int1,int2, *res;
{
printf("The value %d", int 1 + int 2);
}
TCS Confidential
1
174
Contd...
warning
warning
175
TCS Confidential
1
175
Lex
definitions
% %
rules
% %
user subroutines
TCS Confidential
1
176
Lex Contd...
Example (on rule)
Expression > integer * integer
Expression > integer + integer
Expression > integer - integer
Expression > integer / integer
The lex specification file
%%
[0-9]+
[-*+/]
[\t ] ;
177
{ return (INT); }
{ return (OPR); }
{
printf(Lexical analyser error\n);
exit(-1);
}
TCS Confidential
1
177
Lex Contd...
%%
yywrap()
{
return(1);
}
yywrap() indicates no further
178input available
Invoked as
$ lex filename
$ lex lex.l
Generates
lex.yy.c
TCS Confidential
1
178
Yacc :
What is it?
Features
program
basic item
1
179
Yacc Contd...
Specifications
Example
Declarations
%%
rules
%%
$ yacc filename
$ yacc yac.y
y.tab.c
TCS Confidential
1
180
1
181
Chapter 6
182
Communication Features
TCS Confidential
1
182
Communication Features
1.
2.
1
183
Write
Talk
Post
News
Mesg
184
TCS Confidential
1
184
users
Sending mail on local system
At the end of the message press ^D to exit
$ mail <loginnames>
185
1
185
Receiving mail
$ mail
?
q
s [file]
w [file]
d
n or +
r
m [user]
! cmd
Prompt
quit
save (defalut mbox)
write without header
delete
next message
186
previous
reply to the sender
mail to user
run the shell command
TCS Confidential
1
186
Write
from
another terminal
187
The message
is ended by pressing ^D.
$ write <username>
Talk
two
different
terminals.
$ talk <username>
TCS Confidential
1
187
Post
Messages can be posted on a bulletin board for
previewing by all users.
News
first
*
Keeps the user informed of current events
Refers file /usr/news
e.g.,
$ news
$ mesg [option]
option : - y
write allowed
-n
mesg is y or n
$ mesg
1
188
Networking Commands
*
uucp
uuto
telnet
rlogin
ftp
rcp
uux
ct
cu
189
TCS Confidential
1
189
uucp
to
another
* Refers file uuname and uulog
uucp [options] source-files destination files
options
190
-d make necessary
directory
-c do not copy local file to spool directory
-m send mail to the requester when the
copy is completed
uuto
*
*
*
Allows remote login over the network
*
Similar to telnet, but a much more flexible
facility.
$ rlogin system_name!username
TCS Confidential
1
191
ftp
*
*
$ ftp
ftp > open sys1
Name login:
passwd
192
ftp > ?
$
TCS Confidential
1
192
rcp
facility
uux
remote
redirect the
stdout to a file on a specified computer
$ uux [options] commandstring
All shell special characters must be quoted
ct
*
*
modem
This command dials the 194
phone number of the modem
$ ct [options] telno
$ ct -h -w5 -s1200 9=5553497
1200
is not
local
1
194
cu
*
*
simultaneously
Examples
cu -s2400 9=5557867
Connected
195
login :
~%take proposal
computer to local
~%put minutes minutes.bak
file minutes on the
minutes.bak
~%!comma
~%$command
output
~%.
TCS Confidential
1
195
Chapter 7
196
System calls
TCS Confidential
1
196
System Calls :
1.
Introduction
2.
3.
TCS Confidential
1
197
*
Difference lies in their incorporation in the UNIX
System
*
*
be
TCS Confidential
1
198
opening files
reading files
writing in files
creating files
199
changing the permission of files
changing the owner and group of files
seeking to file-location
closing files
TCS Confidential
1
199
open ()
Example :
Opening the file test in read mode and checking for the error condition
fd = open(/usr/trg/test,0);
if ( fd == -1 )
{
printf(error in opening file test);
exit(1);
}
TCS Confidential
2
200
read ()
*
*
201
filedesc
buffer
read
nbytes
read() returns the number of characters read and 0 in case ofend of file (EOF)
and returns -1 on error
TCS Confidential
2
201
Example :
Reading the file test 100 characters at a time
while ( ( n = read(fd,buff,100)) > 0 )
{
printf(file test has contents %s ,buff);
}if ( n == 0 )
printf ( End of file );
if ( n == -1 )
printf (Error in reading202
file test);
When each read is finished the pointer advances by 100 bytes so
that the next read picks from there
*
TCS Confidential
2
202
write ()
*
203
filedesc -
buffer
file
nbytes
2
203
Example :
while ( ( n = read (fd,buff,100)) > 0 )
{
n1 = write (1,buff,100);
/* writing to standard output */
/* file id - 1 is for stdout */
if ( n1 == -1 )
printf (Error in writing on stdout");
}
204
if ( n == -1 )
printf (Error in reading file test ");
TCS Confidential
2
204
creat ()
*
205
filename -
mode
2
205
creat ()
Contd...
Example :
umask(0000);
fd = creat(newfile,0666);
if ( fd == -1 )
{
printf(error in creating
206file newfile );
}
Creates a file called newfile in mode 0666 i.e., read and write
permissions for owner, group, and others
Note : while creating a new file ensure umask is set to zero
Otherwise,
If umask had been 0022 in the environment variable, then the
effective permission would be
mode & ~0022
TCS Confidential
2
206
chmod ()
*
207
filename -
mode
2
207
Example :
ret = chmod(test.c,0600);
if ( ret == -1 )
{
printf(error in changing the file permission);
}
208
TCS Confidential
2
208
chown ()
*
filename
the
owner
group
group id
chown() returns 0 on success and returns -1 on error
TCS Confidential
2
209
chown ()
Contd.
Example :
ret = chown(test.c,0,1);
if ( ret == -1 )
{
210
TCS Confidential
2
210
lseek ()
* lseek() changes the position of read-write pointer for the file descriptor
int lseek(filedes, offset, origin);
int filedes, origin;
long offset;
211 on success
lseek() returns new-value of the pointer
returns -1 on error
The value of the pointer depends on origin :
0 set the pointer to offset bytes from the begining of the file
1 increment the current value of the pointer by offset
2 set the pointer to the size of the file plus offset bytes
TCS Confidential
2
211
lseek () Contd...
Example :
ret = lseek(fd,1000,0);
if ( ret == -1 )
{
212
printf(error in seeking to the 1000th byte of the file );
}
lseek(fd,1000,0) skips the first 1000 bytes of the file and starts
reading from the 1001th byte
TCS Confidential
2
212
fork ()
* fork() creates a new process which is a child process
* Child process is a logical copy of the parent process
* Parents return value is the process id of the child
* Childs return value is 0
213
TCS Confidential
2
213
TCS Confidential
2
214
EXAMPLE
fork () , getpid () , getppid (), getpgrp ()
#include <stdio.h>
main()
{
int id ;
int pid , pgrp ;
215
int ppid ;
id = fork();
printf (PPID - %d PID- %d id - %d \n, getppid() , getpid() ,id );
printf (PGRP - %d \n , getpgrp() );
}
TCS Confidential
2
215
EXAMPLE
fork () , getpid () , getppid (), getpgrp ()
Output
PPID - 371 PID - 372 id - 0 - From child Process
PGRP - 136
PPID - 136 PID - 371 id - 372 - From Parent Process
PGRP - 136
216
PID - Process-ID
PPID - Parent Process - ID
ID - Returned value from fork()
PGRP - Process-group-ID
If PID is equal to the process PGRP then the process is the group leader
TCS Confidential
2
216
wait ()
&
execl ()
* wait() causes a parent to stop running and await the termination of a child process
* execl() overlays the original process with a new set of instructions
Example on execl()
217
#include <stdio.h>
main()
{
int id;
printf ( Parent process \n);
if ( ( id = fork() ) == 0 )
{
printf(Statement from child process\n);
execl(/bin/date,date,0);
}
printf( Parent process again \n);
}
TCS Confidential
2
217
EXAMPLE ON execl()
Output
Parent Process
Statement from child process
Parent process again
Tue Sep 10 11:34:17 1991
Process forked two processes and parent process avoided execl() to print the
final statement i.e parent process did not wait for the child to finish
To make the parent wait for the child to finish - wait() can be used
The example on execl() gets modified
218 as
#include <stdio.h>
main()
{
int id;
printf ( Parent process \n);
if ( ( id = fork() ) == 0 )
{
printf(Statement from child process\n);
execl(/bin/date,date,0);
}
wait();
printf( Parent process again \n);
}
TCS Confidential
2
218
Parent Process
Statement from child process
219
TCS Confidential
2
219
220
TCS Confidential
2
220