Académique Documents
Professionnel Documents
Culture Documents
Introduction
Unix (officially trademarked as UNIX, sometimes also written as UNIX) is a multitasking, multi-user
computer operating system originally developed in 1969 by a group of AT&T employees at Bell Labs,
including Ken Thompson, Dennis Ritchie, Brian Kernighan, Douglas McIlroy, and Joe Ossanna. The Unix
operating system was first developed in assembly language, but by 1973 had been almost entirely
recoded in C, greatly facilitating its further development and porting to other hardware. Today's Unix
system evolution is split into various branches, developed over time by AT&T as well as various
commercial vendors, universities (such as University of California, Berkeley's BSD), and non-profit
organizations.
Unix features :
Open source
Unix is open source operating system.
Portability:
The system is written in high-level language making it easier to read, understand, change and, therefore
move to other machines. The code can be changed and complied on a new machine. Customers can
Unix programming
Page 1
Unit-1
then choose from a wide variety of hardware vendors without being locked in with a particular vendor.
Machine-independence:
The System hides the machine architecture from the user, making it easier to write applications that can
run on micros, mins and mainframes.
multi-user
more than one user can use the machine at a time supported via terminals (serial or network
connection) .
A multi-user operating system allows more than one user to share the same computer system at the same
time. It does this by time-slicing the computer processor at regular intervals between the various
users.
In the above example, there are five users which share the processor hardware and main
memory on a time basis.
We start off by giving the first user (which we will call Sophia) the processor hardware, and
run Sophia's program for 1/5th of a second. When the time is up, we intervene, save Sophia's
program state (program code and data) and then start running the second users program (for
1/5th of a second).
This process continues till we eventually get back to user Sophia. To continue running Sophia's
program, we restore the programs code and data and then run for 1/5th of a second.
This switching between user programs is done by part of the kernel. To switch from one program
to another requires,
Unix programming
Page 2
Unit-1
multi-tasking
more than one program can be run at a time hierarchical directory structure to support the
organisation and maintenance of files
Multi-tasking systems support foreground and background tasks. A foreground task is one that the
user interacts directly with using the keyboard and screen. A background task is one that runs in the
background (it does not have access to the keyboard). Background tasks are usually used for printing or
backups.)
portability
only the kernel ( <10%) written in assembler tools for program development a wide range
of support tools (debuggers, compilers)
Hierarchical File System:
UNIX uses a hierarchile file structure to store information. This structure has the maximum flexibility in
grouping information in a way that reflects its natural state. It allows for easy maintenance and efficient
implementation.
UNIX shell:
UNIX has a simple user interface called the shell that has the power to provide the services that the user
wants. It protects the user from having to know the intricate hardware details.
Unix programming
Page 3
Unit-1
Utilities:
UNIX has over 200 utility programs for various functions. New utilities can be built effortlessly by
combining existing utilities.
Software Development Tools:
UNIX offers an excellent variety of tools for software development for all phases, from program editing
to maintenance of software,
Unix programming
Page 4
Unit-1
Cat[-sn][filenames]
If more than one filename is supplied, it is added at the end of the
previous file and displayed.
-n = Display numbers in ascending order in front of each line.
-s = Do not comment about nonexistent files.
filenames = The filename(s) in the order that you want them to be
displayed.
Several UNIX commands are provided for text editing and shell programming. (emp.lst) each line of this file has six fields separated by five delimiters. The details of an employee are
stored in one single line. This text file designed in fixed format and containing a personnel
database. There are 15 lines, where each field is separated by the delimiter |.
$ cat emp.lst
2233 | a.k.shukla | g.m | sales | 12/12/52 | 6000
9876 | jai sharma | director | production | 12/03/50 | 7000
5678 | sumit chakrobarty | d.g.m. | marketing | 19/04/43 | 6000
2365 | barun sengupta | director | personnel | 11/05/47 | 7800
To create, copy, remove and change permissions on files you can use the following
commands
Unix programming
Page 5
Unit-1
Unix programming
Page 6
Unit-1
Unix programming
Page 7
Unit-1
Unix programming
Page 8
Unit-1
Page 9
Unit-1
4a4
> jdoe
7c7
< mschmidt
--> proy
touch
The touch command can be used to create a new (empty) file or to update the last
access date/time on an existing file. The command is used primarily when a script
requires the pre-existence of a file (for example, to which to append information)
or when the script is checking for last date or time a function was performed.
Syntax
touch [options] [date_time] file
touch [options] [-t time] file
Common Options
-a change the access time of the file (SVR4 only)
-c dont create the file if it doesnt already exist
-f force the touch, regardless of read/write permissions m change the modification time of the file (SVR4 only)
-t time use the time specified, not the current time (SVR4 only)
CC first two digits of the year
YY second two digits of the year
MM month, 01-12
DD day of month, 01-31
hh hour of day, 0023 mm minute, 00-59
SS second, 00-61
To create a file:
% touch filename
wc
Unix programming
Page 10
Unit-1
wc stands for "word count"; the command can be used to count the number of
lines, characters, or
words in a file.
Syntax
wc [options] file
Common Options
-c count bytes
-m count characters (SVR4)
-l count line
-w count words
If not specified target defaults to a file of the same name in the present working
directory.
Common Options
-f force a link regardless of target permissions; dont report errors (SVR4 only)
-s make a symbolic link
Examples
A symbolic link is used to create a new path to another file or directory. If a group
of users, for
example, is accustomed to using a command called chkmag, but the command has
been rewritten and
is now called chkit, creating a symbolic link so the users will automatica
automatically execute chkit when they
enter the command chkmag will ease transition to the new command.
A symbolic link would be done in the following way:
% ln -s chkit chkmag
Unix programming
Page 11
Unit-1
tee sends standard in to specified files and also to standard out. Its often used in
command pipelines.
Syntax
tee [options] [file[s]]
Common Options
-a append the output to the files
-i ignore interrupts
Examples
In this first example the output of who is displayed on the screen and stored in the
file users.file:
brigadier: condron [55]> who | tee users.file
condron ttyp0 Apr 22 14:10 (lcondronpc.acs.) frank ttyp1 Apr 22 16:19 (nyssa)
condron ttyp9 Apr 22 15:52 (lcondron-mac.acs)
brigadier: condron [56]> cat users.file
condron ttyp0 Apr 22 14:10 (lcondronpc.acs.) frank ttyp1 Apr 22 16:19 (nyssa)
Unix programming
Page 12
Unit-1
Unix programming
Page 13
Unit-1
Unix programming
Page 14
Unit-1
Let us consider that the file address book contains the details of three
persons cat addressbook
paste -s addressbook -to print in one single line
paste -s -d | | \n addressbook -are used in a circular manner
sort : ordering a file
Sorting is the ordering of data in ascending or descending sequence. The sort command orders a
file and by default, the entire line is sorted
sort shortlist
This default sorting sequence can be altered by using certain options. We can also
sort one or more keys (fileds) or use a different ordering rule.
sort options
The important sort options are:
-tchar
-k n
-k m,n
-k m.n
-u
-n
sorts numerically
-r
-f
-m list
-c
Unix programming
Page 15
Unit-1
sort t| k 2 shortlist
sort t| r k 2 shortlist
or
sort t| k 2r shortlist
we can also specify a character position with in a field to be the beginning of sort as
shown above (sorting on columns).
sort n numfile
when sort acts on numericals, strange things can happen. When we sort a file containing
only numbers, we get a curious result. This can be overridden by n (numeric) option.
Unix programming
Page 16
Unit-1
Removing repeated lines can be possible using u option as shown above. If we cut out
the designation filed from emp.lst, we can pipe it to sort to find out the unique designations that
occur in the file.
Page 17
Unit-1
Page 18
Unit-1
File type
File permissions
Number of links
The UID of the owner
The GID of the group owner
File size in bytes
Date and time of last modification
Date and time of last access
Date and time of last change of the inode
An array of pointers that keep track of all disk blocks used by the file
One of the distinguishing features of Unix is that it was designed from its earliest days to be a
multiuser system. In contrast, it is only in recent years that other operating systems have created
true multiuser functionality on a single machine. Because of its multiple-user design, Unix must
use mechanisms that enable users to manage their own files without having access to the files of
other users. These mechanisms are called file ownership and file permissions.
File Ownership
Generally, the files that the user owns are ones that he created, or which were created as a result
of some action on his part. The superuser can change the ownership of any file, whether he
created it or not, with the chown command. For example, if the superuser gives the command
chown jane /home/bill/billsfile
Username versus UID
idea of a username, the name you use when you log in to a Unix machine. The name is assigned
to you by the system administrator . In addition to a username, every user has a numerical ID
number known as a user ID or UID, which is how the user is known to the system. Typically,
these numnumbers are assigned automatically,although they can be specified when an account is
created. The number itself is arbitrary, even though many systems require that ordinary users
have UID numbers above 500.
The superuser always has UID 0.
Unix programming
Page 19
Unit-1
For purposes other than logging in, the username and UID are basically synonymous. For
example, the command
chown jane /home/bill/billsfile could just as easily be rendered as
chown 503 /home/bill/billsfile assuming that Janes UID is 503
Groups
In addition to a UID, you also have at least one group ID, or GID. As with the UID, the operating system
uses GIDs rather than the group names to manage groups. Every user belongs to at least one group, and
may belong to several more. Groups contain users who share certain permissions for certain
activities.You may belong to a group that has the same name as your username. The superuser may add
you to other groups depending on the access you need to certain files or directories. Groups can also own
files, and file ownership can be transferred from one group to another. To do so use the chgroup (change
group) command
chgroup groupname filename
File Permissions
Permissions should be as restrictive as possible. As long as legitimate users can use the file in
intended ways, every other avenue of access should be locked down.
There are three kinds of file permission:
Read (file can be viewed)
Write (file can be edited)
A file or a directory is created with a default set of permissions, which can be determined
by umask. Let us assume that the file permission for the created file is -rw-r--r--. Using chmod
Unix programming
Page 20
Unit-1
command, we can change the file permissions and allow the owner to execute his file. The
command can be used in two ways:
Relative Permissions
chmod only changes the permissions specified in the command line and leaves the other
permissions unchanged. Its syntax is:
Category
operation
permission
u - user
+ assign
r - read
g - group
- remove
w - write
o - others
= absolute
x - execute
a - all (ugo)
Unix programming
Page 21
Unit-1
-rwxr--r-- 1
The command assigns (+) execute (x) permission to the user (u), other permissions remain
unchanged.
or
chmod +x xstart
-rwxr-xr-x 1
Let initially,
-rwxr-xr-x
Unix programming
xstart
Page 22
Unit-1
Then, it becomes
Absolute Permissions
Here, we need not to know the current file permissions. We can set all nine permissions
explicitly. A string of three octal digits is used as an expression. The permission can be
represented by one octal digit for each category. For each category, we add octal digits. If we
represent the permissions of each category by one octal digit, this is how the permission can be
represented:
Octal
Permissions
Significance
---
no permissions
--x
execute only
-w-
write only
-wx
r--
read only
r-x
Unix programming
Page 23
Unit-1
rw-
rwx
We have three categories and three permissions for each category, so three octal digits
can describe a files permissions completely. The most significant digit represents user and the
least one represents others. chmod can use this three-digit string as the expression.
will assign all permissions to the owner, read and write permissions for the group and only
execute permission to the others.
777 signify all permissions for all categories, but still we can prevent a file from being
deleted. 000 signifies absence of all permissions for all categories, but still we can delete a file. It
is the directory permissions that determine whether a file can be deleted or not. Only owner can
Unix programming
Page 24
Unit-1
change the file permissions. User can not change other users files permissions. But the system
administrator can do anything.
or
----------
This is simply useless but still the user can delete this file
On the other hand,
-rwxrwxrwx
The UNIX system by default, never allows this situation as you can never have a secure system.
Hence, directory permissions also play a very vital role here
Unix programming
Page 25
Unit-1
This makes all the files and subdirectories found in the shell_scripts directory, executable by all
users. When you know the shell meta characters well, you will appreciate that the * doesnt
match filenames beginning with a dot. The dot is generally a safer but note that both commands
change the permissions of directories also.
Directory Permissions
It is possible that a file cannot be accessed even though it has read permission, and can be
removed even when it is write protected. The default permissions of a directory are,
rwxr-xr-x (755)
Example:
mkdir c_progs
ls ld c_progs
Unix programming
Page 26
Unit-1
If a directory has write permission for group and others also, be assured that every user
can remove every file in the directory. As a rule, you must not make directories universally
writable unless you have definite reasons to do so.
Usually, on BSD and AT&T systems, there are two commands meant to change the
ownership of a file or directory. Let kumar be the owner and metal be the group owner. If sharma
copies a file of kumar, then sharma will become its owner and he can manipulate the attributes
chown
ls -l note
Unix programming
Page 27
Unit-1
Once ownership of the file has been given away to sharma, the user file permissions that
previously applied to Kumar now apply to sharma. Thus, Kumar can no longer edit note since
there is no write privilege for group and others. He can not get back the ownership either. But he
can copy the file to his own directory, in which case he becomes the owner of the copy.
chgrp
This command changes the files group owner. No superuser permission is required.
ls l dept.lst
In this chapter we considered two important file attributes permissions and ownership. After
we complete the first round of discussions related to files, we will take up the other file
attributes.
Unix programming
Page 28
Unit-1
process utilities
Process States
Ps-show status of active processes
ps [options]
Common Options
BSD SVR4
-a -e all processes, all users
-e environment/everything
-g process group leaders as well
-l -l long format
-u -u user user oriented report
-x -e even processes not executed from terminals
-f full listing
Unix programming
Page 29
Unit-1
System Processes
System processes are programs running behind the scenes handling many essential maintenance aspects
for your system. Normally, system processes do not have a TTY (teletype) in use. Many of these
processes are often called daemons, and they do routine work. The following is an example of system
processes running on a Linux system
$ ps ax
PID TTY STAT TIME COMMAND
1 ? S 0:00 init [3]
2 ? SW 0:00 [migration/0]
3 ? SWN 0:00 [ksoftirqd/0]
4 ? SW< 0:00 [events/0]
5 ? SW< 0:00 [khelper]
6 ? SW< 0:00 [kacpid]
20 ? SW< 0:00 [kblockd/0]
21 ? SW 0:00 [khubd]
31 ? SW 0:00 [pdflush]
32 ? SW 0:00 [pdflush]
33 ? SW 0:13 [kswapd0]
34 ? SW< 0:00 [aio/0]
618 ? SW 0:00 [kseriod]
646 ? SW< 0:00 [ata/0]
647 ? SW 0:00 [khpsbpkt]
670 ? SW 0:00 [kjournald]
788 ? SW 0:00 [kjournald]
793 ? S 0:00 /usr/sbin/syslogd
797 ? S 0:00 /usr/sbin/klogd
816 ? S 0:00 /usr/sbin/sshd
Unix programming
Page 30
Unit-1
Process Attributes
Each process has an environment with various attributes such as command-line arguments, user
environment variables, file descriptors, working directory, file creation mask, controlling terminal
(console),resource limitations, and a lot more. Many of the attributes are shared with the parent process.
To view the various process attributes, you can use the ps -o switch, which is available for both styles of ps
Unix programming
Page 31
Unit-1
Stopping Processes
kill - terminate a process
Ending a process can be done in several different ways. Often, from a console-based command, sending a
CTRL + C keystroke (the default interrupt character) will exit the command. The standard tool for killing a
process is kill. Technically, the kill command does not kill a command,but sends a special signal to the process. Signals
are used for simple communication between processes. To use the kill command, just place the process ID of the
process to signal as the command line argument.For example, to send the default SIGTERM signal to the
process ID 5432, run the command:
kill 5432
To list the possible signal names, run kill with the -l switch:
$ kill -l
HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS
SEGV SYS PIPE ALRM TERM USR1 USR2 CLD PWR WINCH URG
POLL STOP TSTP CONT TTIN TTOU VTALRM PROF XCPU
XFSZ WAITING LWP FREEZE THAW CANCEL LOST XRES RTMIN RTMIN+1
RTMIN+2 RTMIN+3 RTMAX-3 RTMAX-2 RTMAX-1 RTMAX
Page 32
Unit-1
Zombie Processes
Normally, when a child process is killed, the parent process is told via a SIGCHLD signal. Then the
parent can do some other task or restart a new child as needed. However, sometimes the parent process is
killed. In this case, the parent of all processes, init, becomes the new PPID (parent process ID). You can
often see this indicated by a process ID of 1 as the PPID of some other process.
disk utilities
df - summarize disk block and file usage
df is used to report the number of disk blocks and inodes used and free for each file
system. The
Unix programming
Page 33
Unit-1
output format and valid options are very specific to the OS and program version in
use.
Syntax
df [options] [resource]
Common Options
-l local file systems only
(SVR4) -k report in kilobytes
(SVR4) df
Filesystem kbytes used avail capacity Mounted on
/dev/sd0a 20895 19224 0 102% /
/dev/sd0h 319055 131293 155857 46% /usr
/dev/sd1g 637726 348809 225145 61% /usr/local
Common Options
-a display disk usage for each file, not just subdirectories
-s display a summary total only
-k report in kilobytes (SVR4)
du
1 ./.elm
1 ./Mail
1 ./News
20 ./uc
du -a uc
7 uc/unixgrep.txt
5 uc/editors.txt
1 uc/.emacs
1 uc/.exrc
4 uc/telnet.ftp
1 uc/uniq.tee.txt
20 uc
Unix programming
Page 34
Unit-1
networking commands
TELNET and FTP are Application Level Internet protocols. The TELNET and
FTP protocol specifications have been implemented by many different sources,
including The National Center for Supercomputer Applications (NCSA), and many
other public domain and shareware sources rlogin is a remote login service that
was at one time exclusive to Berkeley 4.3 BSD UNIX.
Essentially, it offers the same functionality as telnet, except that it passes to the
remote computer information about the user's login environment. Machines can be
configured to allow connections from trusted hosts without prompting for the
users passwordsA more secure version of this protocol is the Secure SHell, SSH,
software written by Tatu Ylonen and available via ftp://ftp.net.ohiostate.edu/pub/security/ssh.
the r commandsrsh (remotshell), rcp (remote copy), and rlogin (remote login)were prevalent
in the past, but because they
offer little security, theyre generally discouraged in todays environments. rsh and rlogin are
similar in functionality to telnet, and rcp is similar to ftp.
telnet [options] [ remote_host [ port_number ] ]
tn3270 [options] [ remote_host [ port_number ] ]
ftp [options] [ remote_host ]
Common Options
ftp telnet Action
-d set debugging mode on
-d same as above (SVR4 only) -i
turn off interactive prompting
-n dont attempt auto-login on connection
-v verbose mode on
-l user connect with username, user, on the remote host (SVR4
only) -8 8-bit data path (SVR4 only)
telnet solaris
or
telnet 192.168.1
Unix programming
Page 35
Unit-1
telnet oscar.us.ohio-state.edu
tn3270 ohstmvsa.acs.ohio-state.edu
ftp magnus.acs.ohio-state.edu
Common Options
-l force long output format
-m match username only, not first or last names
-s force short output format
Examples
brigadier: condron [77]> finger workshop@nyssa This
is a sample .plan file for the nyssa id, workshop.
This id is being used this week by Frank Fiamingo, Linda
DeBula, and Linda Condron, while we teach a pilot version
of the new Unix workshop we developed for UTS.
Remote login
rlogin [ -l username ] remote_host
rsh [ -l username ] remote_host [ command ]
rcp [ [user1]@host1:]original_filename [ [user2]@host2:]new_filename
where the parts in brackets ([]) are optional. rcp does not prompt for passwords, so
you must have permission to execute remote commands on the specified machines
as the selected user on each machine.
Common Options
-l username connect as the user, username, on the remote host (rlogin & rsh)
Using ssh
ssh (Secure SHell) and telnet are two methods that enable you to log in to a remote
system and run commands interactively;
command hostname
ssh darwin
or
ssh 192.168.1.58
Unix programming
Page 36
Unit-1
backup utilities
Deciding what to back up is a time-consuming process, but well worth the effort, especially if you
have to recover information.
generally want to back up the entire file system. If that isnt possible because of resource
constraints(money and/or time)
_Zip drivesAn older technology losing market share because of its low capacity and slow speed. Hard
drive/network backupThis type of backup is more and more common with the price of hard
Backup Types
Full: Backs up all data within the scope of the backup definition
Differential: Backs up all data that has changed since the last full backup
Incremental: Backs up all data that has changed since the completion of any other type of Backup
Backup Commands
Using tar
The tar (tape archive) command enables you to create tape archives and, if needed, send them
directly to a tape device. The tar command has many functions, including:
tShows the table of contents for the tarfile
xExtracts or restores the contents of the tarfile
uUpdates the contents of a tarfile
The syntax for creating a tarfile is:
tar -cvf tarfile_name_or_tape_device directory_name
The c argument means to begin writing at the beginning of the tarfile,
the v represents verbose, which provides a lot of output showing what the command is
doing.
The f option notifies the tar command that the user will be providing a tarfile name
instead of using the default identified in /etc/default/tar.
To create a tarfile with the contents of the entire /etc directory to a properly configured
tape device named /dev/rmt0, for example, youd use the following command:
tar -cvf /dev/rmt0 /etc
If you want only a simple listing, you can omit the v option, reducing output; your command
would be
tar -tf /dev/rmt0
or
Unit-1
To view information on the gzipped file, you can use the -l option, which shows output similar to
the
following:
#gzip -l mytextfile.gz
compressed uncompr. ratio uncompressed_name
614 1660 64.8% mytextfile
bzip2
The bzip2 command offers the same type of functionality as gzip, with some improvements on
the
compression capability.
Heres the syntax for a simple file compression using bzip2:
bzip2 filename
cpio
The cpio (copy in/out) command is more difficult to use than the tar command but provides some special
functionality, such as the capability to save special files
Unix programming
Page 38
Unit-1
We often need to search a file for a pattern, either to see the lines containing (or not
containing) it or to have it replaced with something else. This chapter discusses two important
filters that are specially suited for these tasks grep and sed. grep takes care of all search
requirements we may have. sed goes further and can even manipulate the individual characters in
a line. In fact sed can de several things, some of then quite well.
grep searching for a pattern
It scans the file / input for a pattern and displays lines containing the pattern, the line
numbers or filenames where the pattern occurs. Its a command from a special family in UNIX
for handling search requirements.
grep options pattern filename(s)
grep sales emp.lst
will display lines containing sales from the file emp.lst. Patterns with and without quotes is
possible. Its generally safe to quote the pattern. Quote is mandatory when pattern involves more
than one word. It returns the prompt in case the pattern cant be located.
grep president emp.lst
Unix programming
Page 39
Unit-1
When grep is used with multiple filenames, it displays the filenames along with the output.
grep director emp1.lst emp2.lst
Where it shows filename followed by the contents
grep options
grep is one of the most important UNIX commands, and we must know the options that
POSIX requires grep to support. Linux supports all of these options.
-i
-v
-n
-c
-l
-e exp
-x
-f file
-E
-F
Page 40
Unit-1
g*
A single character
.*
[pqr]
a single character p, q or r
[c1-c2]
Page 41
Unit-1
hyphen has a lower ASCII value than the one on the right. Negating a class (^) (caret) can be
used to negate the character class. When the character class begins with this character, all
characters other than the ones grouped in the class are matched.
The *
The asterisk refers to the immediately preceding character. * indicates zero or more occurrences
of the previous character.
g* nothing or g, gg, ggg, etc.
grep [aA]gg*[ar][ar]wal emp.lst
Notice that we dont require to use e option three times to get the same output!!!!!
The dot
A dot matches a single character. The shell uses ? Character to indicate that.
.*
Page 42
Unit-1
ch?
exp1|exp2
(x1|x2)x3
Unix programming
Page 43
Unit-1
find
You can also use the find command to locate files in Unix, although this command may consume
a lot of resources (and make the system respond slowly).
Heres the syntax:
find pathname selection_riteria options
Common Options
For the time search options the notation in days, n is:
Unix programming
Page 44
Unit-1
wc
Use the wc command to determine the length of a given file. wc uses the
syntax: wc [options] filename
By default, the output shows the length in words. The following table shows the options
available for wc.
Unix programming
Page 45