Professional Documents
Culture Documents
Copyright Notice
Copyright Information Technology Consultants reserves all rights to this document. No part of this literature may be stored in a retrieval system, transmitted, or reproduced in any way, including but not limited to photocopy, photograph, magnetic or other record, without the prior agreement and written permission of Information Technology Consultants
09-Sep-2000 Version: 2.1 2
Schedule Day 1
Overview of Unix Learning the basic commands Customizing the shell environment Job Control Understanding file permissions
09-Sep-2000
Version: 2.1
Schedule Day 2
Shell programming (Korn shell) System Programming
File Systems File Handling System Calls Process Management Process Management System Calls
Unix Architecture
Process Management Data structures and internals File System Data structures and internals
09-Sep-2000 Version: 2.1 4
Schedule Day 3
System Programming
Overview of IPC facilities Using pipes Using named pipes Using shared memory Using message queues
Unix Overview
Developed at AT&T Bell Lab in 1969 by Ken Thompson on an idle PDP-7. Later moved to a PDP-11. Ken Thompson also developed a new language called B. Denis Ritchie developed C language from B. In 1973, Unix was re-written in C language. This led to its popularity, the number of installations at Bell Lab grew to about 25.
09-Sep-2000 Version: 2.1 6
Unix Overview
As AT&T was not allowed to sell software at that time, it was provided free-of-cost to universities. By 1977, Unix installations grew to about 500, of which 125 were in universities. Licenses of Unix were provided to commercial and educational institutions. Between 1977 and 1982, Bell Labs combined several Unix variants into a single system known as Unix System III.
09-Sep-2000 Version: 2.1 7
Unix Overview
In 1983, Bell Lab announced their support for a new version, known as System V. By this time, University of California at Berkeley had also developed a new variant of Unix, known as 4.3 BSD for VAX machines. The 4.3 BSD provided new features such as reliable signals and TCP/IP.
09-Sep-2000
Version: 2.1
Unix Overview
Neither AT&T nor BSD is now active in Unix development so most new developments are now defined by a specific vendor or a body such as IEEE (POSIX standard), FreeBSD organization or the Linux community. Today, Linux is adding to Unixs popularity. However, Linux is a Unix-clone and not Unix as the kernel was written from scratch and other tools have been largely taken from the FSF (Free Software Foundation).
09-Sep-2000 Version: 2.1 9
Unix Overview
Chronological evolution of Unix:
Year 1969 1973 1976 1978 1982 1983 1984 1987 1988 1992 From Bell Lab /AT&T First Edition Fifth Edition Sixth Edition Seventh Edition System III System V System V, Release 2 System V, Release 3 System V, Release 4 From BSD
3 BSD 4.1/4.1a BSD 4.2 BSD 4.3 BSD 4.3 BSD Tahoe 4.4 BSD
09-Sep-2000
Version: 2.1
10
Unix Overview
Various Unix Flavors
Product AIX HP Ux Solaris Tru64Unix IRIX SCO Unix From IBM HP Sun Compaq SGI SCO Version
09-Sep-2000
Version: 2.1
11
Unix Overview
Multi-user and multi-tasking environment Provides virtual memory support Devices are treated as files Provides more than one type of shells (command interpreters)
09-Sep-2000
Version: 2.1
12
Unix Overview
Provides more than one mechanism for interprocess communication (IPC) . Excellent networking support through TCP/IP. The standard Unix File system is fast, but not very robust many vendors have introduced their own robust versions.
09-Sep-2000
Version: 2.1
13
Unix Overview
Most system operations depend upon the configuration files such as /etc/passwd, /etc/group, /etc/hosts, /etc/services etc. Default system security can be enhanced through installation of additional security packs. Supports the X-Window system as a GUI. Supports POSIX standards to a large extent.
09-Sep-2000
Version: 2.1
14
Unix Architecture
Utility, command or shell script shell
User Mode Kernel Mode
system calls
File System
Kernel
I/O System
Memory Manager
Scheduler
Buffer Cache
09-Sep-2000
Version: 2.1
15
Function of a shell
Displays a prompt where the user can enter a new command. Validates the command entered by the user. Creates a new process using fork() or similar system call. Executes the command using one of exec() family of system calls.
09-Sep-2000 Version: 2.1 16
Function of a shell
Waits for a.out to complete using the wait() or similar system call, provided the command is not being run as a background job.
Note: Not all shells provide background job facility.
Displays the prompt again, once the command is complete, or immediately displays the prompt if it is a background job.
09-Sep-2000
Version: 2.1
17
Function of a shell
A shell also provides a programming environment through a combination of :
standard Unix utilities and commands built-in shell commands
The programs written to work in environment of a shell are called shell scripts. A shell script is a text file containing above commands.
09-Sep-2000
Version: 2.1
18
Next Topic
09-Sep-2000
Version: 2.1
20
09-Sep-2000
09-Sep-2000
Version: 2.1
22
09-Sep-2000
Version: 2.1
25
Getting Help
Many basic commands are covered in this course. If you want to learn more about these and other commands, see the man pages. When hard copy documents are not available, you can access online documentation by using the following commands:
The man command displays online reference pages. The apropos command displays a one line summary of each command pertaining to a specified subject.
09-Sep-2000 Version: 2.1 27
Getting Help
The apropos command and man -k command are useful tools if you forget a command name. These commands require access to the whatis database. This database must be installed. Both these commands let you enter a command description in the form of keywords. The commands then list all the reference pages that contain any of the keywords.
09-Sep-2000
Version: 2.1
28
Getting Help
Assume that you cannot remember the name of the command that displays who is logged in to the system. To display the names and descriptions of all man pages that are related to users who are logged in, enter one of the following: $ apropos "logged in" or $ man -k 'logged in'
09-Sep-2000 Version: 2.1 29
09-Sep-2000
Version: 2.1
31
File names
A file name can contain any character except the following because these have special meaning to the shell:
Slash (/) Backslash (\) Ampersand (&) Left- and right-angle brackets (< and >) Question mark (?) Dollar sign ($) Left bracket ([) Asterisk (*) Tilde (~) Vertical bar or pipe symbol (|)
09-Sep-2000
Version: 2.1
32
File names
You may use a period or dot (.) in the middle of a file name. If you use a dot at the beginning of the file name it will be hidden when doing a simple listing of files thru the ls command. The maximum length of a file name depends upon the file system used. Most new file system allow a maximum length of 255 characters (the default). Older file systems allow a maximum file name length of only 14 characters.
09-Sep-2000 Version: 2.1 33
Understanding pathnames
A pathname specifies the location of a directory or a file within the file system. A pathname consists of a series of directory names separated by slashes (/) that ends with a directory name or a file name. A pathname that starts with a slash (/) is called a full pathname or an absolute pathname.
09-Sep-2000 Version: 2.1 35
Understanding pathnames
You can also think of a full pathname as the complete name of a file or a directory. Regardless of where you are working in the file system, you can always find a file or a directory by specifying its full pathname. The file system also lets you use relative pathnames. Relative pathnames do not begin with the / that represents the root directory because they are relative to the current directory.
09-Sep-2000 Version: 2.1 36
Understanding pathnames
You can specify a relative pathname in one of several ways:
As the name of a file in the current directory. As a pathname that begins with the name of a directory one level below your current directory. As a pathname that begins with .. (dot dot, the relative pathname for the parent directory). As a pathname that begins with . (dot, which refers to the current directory). This relative pathname notation is useful when you want to run your own version of an operating system command in the current directory (for example ./ls).
09-Sep-2000
Version: 2.1
37
Understanding directories
In Unix, a directory is a file that has a number of entries. Each entry contains:
A filename The I-node number of the file
09-Sep-2000
Version: 2.1
38
Understanding directories
In the C shell and the Korn or POSIX shell, you may also use a tilde (~) at the beginning of relative pathnames. The tilde character used alone specifies your home directory. The tilde character followed by a user name specifies the home directory of that user. For example, $ cd ~/mydir $ cd ~micky/mouse
09-Sep-2000 Version: 2.1 39
Examples: $ ls $ ls l $ ls -Rl
09-Sep-2000
[4]
[5]
[6]
[7]
101 Jun 5 10:03 file1 75 Jun 5 10:03 file2 75 Jun 5 10:03 file2 65 Jun 5 10:06 file3 32 Jun 5 10:07 project
1. 2. 3. 4. 5. 6. 7.
Number of 512-byte blocks used by files in this directory. Number hard of links to each file. User name of the file's owner. Group to which the file belongs. Number of bytes in the file. Date and time the file was created or last modified. Name of the file or directory.
Version: 2.1 41
09-Sep-2000
The drwxr-xr-x indicates file type and permissions for each file. More about these permissions later
09-Sep-2000
Version: 2.1
42
09-Sep-2000
Version: 2.1
43
(continued)
09-Sep-2000 Version: 2.1 44
This command is especially useful when you suspect that a file contains a compiled program, audio data, or image data. Displaying the contents of these types of files can produce strange results on your screen.
09-Sep-2000 Version: 2.1 45
Creating/Deleting Directories
You can use the mkdir command to create directory or sub-directories.
Examples: $ mkdir c_progs $ mkdir c_progs/shared
You can use rmdir command to delete a directory. The directory must be empty. You can also use the rm command with r (recursive) option. Examples:
09-Sep-2000
Version: 2.1
46
Viewing files
You can use the cat command to view a file on screen. Examples:
$ $ cat test.c $ cat *.c
09-Sep-2000
Version: 2.1
47
Viewing files
Following commands can also be used to display a file :
pg more page
The pg command lets you view one or more files. When you display files that contain more lines than will fit on the screen, the pg command pauses as it displays each screen.
09-Sep-2000 Version: 2.1 48
Printing files
If your system has more than one printer, use following format to specify where you want the file to print: lpr -P printername filename The -P flag specifies a printer. The printername is the name of a printer. Printers often have names that describe the location of the printer, the custodian or nearest monitor, or some other descriptive name. Contact system administrator for info. on printer names.
09-Sep-2000 Version: 2.1 49
letter 5444 bytes (standard input) 969 bytes travel 1492 bytes
09-Sep-2000
09-Sep-2000
Version: 2.1
52
Renaming/Deleting Files
You can use the mv command to rename or move a file.
Examples: $ $ mv test.c newtest.c $ mv test.c c_progs $ mv test.c c_progs/newtest.c
09-Sep-2000
Version: 2.1
53
I/O Redirection
For every program, Unix automatically opens three files. These files are:
Standard input (file descriptor 0) Standard output (file descriptor 1) Standard error (file descriptor 2)
Normally, the standard output and standard error file refer to the monitor whereas the standard input file refers to the keyboard.
09-Sep-2000
Version: 2.1
54
I/O Redirection
By using the I/O redirection facility, you can redirect output and error messages to a file. You can also take the input from a file. To redirect standard output, you can use the > symbol.
Examples: $ $ ls -Rl / > allfiles $ cat test.c > copytest.c
09-Sep-2000
Version: 2.1
55
I/O Redirection
To append standard output to an existing file, use >> symbol.
Examples: $ $ ls l *.c >> file_list
09-Sep-2000
Version: 2.1
56
I/O Redirection
To redirect standard input from an existing file, you can use the < symbol. In general, any command that expects input from the keyboard can take the redirected input from a file.
Examples: $ $ cat < file_list $ mail s this is a file user1 < my_message
09-Sep-2000
Version: 2.1
57
Piping
The piping facility allows you to send the standard output from one program as input to another program.
Examples: $ $ ls l | wc l $ who | wc l $ ls l test.c junk.c | wc l
(output of ls is sent to wc) (output of who is sent to wc) (only output of ls is sent to wc)
Note: The last example assumes that file junk.c does not exist.
09-Sep-2000
Version: 2.1
58
Matching filenames
If the file names have a common pattern, the shell can match that pattern, generate a list of those names, and automatically pass them to the command as arguments. The asterisk (*), sometimes referred to as a wildcard , matches any string of characters.
Examples: $ ls file *
09-Sep-2000
Version: 2.1
59
Matching filenames
If the file names have a common pattern, the shell can match that pattern, generate a list of those names, and automatically pass them to the command as arguments. The asterisk (*), sometimes referred to as a wildcard , matches any string of characters. For example, file* will match any filename starting with word file.
09-Sep-2000
Version: 2.1
60
Matching filenames
There is one exception to the general rules for pattern matching. When the first character of a file name is a period, you must match the period explicitly. For example, ls * displays the names of all files in the current directory except those that begin with a period. The command ls -a displays all file names, including those that begin with a period.
09-Sep-2000
Version: 2.1
61
Matching filenames
This restriction prevents the shell from automatically matching the relative directory names. These are . (for the current directory) and .. (for the parent directory). In addition to the asterisk (*), shells provide other ways to match character patterns. These are summarized in the next slide.
09-Sep-2000
Version: 2.1
62
Matching filenames
The filename pattern-matching characters are:
* ? [] [.-.] [!] matches any string, including NULL matches any single character matches any of the set characters matches any character within the specified range matches any character except those in the set
09-Sep-2000
Version: 2.1
63
When you create a hard link, you are providing another name for the same file. Hard links let you link only files in the same file system. All the link names are on equal footing. It is incorrect to think of one file name as the real name, and another as only a link.
09-Sep-2000 Version: 2.1 64
Creating Links
To link a hard link, use the following command format: ln <existing_pathname> <new_pathname> If you want to link files and directories across file systems, you can create symbolic links. To create a symbolic link, add an -s flag to the above ln command and specify full pathnames of both files.
09-Sep-2000 Version: 2.1 66
Understanding Links
Each file has a unique identification, called an Inode number. The I-node number refers to files data stored at a particular location - rather than to the file name. A directory entry is a link between an I-node number and a file name. This link enables you to link multiple file names to the same I-node number. To display I-node number of files, use ls with the -i flag.
09-Sep-2000 Version: 2.1 67
Understanding Links
Hard links to a file carry the same I-node number. Since an I-node number represents a file within a particular file system, hard links cannot exist between file systems. However, the soft link is a new file with its own, new I-node number. Since the soft link refers to the original file by name, rather than by I-node number, they work across file systems.
09-Sep-2000 Version: 2.1 68
Deleting Links
The rm (remove file) command does not always remove a file. Assume that a file has several hard links (and therefore more than one file name). In this case, the rm command removes the link between the file I-node number and that file name, but leaves the physical file intact. The rm command physically removes a file only after it has removed the last link between that file and its names.
09-Sep-2000 Version: 2.1 69
These files define who can use the system and each user's access rights.
09-Sep-2000 Version: 2.1 71
09-Sep-2000
Version: 2.1
72
09-Sep-2000
Version: 2.1
73
09-Sep-2000
Version: 2.1
75
09-Sep-2000
Version: 2.1
76
Above three permissions occur for each of the following three classes of users:
u (file owner) g (group members of file owner) o (all others; also known as world)
09-Sep-2000 Version: 2.1 77
09-Sep-2000
Version: 2.1
78
09-Sep-2000
Version: 2.1
79
The drwxr-xr-x indicates file type and permissions for each file. The first character indicates file type. The remaining characters are interpreted as three groups of three characters each that indicate what read (r), write (w), and execute (x) permissions are set for the owner, group, and others. If a hyphen (-) appears, the corresponding permission is not set.
09-Sep-2000
Version: 2.1
80
09-Sep-2000
Version: 2.1
81
09-Sep-2000
Version: 2.1
82
09-Sep-2000
Version: 2.1
84
09-Sep-2000
Version: 2.1
87
09-Sep-2000
Version: 2.1
88
09-Sep-2000
Version: 2.1
89
09-Sep-2000
09-Sep-2000
Version: 2.1
94
09-Sep-2000
Version: 2.1
96
09-Sep-2000
Version: 2.1
99
09-Sep-2000
Version: 2.1
100
09-Sep-2000
Version: 2.1
103
09-Sep-2000
Version: 2.1
104
Using Processes
A program is a set of instructions that a computer can interpret and run. While a program is running, it is called a process. Unix assigns a process identifier (PID) to every process. When a process begins executing, Unix opens three files for the process: stdin (standard input), stdout (standard output), and stderr (standard error).
09-Sep-2000 Version: 2.1 105
Using Processes
Unix allows you to run a number of different processes at the same time. These different processes can be from one or multiple users. Therefore, you do not have to enter commands one at a time at the shell prompt. Instead, you can run both foreground and background jobs simultaneously.
09-Sep-2000
Version: 2.1
106
Using Processes
Usually, when you enter a command on the command line, you wait for the results to display on your screen. Such commands are called foreground processes or jobs. However, some commands require long time. If a long-duration command runs as a foreground job, you cannot execute other commands until the current one finishes. As a result, you may want to run such command as a background job.
09-Sep-2000 Version: 2.1 107
09-Sep-2000
Version: 2.1
108
09-Sep-2000
Version: 2.1
109
New Topic
09-Sep-2000
Version: 2.1
110
Regular Expressions
A regular expression specifies a set of strings to be matched. It contains ordinary text characters and operator characters. Ordinary characters match the corresponding characters in the strings being compared. Operator characters specify repetitions, choices, and other features. Regular expressions fall into two groups:
Basic regular expressions Extended regular expressions
09-Sep-2000 Version: 2.1 111
09-Sep-2000
Version: 2.1
113
09-Sep-2000
Version: 2.1
116
09-Sep-2000
Version: 2.1
117
Introduction to Shell
Part I
Introduction to Shell
09-Sep-2000
Version: 2.1
118
Introduction to shell
The shell is a program that acts as an interface between Unix kernel and the user. Since the shell is just a program, there are many types of shells available in a typical Unix system. Some of these are:
Bourne Shell C-shell Korn-shell Bash etc.
Version: 2.1 119
09-Sep-2000
Introduction to shell
When a user logs into Unix, the Unix system automatically executes a shell for the user as specified in the /etc/passwd file. The shell displays a prompt on the terminal, and reads a command entered by the user. Once the user enters a command, the shell interprets the command and if the command is a valid command, executes the command itself of requests the kernel to execute the command.
09-Sep-2000 Version: 2.1 120
09-Sep-2000
Version: 2.1
121
Command Interpretation
The shell reads the command entered by the user. The shell checks whether the command is a built-in shell command or not. If the command is a built-in shell command, the shell executes the command.
09-Sep-2000
Version: 2.1
122
Command Interpretation
If the command is not a built-in shell command, the shell looks up the list of directories specified by the PATH environment variable, and finds a program (having the same name as the command entered or looking up at an alias table) that will perform that command. If the shell cannot find the command, it displays an error message. Otherwise it executes the command.
09-Sep-2000
Version: 2.1
123
Command Execution
If the command is a built-in shell command, the shell itself performs the command. If the command is not a built-in shell command, the shell passes on the command to the kernel by:
Creating a new (child shell) process Requesting the kernel to execute the command in the new (child shell) process
09-Sep-2000
Version: 2.1
124
Filename substitution
When the user enters a command string, the shell parses the string into following components:
Command(the first part of the string) Command arguments (the subsequent parts of the string)
For example, if you enter a command-string such as ls l *.c, this string contains the ls command and two arguments -l and *.c.
09-Sep-2000 Version: 2.1 125
Filename substitution
In arguments of a command, the shell recognizes certain characters such as *, ?, [, ], and . - as special characters and expands these characters into a filename list before executing the command. To see how this works, enter following commands:
echo this is a message echo this is a message echo *
09-Sep-2000 Version: 2.1 126
Filename substitution
To see how this works, enter following commands:
echo echo echo echo echo echo echo \.* \.???????????? s* s??????.sh s[12345].sh s[1-5].sh s??????.sh
09-Sep-2000
Version: 2.1
127
I/O Re-direction
The shell provides facilities to re-direct input, output and error for a command through special characters:
Use > character to redirect outout of a command to a file. Use < character to redirect input to a command from a file. Use >> characters to append outout of a command to a file. The > and 2>&1 characters (Bourne Shell only) redirect outout and error of a command to a file.
09-Sep-2000
Version: 2.1
128
I/O Redirection-Examples
cat *.c > outfile cat *.c >> outfile cat nosuchfile 2> outfile cat * nosuchfile > outfile 2>&1 mail $user s testing IO redirection < outfile cat * nosuchfile >> outfile 2>&1
09-Sep-2000
Version: 2.1
129
09-Sep-2000
Version: 2.1
132
Environment Control
The shell recognizes a number of environment variables, that allow you to customize the shell environment.
Note: Shell also supports normal variables.
These variables include: PATH, PS1, PS2 etc. However, some environment variables cannot be changed. These include: HOME, USER, PWD etc.
09-Sep-2000 Version: 2.1 133
Programming Language
The shell allows you to assign values to variables. This is the most basic feature required of a programming language. The shell also provides other programming constructs including:
Condition checking Loops Input and output statements
09-Sep-2000
Version: 2.1
134
09-Sep-2000
Version: 2.1
135
09-Sep-2000
Version: 2.1
136
09-Sep-2000
Version: 2.1
137
09-Sep-2000
Version: 2.1
138
09-Sep-2000
Version: 2.1
140
09-Sep-2000
Version: 2.1
141
09-Sep-2000
Version: 2.1
143
Using sub-shells
A sub-shell (or a child shell) is a new shell program that you start from the current shell. The sub-shell has its own set of environment variables and does not have access to the parent shells environment. You can run a command in a sub-shell by using special characters ( ) and { ; }
09-Sep-2000
Version: 2.1
144
Using sub-shells
Examples: (cd /var ; ls l ) OR {cd /var; ls l;} Before and after executing the above commands, use pwd command to see how using a sub-shell can be of help in some situations.
09-Sep-2000 Version: 2.1 145
Next Topic
09-Sep-2000
Version: 2.1
146
Since a shell script is a program, it is normally assigned execute permission using the chmod command.
09-Sep-2000 Version: 2.1 147
This course does not permit time to cover the above utilities. Where relevant, we might demonstrate the use of these utilities.
09-Sep-2000
Version: 2.1
148
sh script1.sh
If the shell script is assigned execute permission, it can be executed using its name. For example:
./script1.sh
09-Sep-2000 Version: 2.1 149
If the shell script has execute permission and is stored in a directory listed in PATH, it can be executed using the dot command. For example:
. script1.sh
09-Sep-2000 Version: 2.1 150
09-Sep-2000
Version: 2.1
151
Shell Variables
The shell provides 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. Therefore, if you define an environment variable, it can be used within a shell script.
09-Sep-2000 Version: 2.1 152
09-Sep-2000
Version: 2.1
154
OR OR
09-Sep-2000
Version: 2.1
155
Examples:
$ x=10; export x $ allusers=`who` ; export allusers
09-Sep-2000
Version: 2.1
156
09-Sep-2000
Version: 2.1
157
09-Sep-2000
Version: 2.1
158
Shell meta-characters
The Bourne Shell recognizes a number of metacharacters. Each meta character can represent zero or more ordinary characters. These meta characters are used in:
Filename expansion Variable assignment Forming regular expressions etc.
09-Sep-2000
Version: 2.1
159
Shell meta-characters
These meta-characters are:
Asterisk - * Question Mark - ? Square brackets [ and ] Single quote Double quote Back quote - ` Back slash - \
09-Sep-2000
Version: 2.1
160
Shell meta-characters
Examples:
$ x=* ; echo $x $ x=?????? ; echo $x $ x=[a-p]????? ; echo $x $ x=$USER sold the book for $10 ; echo $x $ x=$USER sold the book for $10 ; echo $x $ x=$USER sold the book for \$10 ; echo $x $ x=`who` ; echo Current users = $x
09-Sep-2000
Version: 2.1
161
Shell meta-characters
Examples:
$ x=* ; echo $x $ x=?????? ; echo $x $ x=[a-p]????? ; echo $x $ x=$USER sold the book for $10 ; echo $x $ x=$USER sold the book for $10 ; echo $x $ x=$USER sold the book for \$10 ; echo $x $ x=`who` ; echo Current users = $x
09-Sep-2000
Version: 2.1
162
09-Sep-2000
Version: 2.1
165
09-Sep-2000
Version: 2.1
167
09-Sep-2000
Version: 2.1
169
09-Sep-2000
Version: 2.1
170
09-Sep-2000
Version: 2.1
171
09-Sep-2000
Version: 2.1
172
09-Sep-2000
Version: 2.1
173
09-Sep-2000
Version: 2.1
174
09-Sep-2000
Version: 2.1
175
09-Sep-2000
Version: 2.1
176
Combining conditions
It is possible to combine conditions by using following operators:
-a (logical AND operator) -o (logical OR operator) ! (logical NOT operator)
09-Sep-2000
Version: 2.1
177
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]
09-Sep-2000 Version: 2.1 178
180
09-Sep-2000
Version: 2.1
181
09-Sep-2000
Version: 2.1
182
Another example
----------------------script4.sh--------------------------
#! /bin/sh usernames=`who | awk {print $1}` echo Total users logged in = $#usernames # for user in ${usernames} do echo $user done -----------------------------------------------------------09-Sep-2000 Version: 2.1 183
09-Sep-2000
Version: 2.1
184
09-Sep-2000
Version: 2.1
193
(continued...)
09-Sep-2000 Version: 2.1 195
(continued...)
09-Sep-2000 Version: 2.1 196
(continued ...)
09-Sep-2000 Version: 2.1 197
(continued ...)
09-Sep-2000 Version: 2.1 198
(end of script...)
09-Sep-2000 Version: 2.1 199
09-Sep-2000
Version: 2.1
200
09-Sep-2000
Version: 2.1
201
09-Sep-2000
Version: 2.1
202
09-Sep-2000
Version: 2.1
203
New Topic
System Programming
09-Sep-2000
Version: 2.1
204
09-Sep-2000
Version: 2.1
205
09-Sep-2000
Version: 2.1
207
09-Sep-2000
Version: 2.1
208
Next Topic
09-Sep-2000
Version: 2.1
209
Ashwin: Ashwin: Start from here Start from here 2marrow 2marrow
File Handling
Topics
Creating files Reading and writing files Getting file statistics Sharing and locking files Reading directories
09-Sep-2000
Version: 2.1
210
Unix Files
A file is a stream (series) of bytes. A file does not have any structure (such as fixed-length records, indexedetc.). There is no concept of a record as far as Unix is concerned. If you want to write an application that uses records, you need to implement this feature yourself. There is no system call to delete a part of a file (a record from a file). If you need this feature, you need to implement it yourself.
09-Sep-2000 Version: 2.1 211
Creating a file
To create a file, a process can use creat( ) or open( ) system calls. The creat( ) system call has following prototype: int creat( const char *pathname, // the name and directory // where you want the file mode_t mode // access permissions Version: 2.1 09-Sep-2000 212 );
Creating/Opening a file
The open( ) system call has following prototype:
int open( const char *pathname, int flags, mode_t mode ); // the pathname and // the mode in which // access permissions
The open( ) system, if successful, returns a descriptor (which is actually a number) to refer to the open file. The process then uses this descriptor to read/write etc. from the file.
09-Sep-2000
Version: 2.1
213
Creating/Opening a file
The flags parameter can be:
O_CREAT O_RDONLY O_WRONLY O_RDWR O_APPEND O_EXCL ( used with O_CREAT) - create a file only if it does not exist
The mode parameter indicates octal permissions to be assigned to the file. Only used this parameter when the flags contains O_CREAT.
09-Sep-2000 Version: 2.1 214
Creating a file
Try the following Try:
Change myfile.txt to /myfile.txt and run the int fd; fd = creat( myfile.txt, 0644); program
if ( -1 == fd ) { perror (Cannot create file); } else { printf(File created); }
09-Sep-2000
Version: 2.1
215
Try the following (include another header file: Change O_RDWR to O_RDWR | O_EXCL and <fcntl.h>)
run the program
int fd; fd = open( newfile.txt, O_CREAT | O_RDWR, 0644 ); if ( -1 == fd ) { perror (Cannot create file); } else { printf(File createddescriptor is: %d \n, fd ); }
09-Sep-2000 Version: 2.1 217
The read( ) system call, if successful, returns the number of bytes read from the file. If it encounters end-of-file, it returns 0.
09-Sep-2000 Version: 2.1 218
219
Writing to a file
To write to a file, a process uses the write( ) system call, which has following prototype: #include <unistd.h> ssize_t write( int fd, // file descriptor void *buf, // address of the buffer size_t count // length of the buffer ); The write( ) system call, if successful, returns the number of bytes written to the file. If it 09-Sep-2000 Version: 2.1 encounters error, it returns -1.
220
Writing to a file
Try:
int bytes; Try-2: char buffer[64]; Change fd to 1 and run the /* the existing code goes here */ program printf(Enter data:); Try-3: gets(bufffer); Change fd to bytes = write ( fd, buffer, sizeof(buffer) 0);and run the program if ( -1 == bytes ) { perror(Cannot write to file); } printf(Written %d bytes to file..\n, bytes);
09-Sep-2000 Version: 2.1 221
Closing a file
To close a file, a process uses the close( ) system call, which has following prototype: #include <unistd.h> int close( int fd, );
// file descriptor
09-Sep-2000
Version: 2.1
222
09-Sep-2000
Version: 2.1
223
09-Sep-2000
Version: 2.1
224
09-Sep-2000
Version: 2.1
225
09-Sep-2000
Version: 2.1
226
09-Sep-2000
Version: 2.1
227
09-Sep-2000
Version: 2.1
228
Deleting a file
To remove a file name, you can use unlink() system call. Please note that this call removes a name and decrements the link count. If all the names are removed (link count becomes zero), then the file is physically deleted.
int unlink(const char *pathname // pathname of file to be deleted );
09-Sep-2000
Version: 2.1
229
Locking a file
To lock/unlock a file, you can use the flock() or fcntl() system calls:
#include <sys/file.h> int flock( int fd, // file descriptor int operation // type of lock ) The lock types (operation) are: Shared (LOCK_SH) or Exclusive ( LOCK_EX)
09-Sep-2000
Version: 2.1
230
Locking a file
To lock/unlock a file, you can use flock() or fcntl() system calls: #include <sys/file.h> int flock( int fd, descriptor int operation )
The lock types (operation) are:
Unlock (LOCK_UN) or Donot wait/block for lock ( LOCK_NB)
09-Sep-2000 Version: 2.1 231
Locking a file
Example of using flock() int main() { int fd, status; fd = open( sharedfile.txt, O_RDWR); status = flock( fd, LOCK_EX | LOCK_NB ); if ( -1 == status ) { perror(Cannot lock file); }
Version: 2.1
09-Sep-2000
232
Locking a file
Example of using fcntl() to lock a file int fd, status; struct flock lockinfo; fd = open( sharedfile.txt, O_RDWR); lockinfo.l_type = F_WRLCK; // write lock lockinfo.l_whence =SEEK_SET ; // from BOF lockinfo.l_start = 0; // start from BOF lockinfo.l_len = 100; // lock 100 bytes
09-Sep-2000 Version: 2.1 233
09-Sep-2000
Version: 2.1
234
09-Sep-2000
Version: 2.1
235
09-Sep-2000
Version: 2.1
236
09-Sep-2000
Version: 2.1
239
09-Sep-2000
Version: 2.1
240
Reading Directories
To read directories, following calls are available:
opendir readdir or scandir rewinddir/seekdir closedir
09-Sep-2000
Version: 2.1
241
Reading Directories
To read directories, following calls are available:
Include <dirent.h> header file. Declare a variable DIR *d; Declare a variable struct dirent *dp; Use opendir() function to open a directory. In a while (1) loop, use readdir() function to read each directory entry. Break from the loop and close the directory when readdir returns NULL. Use printf to display following from each entry (the dp variable):
d_ino (print using %d) d_name (print using %s)
09-Sep-2000 Version: 2.1 242
09-Sep-2000
Version: 2.1
244
09-Sep-2000
Version: 2.1
245
Using debugger
Each Unix system may provide a different debugger. Most Unix systems support dbx. Linux provides the gdb debugger. To use the debugger:
compile your program with -g option to the cc command. Invoke the debugger using gdb <program_name>
09-Sep-2000
Version: 2.1
246
New Topic
09-Sep-2000
Version: 2.1
247
Process Management
Topics
Creating a new process Running a new program in a process Waiting for child process to exit Collecting exit status from child process Accessing process environment Changing process environment Process relationships (Process Groups) Redirecting output of a process to a file Redirecting input to a process from a file
09-Sep-2000
Version: 2.1
248
Ashwin: Ashwin: Satrt 4m here 24rdrd Satrt 4m here 24 april 2002 april 2002
09-Sep-2000
Version: 2.1
250
09-Sep-2000
Version: 2.1
251
09-Sep-2000
Version: 2.1
252
09-Sep-2000
Version: 2.1
253
09-Sep-2000
Version: 2.1
254
09-Sep-2000
Version: 2.1
256
09-Sep-2000
Version: 2.1
257
09-Sep-2000
Version: 2.1
258
09-Sep-2000
Version: 2.1
259
09-Sep-2000
Version: 2.1
261
09-Sep-2000
Version: 2.1
262
The parent process waits for child process to finish the ps -l command (use wait() system call) and displays following message ( and the exit status ? )
Parent <PID> : Finished running the ps command
09-Sep-2000
Version: 2.1
263
Process Relationships
When a child process (the program running in the child process) exits, it returns a status to the parent process. Therefore, when a child process is exiting, the parent process should be ready to collect the exit status. If the parent process is not ready to collect the exit status (if it is busy or suspended), the child process will not be able to exit. Such a child process is known as a zombie process.
09-Sep-2000 Version: 2.1 265
Process Relationships
Unix always ensures that a process has a parent process. If the parent process exits (while the child process is alive), Unix makes another process (the init process) as the parent process of the child that is alive. However, since the original parent process terminated, such a child process is called an orphan.
09-Sep-2000
Version: 2.1
266
09-Sep-2000
Version: 2.1
267
Process Groups
Each Unix process belongs to a process group. Unix uses process groups to distribute signals as well as for job control. By default, a process inherits its process group from the parent process. However, a process can change its process group, or create a new process group at anytime by using the setprgp() system call.
09-Sep-2000 Version: 2.1 268
Redirecting output
Unix opens three files for every Unix process. These files are identified by file descriptors 0 (stdin) , 1 (stdout) and 2 (stderr). A process can close the standard output using the close(1) system call, and use the dup() system call to redirect output to a file. The file must have been opened earlier (in write mode) using the open() system call.
09-Sep-2000
Version: 2.1
269
Redirecting input
Unix opens three files for every Unix process. These files are identified by file descriptors 0 (stdin) , 1 (stdout) and 2 (stderr). A process can close the standard input using the close(0) system call, and use the dup() system call to redirect output from a file. The file must have been opened earlier (in read mode) using the open() system call.
09-Sep-2000
Version: 2.1
270
Run the program and observe the output as well as contents of the newly created file.
09-Sep-2000 Version: 2.1 271