Академический Документы
Профессиональный Документы
Культура Документы
Index
Topics UNIX Operating System Unix Commands Files & Directories Unix Utilities Process Shell Programming Reference Lab
3
Chapter 1
UNIX Operating System
Objectives
In this session, you learn about: The functions of OS The history of Unix The features of UNIX The Unix architecture Process management CPU scheduling Memory management File management
programs
OS acts as an interface between the user and the
Operating System
Operating system interacts with user in two ways Operating system commands Enables user to interact directly with the operating system.
Operating system calls
Provides an interface to a running program and the operating system. System calls in UNIX are written in C.
History of UNIX
Ken Thompson of AT&T Bell Laboratories designed
(Portable Operating System Interface) and X/open standard. In 1988, MIT formed Xconsortium developed vendor-neutral Xwindow System.
What is Linux?
An open-source UNIX like operating system Initially created by Linus Torvalds for PC architecture
10
Features of UNIX
Multi-user, multitasking, timesharing Portability Modularity
File structure
Security Strong networking support & advanced graphics
11
Layered Architecture
banner ls
kernel hardware
sort
12
layer
Fourth layer is the one with which the user actually interacts.
13
Kernel
Kernel is that part of the OS which directly makes interface with the
hardware system.
A program that starts running when the system is booted and
Actions: Provides mechanism for creating and deleting processes Provides processor scheduling, memory, and I/O management Provides inter-process communication. File management and interrupt and error handling.
14
The Shell
A utility program that comes with the UNIX system. Features of Shell are: Interactive Processing Background Processing I/O Redirection Pipes Shell Scripts Shell Variables Programming Constructs
15
Process Management
A process is a program in execution Several processes can be executed simultaneously in
a UNIX system.
A process is generally created using the
the parent process, and the newly created process is called the child process.
16
CPU Scheduling
Unix uses round-robin scheduling to support its multi-user and time-sharing feature. Round-robin fashion of scheduling is considered to be the oldest, simplest and widely used algorithm. Every process is given a time slice (10-100 millisec.)
17
Memory Management
Virtual memory : The entire memory space available to the Unix Kernel. This consists of both the physical memory(RAM) and all swap space.
memory of a system. Swap space is a how a system with 64MB Ram can run a 100MB program
Paging :The action of transferring one Page of physical memory from, or to, the swap space.
18
File Management
UNIX uses a hierarchical file system with / as
its root.
Every non-leaf node of the tree is called as a directory file. Every leaf node can either be a file, or an empty directory
19
File System
dev
bin sh
tmp home
etc
var spool
lib
usr
console lp0
ls user1
bin
src
20
10
File System
File system is the structure in which files are stored
on disk
File in UNIX is sequence of bytes organized in the form
of blocks
The size of each block is 512 bytes (depends on
architecture)
Block size can be decided while creating the file system
structure
21
Address of the addr block Address of the addr block Address of the addr block
22
11
login:
Once the user enters login ID, then the system prompts for password (if login expects password) as given below:
password:
Once the password is entered, the system validates the user id and password combination to identify if it is one of the valid user IDs on the system. If so the system displays a prompt. In case of Bourne shell or Bash shell it is $ sign for ordinary user and # for root login.
24
12
Summary
In this session, you learned about The functions of OS The History of Unix The features of UNIX The Unix Architecture Process management CPU Scheduling Memory management File management
25
Chapter 2
UNIX Commands
26
13
Objectives
In this session, you will learn to: Use the basic Unix commands
27
Simple Commands
pwd Displays the current working directory. date Displays the current date and time
28
14
Simple Commands
who Displays the names of all the users who have currently logged in who am i Displays the name of the current user.
29
:ls [options] [file.] -l list in long format -a list all files including those beginning with a dot -i list inode no of file in first column -s reports disk blocks occupied by file -R recursively list all sub directories -F mark type of each file -C display files in columns
30
15
Meta Characters
Meta Characters * ? [] Purpose Example $ ls l *.c file* $ ls l file? $ ls l file[abc] $ cat file1; cat file2 $ cat abc | wc $ (echo ==== x.c ====; cat x.c) > out count=`expr $count + 1` assuming count has value3, this increments the value of count echo expr $count + 1 displays expr $count + 1 echo expr $count + 1 displays expr 3 + 1 assuming the variable count has value 3
Match with one or more characters or none Match with any single character Match with any single character within the brackets ; Command separator | Pipe two commands () Group commands Useful when the output of thecommand group has to be redirected `command` Execute the command enclosed within back quotes. Useful when the output of a command into a variable in a shell script string string Quote all characters with no substitution (ex. no special meaning for $ ) Quote all cha racters with substitution. The characters $,\ (back slash) and back quote have special meaning.
31
1 2 1 1 1
15 22 12 15 20
File name
32
16
on-line to explain the usage of the Unix system and commands. Syntax: man [options] command_name
Common Options -k keyword list command synopsis line for all keyword matches -M path path to man pages -a show all matching man pages (SVR4)
info command_name - help for commands help -command_name gives command synatx
33
Summary
In this session, you have learned to use the basic Unix commands like
34
17
Chapter 3
Files & Directories
35
Objectives
In this session, you will learn to: set file permissions using the chmod command use directory-related commands namely mkdir, rmdir, cd commands use file-related commands namely cp, mv, rm commands access advanced file permissions using commands umask, suid, sgid, linking files, stickybit create and edit files using the vi editor
36
18
37
38
19
display the contents of a directory file from within the directory change to the directory display a file in the directory copy a file to, or from the directory
39
chmod u+x file_name Syntax: chmod <category> <operation> <permission> <filename(s)> or chmod <octal number> filename Octal Number
4 - for read 2 - for write 1 - for execution $ chmod 744 xyz this sets read, write and execute permissions for owner, read permission for group and others
40
20
Directory Creation
Command Syntax mkdir [OPTION] DIRECTORY $ mkdir <path>/<directory> $ mkdir m <directory> $ mkdir p <directory1>/<directory2>/<directory3> Example:
$ mkdir project1
41
Directory Removal
rmdir command removes directory Syntax
rmdir <directory name>
rmdir project1
Remove multiple directories rmdir pos1 pos2 Remove the directory recursively rmdir p dir1/dir2/dir3 rmdir removes a directory if it is empty and is not the current directory
42
21
Command - cd
cd command is used to change the directory
cd cd .. cd /
- take to the home directory - takes to the parent directory - takes to the root directory
43
File-Related Commands
File Operation Copying a file Moving a file Removing a file Displaying a file and concatenating files Command cp mv rm cat
44
22
Command - cp
Used to copy files across directories Syntax cp <source file> <new file name> Example cp file1 file2
45
Command - cp
Options to cp -p
Copies the file and preserves the following attributes owner id group id permissions last modification time
-r recursive copy; copy subdirectories under the directory if any -i interactive; prompts for confirmation before overwriting the target file, if it already exists
46
23
Command - mv
Used to move a file, or rename a file Preserves the following details
-f
suppresses all prompting (forces overwriting of target) prompts before overwriting destination file
47
-i
Command - rm
Used to remove a file
Syntax : rm file(s)
-f -i -r
suppresses all prompting prompts before deleting destination file will recursively remove the file from a directory (can be used to delete a directory along with the content )
48
24
49
Command - umask
umask value is used to set the default permission of a file and directory while creating umask command is used to see the default mask for the file permission Default umask value will be set in the system environment file like /etc/profile umask 022 will set a mask of 022 for the current session
The file permission after setting this umask value will be
50
25
Command - ln
Linking files
Hard Link (in the same filesystem) $ ln /usr/bin/clear /usr/bin/cls
Hard link uses the same inode number
Soft Link (in different filesystems also used to link directories) $ ln s /usr/bin/clear /home/user1/cls
51
The leftmost octal number 4 indicates set user ID bit to be set, other octal digits indicate regular file permissions. This is meaningful for executable files only.
52
26
cont..
Set group id (SGID) Just like SUID, setting the SGID bit for a file sets your group ID to the file's group while the file is executing
set group ID bit can be set in one of the two ways:
The leftmost octal number 2 indicates set group ID bit to be set, other octal digits indicate regular file permissions. This is meaningful for executable files only.
53
cont..
Sticky bit (SVTX) Typically set to a directory that is shareable Any user can create a file in such sharable directory Only owner of the file or super user (root) can remove a file from the directory
sticky bit can be set in one of the two ways:
The leftmost octal number 1 indicates sticky bit to be set, other octal digits indicate regular file permissions.
54
27
Vi Editor
vi is a visual editor used to create and edit text files. A screen-oriented text editor Included with most UNIX system distributions Command driven Categories of commands include Cursor movement Editing commands Search and replace commands The vi editor is invoked by the following command:
$ vi filename
55
Navigation using vi
B a c k s p a c e h j k S p a c e l
t h e q u i c k b r o w n f o x w ww t h e q u i c k b r o w n f o x w 2 t h e q u i c k b r o w n f o x b b b
t h e q u i c k b r o w n f o x $ t h e q u i c k b r o w n f o x ^
56
28
Editing Commands
Text insertion / replacement
I - inserts text to the left of the cursor a - inserts text to the right of the cursor I - inserts text at the beginning of the line A
- appends text at end of the line o - opens line below O - opens line above R - replaces text from cursor to right s - replaces a single character with any number of characters S - replaces entire line
57
Editing Commands
Deletion
x 3x
dw
2dw dd 2dd
- to delete word
- to delete 2 word - to delete a line - to delete 2 lines
58
29
Editing Commands
Yanking (copy) Y - copy line into buffer 3Y - copy 3 lines into buffer p - copy buffer below cursor P - copy buffer above cursor Save and quit :w - to save :w! - to name a file (:w! filename -> save as) :x - save and quit :q - cancel changes :q! - cancel and quit
59
/
:%s/old/new/g
:#,#s/old/new/g numbers of
60
30
--- Eg. df h
du ---- du -b subdir
Estimate disk usage of directory in Bytes
61
Summary
In this session, you have learned how to use file permissions using the chmod command use directory-related commands namely mkdir, rmdir, cd commands use file-related commands namely cp, mv, rm commands access advanced file permissions using commands umask, suid, sgid, linking the files, stickybit create and edit files using the vi editor
62
31
Chapter 4
UNIX Utilities
63
Objectives
In this session, you will learn how to:
use the Unix utilities such as cat, echo, touch, more, file, wc, cmp, comm, find employ redirection operators use filters such as sort, grep, cut, head, tail, tr, and paste use communication commands telnet, ftp use backup commands zip/gzip and tar
64
32
cat
cat command takes the input from the keyboard, and sends the output to the monitor We can redirect the input and output using the redirection operators
$ cat > file1 Type the content here press <ctrl d> $ cat file1 Displays the content of the file $cat >> file1 This will append standard input to the content of file1
65
touch
touch is used to change the time stamp of the file
-a to change the access time -m to change the modification time -c no create if not exists
touch <file> will change the time of change of the file if the file exists
If the file does not exist, it will create a file of zero byte size.
66
33
read command allows to read input from user and assign it to the variable specified. read x
67
time.
file
Used to display the type of the file tty Prints the terminals name
68
34
characters in a disk file or from the standard input. -l - displays the number of lines -w - displays the number of words -c - displays the number of characters
69
find
Lets user to search set of files and directories based on various criteria Syntax: find [path...] [expression] [path] where to search [expression] What type of file to search (specified with type option) What action to be applied (exec, print, etc.) Name of the files (specified as part of name option, enclosed in
Example
70
35
find
Finding files on the basis of file size
size [+ ]n[bc]
n represents size in bytes (c) or blocks (b) of 512 bytes find . size 1000c find . size +1000c find . size 1000c lists all files that are exactly 1000 bytes in size lists all files that are more than 1000 bytes in size lists all files that are less than 1000 bytes in size
71
find
Finding files on the basis of access time (atime) or modified time (mtime)
atime [+-]n mtime [+-]n
n represents number of days ( actually 24 * n hours) find . atime 2 find . atime +2 find / mtime 2 lists files accessed exactly 2 days ago lists files accessed more than 2 days ago lists files modified less than 2 days ago
72
36
find
Applying a command on files matching the criteria with
73
Standard Files
Standard Input file Keyboard, file descriptor is 0 Standard Output file Monitor, file descriptor is 1 Standard Error file Monitor, file descriptor is 2
74
37
I/O Redirection
< file > file 2> file 2>&1 output $ cat > abc $ ls l > outfile $ cat xyz abc > outfile 2> errfile $ cat xyz abc > outfile 2>&1 redirect standard input from file redirect standard output to file redirect standard error to file merge standard error with standard
75
Filters
Filters are programs that takes its input from the standard input file, process it, and sends it to the standard output file. Commonly used filter commands sort grep cut head tail paste
76
38
sort
Sorts the contents of the given file based on the first char of each line. -n -r
numeric sort (comparison made according to strings numeric value)
reverse sort
-t
+num +num [-num]
77
grep
grep -Global Regular Expression Printer is used for searching regular expressions Syntax grep <options> <pattern> <filename(s)>
78
39
grep options
-c -n -v -i displays count of the number of occurrences displays line numbers along with the lines displays all lines except lines matching pattern Ignores case for matching
79
Patterns
* - matches 0 or more characters [^pqr] - Matches a single character which is not p ,q or r ^pqr -Matches pqr at the beginning of the line pqr$ -Matches pqr at the end of the line . - Matches any one character \ - ignores the special meaning. grep New\[abc\] filename
80
40
81
82
41
Filter command - tr
tr - translate filter used to translate a given set of characters Example :
tr [a-z] [A-Z] < filename
This converts standard input read from lower case to upper case. option -s can be used to squeeze the repeated characters.
83
Filter command - tr
Useful options for tr
-s char Squeeze multiple contiguous occurrences of the character into single char -d char Remove the character
84
42
Command Piping
Allows the output (only the standard output) of a command to be sent as input to another command. Multiple pipes may appear in one command line.
85
debugging purpose.
86
43
87
ftp
Ftp is a file transfer program Provides necessary user interface to the standard File Transfer Protocol Allows users to transfer files to and from a remote host
88
44
ftp - commands
receive file from host receive multiple files from host send file to host send multiple files from host
89
ftp - commands
ls cd lcd
list directory of host change directory on the host change directory on the local machine
To set transfer format ascii set to ascii mode binary set to binary mode
90
45
ftp - commands
Progress indication of transfer hash command Quitting ftp session bye command
91
92
46
93
Compression Utilities
gzip, Usage is very similar to compress and pack utilities in Unix: gzip [-vc] filename
where -v displays the compression ratio. -c sends the compressed output to standard output and leaves the original file intact.
gunzip gunzip can uncompress files originally compressed with compress.
94
47
Summary
In this session, you have learned to: use the Unix Utilities like
employ redirection operators use filters like sort, grep, cut, head, tail, tr, ftp backup commands tar and zip/gzip
95
Chapter 5
Process
96
48
Objectives
In this session, you will learn to:
like
97
Processes
Process - a program in execution When program is executed, a new process is created
98
49
Login shell
As soon as the user logs in, a process is created which executes the login shell. Login shell is set for each login in /etc/passwd file.
99
ps
The ps command is used to display the characteristics of a process It fetches the pid, tty, time, and the command which has started the
process.
-f -u -a -e
lists the pid of the parent process also. lists the processes of a given user lists the processes of all the users lists all the processes including the system processes
100
50
Background Process
Enables the user to do more than one task at a time. If the command terminates with an ampersand (&), UNIX executes
process
101
List the background process fg % <job id> Runs a process in the foreground bg %<job id> Runs a process in the background
102
51
103
Process priority
nice
104
52
Background Process
nohup
Lets processes to continue to run even after logout The output of the command is sent to nohup.out if not
redirected
105
Background Process
wait command
can be used when a process has to wait for the output
of a background process
The wait command, can be used to let the shell wait
process as well.
106
53
Summary
In this session, you learned to: Define a process Use process-related commands like
like
107
Chapter 6
UNIX Shell Programming
108
54
Objectives
In this session, you will learn to:
Use Shell variables Write scripts to process positional parameters Use test command Use if construct Use for loop Use while loop Use case construct Define and use functions Debug shell scripts
109
sh csh
Korn shell
Bourne again shell
ksh
bash
110
55
Command processing
Displays the shell prompt and reads the command typed by the user. Interprets the command and classifies it as an internal (built-in), or an external command. If it is NOT a built-in command, searches for the command in the PATH-specified directories, and executes that command if it is found.
111
Shell Features
Parent shell process
fork
$ vi test.c
112
56
113
History
some UNIX shells support command history facility to keeps track of commands that were executed facility to rerun previously executed commands bash shell supports the following !! !num recall the last command and execute it. execute nth command where n is the the num specified after !
114
57
alias
alias can be used to give new name to an existing command A better name that represents a single command or a sequence of commands to be executed, often with appropriate options alias is an internal command alias newname=command $ alias l=ls l The unalias command cancels previously defined alias.
115
Shell Programming
Allows Defining and referencing variables Logic control structures such as if, for, while, case Input and output
116
58
Shell Variables
A variable is a name associated with a data value, and it offers a symbolic way to represent and manipulate data variables in the shell. They are classified as follows user-defined variables environment variables predefined variables value assigned to the variable can then be referred to by preceding the variable name with a $ sign.
117
Shell Variables
The shell provides the facility to define normal, and environment variables. A normal variable can be only used in the shell where it is defined. An environment variable can be used in the shell where it is defined, plus any child shells invoked from that shell.
118
59
Examples:
x=10 textline_1=This line was entered by $USER textline_2=This line was entered by $USER allusers=`who` usercount=`who | wc l`
119
120
60
To define an environment variable, use following syntax: variable_name=value export variable_name Examples:
121
SHELL
TERM
122
61
123
If the shell script is assigned execute permission, it can be executed using its name. For example:
./script1.sh
124
62
125
Built-in variables
Following are built-in variables supported
$0, $1$9 $* $@ $?
- positional arguments - all arguments - all arguments - exit status of previous command
executed
$$ $!
126
63
----------------------script2.sh-------------------------echo Total parameters entered: $# echo First parameter is : $1 echo The parameters are: $* shift echo First parameter is : $1 ----------------------------------------------------------- Execute the above script using the script2.sh these are the
parameters command.
127
128
64
129
The expression can be formed using a combination of shell variables and the operators supported by the test command. These operators provide facility to compare numbers, string and logical values, file types and file access modes.
130
65
131
132
66
=) string1 != string2 (not equal to) string1 (string is not NULL) -n string1 (string is not NULL and exists) -z string1 (string is NULL and exists)
133
134
67
135
136
68
Combining Conditions
It is possible to combine conditions by using following operators:
-a (logical AND operator) -o (logical OR operator)
137
Combining Conditions
The syntax for this is:
test expression_1 a expression _2, OR [ expression _1 a expression _2 ] test expression_1 o expression _2, OR [ expression_1 o expression_2 ] test ! expression _1 OR [ ! expression _1 ]
138
69
139
Example
# to check if the current directory is the same as your home directory curdir=`pwd` if test $curdir != $HOME then echo your home dir is not the same as your pesent working directory else echo $HOME is your current directory fi
140
70
141
Example:
The syntax of this loop is:
142
71
Example ----------------------script.sh-------------------------#! /bin/sh usernames=`who | cut d f1` # for user in ${usernames} do echo $user done ------------------------------------------------------------
143
144
72
Example
Shell script checks for a blank/non blank string eg: read nam while [ -z $nam ] do read nam done echo the string is $nam the above piece of code keeps accepting string variable nam until it is non zero in length.
145
Example
Shell script to compute factorial of a given number #!/bin/bash n=$1 if [ $n -eq 0 ]; then fact=0 else fact=1 while [ $n ne 0 ] do fact=`expr $fact \* $n` n=`expr $n 1` done fi echo $fact
146
73
147
Example
#!/bin/bash echo enter 2 nos read num1 read num2
(contd.) case $choice in 1) res=`expr $num1 + $num2` echo result is $res;;
echo enter 1 (for addition) 1) res=`expr $num1 - $num2` or 2 (for subtraction) echo result is $res;; read choice
*) echo invalid input;; esac
148
74
Example
#!/bin/bash read number case $number 1) echo 1st break;; 2) echo 2nd break;; 3) echo 3rd break;; *) echo ${number}th break;; esac
149
Summary
In this session, you have learned to:
Use Shell variables Write scripts to process positional parameters Use test command
Use if construct
Use for loop Use while loop Use case construct
150
75
UNIX Bibliography
UNIX in a Nutshell for BSD 4.3: A Desktop Quick Reference For Berkeley (O'Reilly & Associates, Inc., 1990, ISBN 0-937175-20-X). UNIX in a Nutshell: A Desktop Quick Reference for System V & Solaris 2.0 (O'Reilly & Associates, Inc., 1992, ISBN 0-56592-001-5). The UNIX Programming Environment, Brian W. Kernighan & Rob Pike (Prentice Hall, 1984).
151
Thank You
152
76