Unix Full Notes-1

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 90

UNIX 2023

Syllabus
1. Introduction: The operating System, The Unix OS, Knowing Your Machine, A Brief Session,
Conclusion, Background: How It all Clicked, POSIX and the Single Unix Specification, Linux and
GNU, The Unix Architecture, Features of Unix, Conclusion.
10Hrs

2. Understanding the Unix Command: Locating Commands, Internal and External Commands,
Command Structure, Flexibility of Command Usage, man, Understanding the man Documentation,
Further Help with man -k, apropos and What is, When Things Go Wrong, Conclusion,
General Purpose Utilities: cal, date, echo, printf, bc, script, passwd, who, uname, tty, sty,
Conclusion.
12Hrs

3. The File System: The File, What’s a (File) name?, The Parent–Child Relationship, The HOME
Variable, pwd, cd, mkdir, rmdir, Absolute Pathnames, Relative Pathnames, ls, The UNIX File
System, Conclusion, Handling Ordinary Files: cat, cp, rm, mv, more, the lp subsystem, file, wc, od,
cmp, comm., diff, dos2uinx and unix2dos, Compressing and Archiving Files, gzip and gunzip, tar, zip
and unzip, Conclusion.
10Hrs.

4. Basic File Attributes: ls-l, The –d Option, File Ownership, File Permissions, chmod, Directory
Permissions, Changing File Ownership, Conclusion, The vi Editor: vi Basics, I/P Mode, The ex ode,
Navigation, Editing Text, Undoing Last Editing Instructions (u and u), Repeating the Last Command,
Searching for a pattern, Substitution, Conclusion.
8Hrs

5. The Shell: The Shell’s Interpretive Cycle, Shell Offerings, Pattern Matching, Escaping and
quoting, Redirection, /dev/null and /dev/tty, pipes, tee, Command Substitution, Shell Variables,
Conclusion.
8Hrs

6. The Process: Process Basics, Process Status, System Processes, Mechanism of Process Creation,
Running Jobs in Background, Killing Processes with Signal, Job Control.
4Hrs

7. Essential Shell Programming: Shell Scripts, Making Scripts Interactive, Using Command Line
Arguments, exit and exit Status of Command, The Logical Operators && and || -Conditional
execution, The if Conditional, Using test and [] to evaluate Expressions, The case Conditional, expr:
Computation and String Handling, Calling a Script by Different Names, while: Looping, for: Looping
with a list, Manipulating the Positional Parameters, The here document (<<), trap: Interrupting a
Program, Debugging Shell Scripts with set -x, Sample Validation and Data entry scripts, Conclusion.
10Hrs

Text book:
1. “UNIX Concepts and Applications” by Sumitabha Das, Third Edition, Tata McGraw-Hill

DON BOSCO CHITRADURGA Page 1


UNIX 2023

1. Introduction:
What is Unix?

The Unix operating system is a set of programs that act as a link between the computer and the user. The
computer programs that allocate the system resources and coordinate all the details of the computer's internals is
called the operating system or the kernel.

Users communicate with the kernel through a program known as the shell. The shell is a command line
interpreter; it translates commands entered by the user and converts them into a language that is understood by
the kernel.

 Unix was originally developed in 1969 by a group of AT&T employees Ken Thompson, Dennis
Ritchie, Douglas McIlroy, and Joe Ossanna at Bell Labs.

 There are various Unix variants available in the market. Solaris Unix, AIX, HP Unix and BSD are a
few examples. Linux is also a flavor of Unix which is freely available.

 Several people can use a Unix computer at the same time; hence Unix is called a multiuser system.

 A user can also run multiple programs at the same time; hence Unix is a multitasking environment.

Operating system:
An operating system is the software that manages the computer’s hardware and provides a convenient and safe
environment for running program. It acts as an interface between programs and the hardware resources that
these programs access. It is loaded into memory when a computer is booted and remains active as long as the
machine is up.

Features of Operating system:


 The operating system allocates memory for the program and loads the program to the allocated
memory.
 It also loads the CPU registers with control information related to the program. The registers maintain
the memory locations where each segment of a program is stored.
 The instructions provided in the program are executed by the CPU. The operating system keeps track
of the instruction that was last executed. This enables it to resume a program if it had to be taken out of
the CPU before it completed execution.
 If the program needs to access the hardware, it makes a call to the operating system rather than
attempting to do the job itself. For instance, if the program needs to read a file on disk, the operating
system directs the disk controller to open the file and make the data available to the program.
 After the program has completed execution, the operating system cleans up the memory and registers
and makes them available for the next program.

UNIX operating system

Definition: UNIX is an operating system and one of the popular OS since decades because of its multi-user,
multi-tasking environment, stability, portability and powerful networking capabilities.

The UNIX was founded by Ken Thompson, Dennis Ritchie and Brain kerninghan at AT& T Bell Labs
research in 1969.

Linux was founded by Linus Benedict Torvalds in 1991.

UNIX is a giant OS. It has richness and elegance go beyond the commands and tools that constitute it, while
simplicity permeates the entire system. It runs on practically every hardware and provides inspiration to the
open source movement.

DON BOSCO CHITRADURGA Page 2


UNIX 2023
UNIX offers a host of applications that benefit users, programmers, and system administrators. It encourages
users to combine multiple programs to solve a complex problem.

For programmers, UNIX offers a rich set of programming tools that aid in developing, debugging, and
maintaining programs. UNIX is also more easily maintained than most systems.

One of these programs is the system’s command interpreter, called the shell. You interact with a UNIX system
through the shell.

A Brief History of UNIX


 In the late 1960s, researchers from General Electric, MIT and Bell Labs launched a joint project to
develop an multi-user, multi-tasking OS for mainframe computers known as MULTICS (Multiplexed
Information and Computing System).
 For some reasons MULTICS failed, but it did inspire Ken Thompson, who was a researcher at Bell
Labs, to have a go at writing a simpler operating system himself. He wrote a simpler version of
MULTICS on a PDP7 in assembler and called his attempt UNICS (Uniplexed Information and
Computing System).
 Because memory and CPU power were at a premium in those days, UNICS (eventually shortened to
UNIX) used short commands to minimize the space needed to store them and the time needed to
decode them. For EXAMPLES: the tradition of short UNIX commands we use today, e.g. ls, cp, rm,
mv etc.
 Ken Thompson then teamed up with Dennis Ritchie, the author of the first C compiler in 1973. They
rewrote the UNIX kernel in C - this was a big step forwards in terms of the system's portability - and
released the Fifth Edition of UNIX to universities in 1974.
 The Seventh Edition, released in 1978, marked a split in UNIX development into two main branches:
SYSV (System 5) and BSD (Berkeley Software Distribution).
 BSD arose from the University of California at Berkeley where Ken Thompson spent a sabbatical
year. Its development was continued by students at Berkeley and other research institutions.
 SYSV was developed by AT&T and other commercial companies. UNIX flavors based on SYSV have
traditionally been more conservative, but better supported than BSD- based flavors.

Differences between UNIX and DOS:

UNIX DOS
1 UNIX can have GUI DOS Can’t have GUI
2 UNIX is more secure DOS is not more secure compared to UNIX
3 UNIX is multitasking DOS is singletasking
4 UNIX are multiuser DOS is single user
5 UNIX is case sensitive DOS is not case sensitive
6 UNIX is used in servers DOS is used in embedded systems

Differences between UNIX and Windows:

UNIX WINDOWS
1 UNIX is an open source(free downloadable). Windows is not an open source(paid one).
2 UNIX has very high security system Windows has low security system
3 UNIX is a command based operating Windows is not a command based
System operating system
4 The file system is arranged in The file system is arranged in parallel
hierarchical manner Manner.
5 UNIX is not a user friendly. Windows is a user friendly.
6 Free office(such as excel, PowerPoint Pay for MS Office
and others).
7 Low Hardware cost High Hardware cost

DON BOSCO CHITRADURGA Page 3


UNIX 2023
Brief Session:

1. Logging in with User name and Password


UNIX is security-conscious, and can be used only by those persons who maintain an account with the computer
system. The list of accounts is maintained separately in the computer.

The system can be used by many users, someone has to be given charge of administration of the
system. This person is known as the system administrator, and he is the person who will grant you the
authority to use the system.

The login prompt indicates that the terminal is available for someone to log in. This message also
indicates that the previous user has logged out. Since you now an account named “DON BOSCO”, enter this
string at the prompt. Then press the [enter] key after the string:

Login: DON BOSCO [enter]


Password:

The system now requested you to enter the secret code (PASSWORD) that was handed to you by your
administrator. This code should be known to none except yourself.
Type the secret code and press [enter]:

Login: DON BOSCO


Password: ******* [enter]

The string that you entered at the first prompt (login:) is known variously as your login name, user-id, or
username. The secret code that you entered the next prompt (password:) is known as the password. If you enter
either of them incorrectly, the system flashes the following message:

Login incorrect
Login:_

2. Command :

Command is an instruction given to the computer to do a task.


We have now been able to successfully log on to a UNIX system. Let’s enter something from the keyboard,
press[enter], and then see what happens:
$ unix [enter]
Ksh: unix : command not found

3. date : Displaying both date and time


UNIX has a date command that shows the date and time in the form used on the internet. Type the four
character in date and press [enter].

$ date [ enter]
Thu Mar 16 11:22:33 IST 2023
$_
The date command instructs the machine to display the current date and time.

4. tput clear: Clearing the Screen


All UNIX systems offer the tput command to clear the screen. This is something you’d often like to do to avoid
getting distracted by output or error messages of previous commands. However, when you use tput as it is (i.e
without any additional words), this is what UNIX has to say:
$ tput
Usage: tput [-T [term]] capname [parm argument…]
Or: tput –S <<

DON BOSCO CHITRADURGA Page 4


UNIX 2023
This message makes little sense to a beginner, so we won’t attempt to interpret it right now. However, tput
requires additional input to work properly. To make tput work, follow tput with the word clear:

$ tput clear

The screen clears and the prompt and cursor are positioned at the top-left corner.

5. cal : The Calender


Cal is to see the calendar for the month Mar 2023, provide the month number and year as the two argument to
cal:

$ cal 3 2023

6. who: Who are the Users?


UNIX is a system who are using the system. Use the who command:
$ who

DON BOSCO console March 11:22:33(:00)

7. ps : Viewing Processes
Every command that you run gives rise to a process, and the shell is a process as well. To view all process that
you are responsible for creating, run the ps command:

$ ps

PID TTY TIME CMD


364 console 0:00 ksh

ps generates a header followed by a line containing the details of the ksh process. When you run several
programs, there will be multiple lines in the ps output. Ksh represents the Korn Shell and is constantly running
at this terminal. This process has unique number 364 (called process-id or PID), and when you log out, this
process is killed.

8. ls : Listing Files
Your UNIX system has a large number of files that control its functioning, and users also create files on their
own. These files are organized in separate folders called directories. You can list the names of the files available
in this directory with the ls command:

$ ls
README upper case first
chap01
chap02
chap03
helpdir
progs

ls displays a list of six files, three of which actually contain the chapters of this textbook. Note that the files are
arranged alphabetic with uppercase having precedence over lower.
Since the files containing the first three chapters have similar filenames, UNIX lets you use a special
short-hand notation (*) to access them:

$ ls chap*
chap01
chap02
chap03

DON BOSCO CHITRADURGA Page 5


UNIX 2023
Sometimes, just displaying a list of filenames isn’t enough; you need to know more about these files. For that to
happen, ls has to be used with an option, -l, between the command and filenames:

$ ls –l chap* -l is an option
_rw_r_ _r_ _ 1 xyz user 5609 Apr 23 09:30 chap01
_rw_r_ _r_ _ 1 xyz user 26129 may 13 19:30 chap02
_rw_r_ _r_ _ 1 xyz user 54609 feb 15 12:30 chap03

The argument begging with a hyphen is known as an option. The characteristic feature of most command
options is that they begin with a – (hyphen).

9. Directing Output to a File


UNIX has simple symbols (called metacharacters) for creating and storing information in files. Instead of
viewing the output of the ls command on the terminal, you can save the information in a file, list, by using a
special symbol, > :

$ ls > list
$_

You see nothing on the terminal except the return of the prompt. The shell is at work here. It has a mechanism
of redirecting any output, normally coming to the terminal, to a disk file. To check whether the shell has actually
done the job, use the cat command with the file name as argument:

$ cat list cat displays a file’s content


README
chap01
chap02
chap03
helpdir
progs

10. wc :Counting Number of Lines in a File


How many lines are there in the file? The wc command

$ wc list
6 6 42 list

It has the three numbers along with file name. You have to have the manual or this text in front of you to know
that the file list contain 6 lines, 6 words and 42 characters.

11. Feeding Output of One Command to Another

Previously, you used ls to list files, and then the > symbol to save the output in the file list. You then counted
the number of lines, words, and characters in this file with wc. In this way, you could indirectly count the
number of files in the directory. The shell can do better; its manipulative capability enables a direct count
without creating an intermediate file. Using the | symbol, it connects two commands to create a pipeline:

$ ls | wc
6 6 42 no file name this time!

On seeing the symbol, | ( known as a pipe), the shell performs the job of connecting the two commands.

12. Programming with the shell


The system also features a programming facility. You can assign a value to a variable at the prompt:
$ x=5 no spaces on either side of =

DON BOSCO CHITRADURGA Page 6


UNIX 2023
$_

And then evaluate the value of this variable with the echo command and a $-prefixed variable name:

$ echo $x A $ required during evaluation


5

13. exit : Signing Off


To suspend the session for the time being. You should use the exit command to do that:

$ exit
login:

Alternatively, you may be able to use [ctrl-d] (generating by pressing the ctrl key and the character d on the
keyboard) to quit the session.
The login : message confirms that the session has been terminated, thus making it available for the next user.

A Conceptual Background
How It All Clicked
With the goal of building a comfortable relationship with the machine, Thompson and Ritchie designed
a system for their own use rather than for others. They could afford to do this because UNIX wasn’t initially
developed as a commercial product, and the product, and the project didn’t have any predefined objective.

UNIX is a command-based system, and you have used a number of them already in the hands-on
session. These commands have varied usage and often have a large number of options and arguments.

LINUX AND GNU


Although UNIX finally turned commercial, Richard Stallman and Linus Torvalds had different ideas.
Torvalds is the father of Linux, the free UNIX that has swept the computer world by storm. Stallman runs the
Free Software Foundation (formerly known as GNU—a recursive acronym that stands for “GNU’s Not Unix”)
Linux is distributed under the GNU General Public License which makes it mandatory for developers and
sellers to make the source code public. Linux is particularly strong in networking and internet features, and is an
extremely cost-effective solution in setting up an internet server or a local internet.

The most popular GNU/Linux flavours include Red Hat, Caldera, SuSE, Debian and Mandrake.

THE UNIX ARCHITECHTURE


The entire UNIX system is supported by a handful of essentially simple, through somewhat abstract concept.

a. Division of Labor: The division of labor between 2 agencies—the kernel and shell.
Kernel: The kernel is the heart of the operating system. It interacts with hardware and most of the tasks like
memory management, trash scheduling and file management.
The kernel is the core of the OS – a collection of routines mostly written in c. It is loaded into memory when
the system is booted and communicates directly with the hardware. User programs that need to access the
hardware use the services of the kernel, which performs the job on the user behalf. These programs access the
kernel through a set of functions called system calls.
Shell: The shell is the utility that processes your requests. When you type in a command at your terminal, the
shell interprets the command and calls the program that you want. The shell uses standard syntax for all
commands. C Shell, Bourne Shell and Korn Shell are most famous shells which are available with most of the
Unix variants.

DON BOSCO CHITRADURGA Page 7


UNIX 2023
Commands and Utilities: There are various command and utilities which you would use in your day to day
activities. cp, mv, cat and grep etc. are few examples of commands and utilities. There are over 250 standard
commands plus numerous others provided through 3rd party software. All the commands come along with
various optional options.
b. Files and Directories: All data in UNIX is organized into files. All files are organized into directories. These
directories are organized into a tree-like structure called the file system.

Process: Another entity is the process, which is the name given to a file when it is executed as a program. You
can say that a process is simply the “time image” of an executable file. Like files, process also belongs to a
separate hierarchical tree structure.
c. The system call: The UNIX system, comprising the kernel, shell and application programs written in c.
though there are over a thousands of commands in the system. They all uses a handful of functions called
system calls, to communicate with the kernel.

FEATURES OF UNIX OPERATING SYSTEM


The following section presents the major features of these operating systems.

1. UNIX: A Multiuser System


UNIX is a multiprogramming system. It permits multiple programs to run and compete for the affection of
the CPU. This can happen in two ways:
 Multiple users can run separate jobs
 A single user can also run multiple jobs
In UNIX, the resources are actually shared between all users. UNIX is also a multi user system.

2. UNIX: A Multitasking System Too


A single user can also run multiple task concurrently; UNIX is a multitasking system. It is usual for a
user to edit a file, print another one on the printer, send a mail to a friend and browse the World Wide Web – all
without leaving any of the applications. The kernel is designed to handle a user’s multiple needs.
In a multitasking environment, a user has seen a one job running in the foreground, the rest of the job in
background. You can switch jobs between background and foreground, suspend or terminate them.

3. The Building—Block Approach


In UNIX, there is no separate command was designed to perform the multiple job. The command that
can be connected in this way are called the filters because they filter or manipulator data in different ways.

DON BOSCO CHITRADURGA Page 8


UNIX 2023

For example: you have to see how two commands are connected i.e ls and wc were with the | (pipe) to count the
number of files in your directory.

It’s through a pipe and filter that UNIX implements the small—is—beautiful philosophy. Today many UNIX
tools are designed with the requirements that the output of one tool be used as input to another.

4. The UNIX tool kit


There are number of general purpose tools such as, text manipulation utilities (called filters), compilers
and interpreters, networked applications and system administration tools. New tools are being added and the
older ones are being removed or modified.

5. Pattern Matching
The “ * “ is a special character used by the system to indicate that it can match a number of file names.

UNIX features elaborate pattern matching schemes that use several character from this meta characters set.
Some of the most advanced and useful tools also use a special expression called regular expression that is
framed with characters from this set.

6. Programming facility
The UNIX shell is also a programming language. It was designed for a programmer, not a casual end
user. It has all the necessary ingredients, like control structures, loops and variables, that establish it as a
powerful programming language in its own right. These features are used to design shell scripts. Programs that
can also invoke the UNIX command.

7. Documentation: Manually accessing the data


The principle of online facility available in the man command, which remains the most important
reference for command and their configuration files.
Apart from an online documentation, there is a vast ocean of UNIX resource available on the internet. There are
several news groups on UNIX where you can fire. Your queries in case you are standard with a problem--be it a
problem related to shell programming or a network configuration issue. The FAQ (frequently asked question) –
a document that addresses common problems- is also widely available on the net.

Shell

The shell is interacting with the user. It is an interface between the user and the kernel. Even though there is
only one kernel running on the system. There could be several shells in action –one for each user who is logged
in.

Types of shells
Different people implemented the interpreter function of the shell in different ways:
 Bourne shell
 C shell
 Korn shell
Bourne shell:
The Bourne shell or sh, was the default UNIX shell of UNIX version 7, and replaced the Thompson
shell, whose executable file had the same name sh. It was developed by Stephen Bourne of AT&T Bell
Laboratories.
The binary programs of the Bourne shell or a compatible program is located at /bin/sh on most UNIX
system. Its command interpreter contained all the features that are commonly considered to procedure structured
programs.

C shell:
The C shell is a UNIX shell was created by Bill Joy while a graduate student at university California at
Berkeley.
It has two advantages over the Bourne shell
 It allows aliasing of commands. i.e you can decide what name you want to call a command by. This
provides very useful when lengthy commands which are used time & again are renamed by user.
Instead of typing the entire command you can simply use the short alias at the command line.

DON BOSCO CHITRADURGA Page 9


UNIX 2023
 If you want to save more on the typing work, c shell has a command history feature. This is the second
benefit that comes with c shell. Previously typed commands can be recalled. Since c shell keeps track
of all commands issued at the command line

Korn shell
The korn shell (ksh) is a UNIX shell was developed by David Korn ( AT &T Bell lab). Ksh is
backwards compatible with Bourne shell and includes many features of c shell as well as a command history.
The main advantage of ksh over the traditional UNIX shell is in its uses a programming language.

POSIX AND THE SINGLE UNIX SPECIFICATION


Dennis Ritchie’s decision to rewrite UNIX in C didn’t quite make UNIX very portable. UNIX
fragmentation and the absence of a single conforming standard adversely affected the development of portable
applications. First, AT&T created the System V Interface Definition (SVID). Later, X/ Open (now The Open
Group), a consortium of vendors and users, created the X/Open Portability Guide (XPG). Products conforming
to this specification were branded UNIX95, UNIX98 OR UNIX03 depending on the version of the specification.

Yet another group of standards, the Portable Operating System Interface for Computer
Environments (POSIX), were developed at the behest of the Institution of Electrical and Electronics Engineers
(IEEE). POSIX refers to operating systems in general, but was based on UNIX. Two of the most cited standards
from the POSIX family are known as POSIX.1 and POSIX.2. POSIX.1 specifies the C application program
interface—the system calls. POSIX.2 deals with the shell and utilities.

In 2001, joint initiative of X/Open and IEEE resulted in the unification of the two standards. This is the Single
UNIX Specification, version 3 (SU SV3). The “write once, adopt everywhere” approach to this development
means that once software has been developed on any POSIX-complaint UNIX system, it can be easily ported to
another POSIX-complaint UNIX machine with minimum modifications.

Questions

2 marks
1. Define unix operating system?
2. Name the variant of the unix?
3. When was unix developed?
4. Who was developed unix?
5. Define operating system?
6. Which command use to display current date?
7. Which command use to clear the screen?
8. Which command use to display current month and year?
9. Which command use to find the current user?
10. Which command use to display the current process?
11. Which command use to listing files?
12. Which command use to counting files, lines and characters?
13. Which symbol use to combine to display multiple commands?
14. Which command use to exit the current session?
15. What is kernel?
16. What is shell?
17. What is the function of the ‘|’ (pipeline)?
18. What is the function of the ‘>’?
19. What are the types of shell?
20. Expand POSIX
21. Expand SVID
22. Who developed Bourne shell?
23. Who developed C shell?
24. Who developed korn shell?

5 marks
1. What are the features of operating system?
2. Difference between UNIX and DOS?
3. Difference between UNIX and WINDOWS?

DON BOSCO CHITRADURGA Page 10


UNIX 2023
4. Explain the history of UNIX briefly
5. Explain the features of UNIX operating system?

10 marks
1. Explain the functions of following commands
a. cal
b. date
c. tput
d. ls
e. wc
f. who
g. exit
h. >
i. ls *
j. | (pipeline)

2. Explain the Architecture UNIX operating system?

DON BOSCO CHITRADURGA Page 11


UNIX 2023

2. Understanding the Unix Command


Locating Commands:
The UNIX system is command-based. i.e, things happen because of the commands that you key in.
UNIX commands are seldom more than four characters long. All UNIX commands are single words like ls, cat,
who etc. These names are all in lowercase.
For instance, if you enter of LS instead of ls, this is how the system will respond:

$ LS
bash: LS : command not found

This message from the shell. There is obviously no command named LS on the UNIX system. This
seems to suggest that there’s “predetermined” list of such commands that the shell first searches before it
flashes the message above.
These commands are essentially files containing programs, mainly written in C. Files are stored in
directories. For instances: the ls command is a file found in directory /bin:

Note: cases are distinct and case sensitive.

The easiest way of knowing the location of an executable program is to use the type command:

$ type ls Won’t work in C Shell


ls is /bin/ls

when you execute the ls command, the shell locates this file in the /bin directory and makes arrangements to
execute it.

a. The PATH
The sequence of directories that the shell searches to look for a specified in its own PATH variable.
Use echo to evaluate this variable and you will see a directory list separated by colons:

$ echo $ PATH
/bin:/usr/bin:/usr/local/bin:/usr/ccs/bin:/usr/local/jave/bin:.

There are six directories in this colon-separated list. To consider the second one, /usr/bin represents a
hierarchy of three directory names. The first / indicates the top-most directory called root. So usr below the root
directory and bin below the usr.
When you issue a command, the shell searches this list in the sequence specified to locate and execute it. Note
that this list also includes the current directory indicated by a singular dot at the end.

Internal and External Commands


Since ls is a program or file having an independent existence in the /bin directory, it is branded as an
external command.
Most of the commands are external in nature, but there are some which are not really found anywhere,
and some which are normally are not executed even if they are in one of the directories specified by PATH.
Take for instance the echo command:

$ type echo
echo is a shell built-in

echo is not an external command in the sense that, when you type echo, the shell won’t look in its
PATH to locate it. rather, it will execute it from its own set of built-in commands that are not stored in separate
files. These built-in commands are known as Internal Commands.

Command Structure
UNIX is well-known, you must know the syntax of the important commands. You have already
encountered commands that were used with multiple words (like tput clear, cat list etc). The first word is
actually the command; the additional words are called arguments.

DON BOSCO CHITRADURGA Page 12


UNIX 2023
Commands and arguments have to be separated by spaces or tabs to enable the system to interpret them
as words. You can use any number of them to separate the words that form a command. A contiguous string of
spaces and tabs together form what is known as white space. Where the system permits the use of one
whitespace character to separate words, it generally permits several, as in this command:

Cat README

UNIX arguments range from the simple to the complex. They consist of options, expressions, instructions,
filenames, etc.

1. Options
There is a special type of argument that is mostly used with a – sign. For instances, when you use

ls –l note
-l is an argument to ls by definition, but more importantly, it is a special argument known as an option.
A option is normally preceded by a minus (-) sign to distinguish it from filenames. There must not be
any whitespaces between – and l.
Options are also arguments, but given a special name because their list is predetermined. When we use echo
hello dolly, hello and dolly are also arguments, but they are not predetermined.

If you use a command with a wrong option, the shell locates the command all right, but the command this time
finds the option to be wrong:

$ ls –z note
ls: illegal option -–z

The above message has been generated by the command, and not by the shell. ls does have large number of
option (over 20), but –z is not one of them.

ls –l –a –t

you might as well use

ls –lat

to obtain the same output. This facility reduces your typing load, which becomes significant when you use
command with several options.

2. Filename as Arguments
Many UNIX commands use a filename as arguments so the command can take input from the file. If a
command uses a filename as argument at all, it will generally be its last argument –and after all options. It is
also quite common to see many commands working with multiple filenames as arguments:

ls –lat chap01 chap02 chap03


cp chap01 chap02 progs cp copies files
rm chap01 chap02 rm removes files

The command with its arguments and options is known as the command line. This line can be considered
complete only after the user has hit [enter]. The complete line is then fed to the shell as its input for
interpretation and execution.

3. Exceptions
There are exceptions to the general syntax of commands. There are commands (pwd) that don’t accept any
arguments, and some (who) that may or may not be specified with arguments. The ls command can run without
arguments (ls), with only options (ls –l), with only filenames (ls chap01 chap02), or using a combination of both

DON BOSCO CHITRADURGA Page 13


UNIX 2023
(ls –la chap01 chap02). The word option turns out to be a misnomer in some instances; some commands
compulsorily have to use one (cut).

Flexibility of Command Usage

UNIX provides flexibility in using the commands.

1. Combining Commands
Instead of executing commands on separate lines, where each command is processed and executed
before the next could be entered, UNIX allows you to specify more than one command in the single command
line. Each command has to be separated from the other by a ; (semicolon).

wc note ; ls –l note

You can even group several commands together so that their combined output is redirected to a file.

(wc note ; ls –l note) > newlist

When a command line contains a semicolon, the shell understands that the command on each side of it needs to
be processed separately. Here the ; is known as a metacharacter.

Note: When a command overflows into the next line or needs to be split into multiple lines, just press
enter, so that the secondary prompt (normally >) is displayed and you can enter the remaining part of the
command on the next line.

2. Entering a Command before previous command has finished


You need not have to wait for the previous command to finish before you can enter the next command.
Subsequent commands entered at the keyboard are stored in a buffer (a temporary storage in memory) that is
maintained by the kernel for all keyboard input. The next command will be passed on to the shell for
interpretation after the previous command has completed its execution.

man: Browsing The Manual Pages Online

UNIX commands are rather cryptic. When you don’t remember what options are supported by a
command or what its syntax is, you can always view man (short for manual) pages to get online help. The man
command displays online documentation of a specified command.

A pager is a program that displays one screenful information and pauses for the user to view the
contents. The user can make use of internal commands of the pager to scroll up and scroll down the information.
The two popular pagers are more and less.
more is the Berkeley’s pager, which is a superior alternative to original pg command.
less is the standard pager used on Linux systems. less if modeled after a popular editor called vi and is
more powerful than more as it provides vi-like navigational and search facilities. We can use pagers with
commands like ls | more. The man command is configured to work with a pager.

a. Navigation and Search


The navigation commands are numerous and often vary across UNIX implementations. For the time being,
you should know these two commands which should work on all systems:

 f or spacebar. Which advances the display by one screen of text at a time


 b, which moves back one search

DON BOSCO CHITRADURGA Page 14


UNIX 2023
Understanding The man Documentation
The man documentation is organized in eight (08) sections. Later enhancements have added
subsections like 1C, 1M, 3N etc.) References to other sections are reflected as SEE ALSO section of a man
page.
When you use man command, it starts searching the manuals starting from section 1. If it locates a
keyword in one section, it won’t continue the search, even if the keyword occurs in another section. However,
we can provide the section number additionally as argument for man command.
For example, passwd appears in section 1 and section 4. If we want to get documentation of passwd in section 4,
we use,
$ man 4 passwd OR $ man –s4 passwd (on Solaris)

1. Understanding a man Page


A typical man page for wc command is shown below:
A man page is divided into a number of compulsory and optional sections. Every command doesn’t need all
sections, but the first three (NAME, SYNOPSIS and DESCRIPTION) are generally seen in all man pages.
NAME presents a one-line introduction of the command. SYNOPSIS shows the syntax used by the command
and DESCRIPTION provides a detailed description.
The SYNOPSIS follows certain conventions and rules:
 If a command argument is enclosed in rectangular brackets, then it is optional; otherwise, the argument
is required.
 The ellipsis (a set if three dots) implies that there can be more instances of the preceding word.
 The | means that only one of the options shows on either side of the pipe can be used.

All the options used by the command are listed in OPTIONS section. There is a separate section named EXIT
STATUS which lists possible error conditions and their numeric representation.
Note: You can use man command to view its own documentation ($ man man). You can also set the pager to
use with man ($ PAGER=less ; export PAGER). To understand which pager is being used by man, use $ echo
$PAGER.

DON BOSCO CHITRADURGA Page 15


UNIX 2023

Further Help with man –k, apropos and whatis


man –k: Searches a summary database and prints one-line description of the command.

Example:

$ man –k awk
awk awk(1) - pattern scanning and processing language
nawk nawk(1) - pattern scanning and processing language

apropos: aprops lists the commands and files associated with a keyword.

Example:

$ apropos FTP

ftp ftp(1) - file transfer program


ftpd in.ftpd(1m) - file transfer protocol server
ftpusers ftpusers(4) - file listing users to be disallowed ftp login privileges

DON BOSCO CHITRADURGA Page 16


UNIX 2023
whatis: lists one-liners for a command.

Example:

$ whatis cp
cp cp(1) - copy files

When Things Go Wrong


Terminals and keyboards have no uniform behavioral pattern. Terminal settings directly impact the
keyboard operation. If you observe a different behavior from that expected, when you press certain keystrokes,
it means that the terminal settings are different. In such cases, you should know which keys to press to get the
required behavior. The following table lists keyboard commands to try when things go wrong.

Keystroke or Function
command
[Ctrl-h] Erases text

[Ctrl-c] or Interrupts a command


Delete
[Ctrl-d] Terminates login session or a program that expects its input from
keyboard
[Ctrl-s] Stops scrolling of screen output and locks keyboard

[Ctrl-q] Resumes scrolling of screen output and unlocks keyboard

[Ctrl-u] Kills command line without executing it

[Ctrl-\] Kills running program but creates a core file containing the memory
image of the program
[Ctrl-z] Suspends process and returns shell prompt; use fg to resume job

[Ctrl-j] Alternative to [Enter]

[Ctrl-m] Alternative to [Enter]

stty sane Restores terminal to normal status

General Purpose Utilities:


The best way to start acquiring knowledge of the UNIX command set is try your hand at some of the
general-purpose of the system. These commands have diverse functions, but can be broadly divided into two
categories. Some of them acts as handy accessories that can perform calculators or handle your mail.

1. cal : The Calendar


You can invoke the cal command to see the calendar of any specific month or a complete year.
Syntax drawn from solarise man page.
cal [ [month] year]

cal can be used without argument in which case it display the calendar of the current month:
$ cal

March 2023
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26
27 28 29 30 31

DON BOSCO CHITRADURGA Page 17


UNIX 2023

the syntax also tells us that when cal is used with argument, the month is optional but the year is not.

To see the calendar for the month of Mar 2023, you need two arguments:
$ cal 03 2023

March 2023
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26
27 28 29 30 31

You can’t hold the calendar of a year in a single screen page. It scrolls off too rapidly before you can use [ctrl-s]
to make it pause. To make cal pause in the same way man pauses, use cal with pager [more or less] using
symbol to connect them.

$ cal 2023 | more


The | symbol connects two commands, where more takes input from the cal command.

2. date : Display the System Date


you can display the current date with date command, which shows the date and time to the nearest second:

$ date
Mon Mar 20 09:22:40 IST 2023

The command can also be used with suitable format specifies as arguments. Each format is preceded by the +
symbol, followed by the % operator.

You can print only the month using the format +%m:
$ date +%m
03

Or the month name:

$ date +%h
Mar

Or you can combine them in one command:

$ date +”%h %m”


Jan 01

There are many other format specifiers and the useful ones are listed below:
d – The day of the month (1 to 31)
y – The last two digits of the year
m – Month in number.
h – Name of the month.
H, M, S – The Hour, minute and second, respectively
D -- The date in the format mm/dd/yy
T -- The time in the format hh:mm:ss

When you use multiple specifiers, you must enclose them within quotes, and use a single + symbol before it.

3. echo : Displaying A Message


We have used it in two ways:

DON BOSCO CHITRADURGA Page 18


UNIX 2023
 To display a message (like echo sun solaris)
 To evaluate shell variables (like echo $SHELL)

An escape sequence is generally a two character-string beginning with a \ (back slash), For instances: \c is an
escape sequence. When this escape sequence is placed at the end of a string used as an argument to echo. The
command interprets the sequence as a directive to place the cursor and prompt in the same line that display the
output:

$ echo “Enter the file name : \c”


Enter file name : $ _

Here are two commonly used escape sequences are:


\ t – A tab which pushes text to the right by eight character position.
\ n – A new line which create the effect of the pressing.

echo interprets a number as octal when it is preceded by ‘\0’.


For instances: which result in the sounding of a beep, has octal value 7. (i. e \07), you can use this values as an
argument to echo. But only after pressing with \0:

$ echo ‘ \07’ double quotes will also do


………beep heard……….

Escape sequence used by echo and printf


Escape sequence significance
\a -- Bell
\b -- Backspace
\c -- No Newline (cursor in the same line)
\f -- Form Feed
\n -- New line
\r -- Carriage return
\t -- tab
\v -- vertical tab
\\ -- back slash
\0n -- ASCII character represented by the octal value n

4. printf : An Alternative To echo


The printf command is available on most modern UNIX systems and is the one you should use instead of echo.

$ printf “No filename entered \n”


No filename entered \n
$_

printf is also accept the all escape sequences used by echo. But unlike echo, it does not automatically insert
newline unless the \n is used explicitly.

$ printf “My current shell is %s\n” $SHELL No comma before $


My current shell is /usr/bin/bash

printf uses many of the formats used by C ‘s printf function. Here are some of the commonly used ones:

%s -- string
%30s -- as above but printed in a space 30 characters wide
% d -- decimal integer
%6d -- as above but printed in a space 6 characters wide
%o -- octal integer
%x -- Hexadecimal integer
%f -- Floating point number

DON BOSCO CHITRADURGA Page 19


UNIX 2023
5. bc : The Calculator
UNIX provides the two types of calculators – the graphical object ( xcalc command) that looks like
one, and the text –based bc command.

When you invoke bc without argument, the cursor keeps on blinking and nothing seems to happen,
‘bc’ belongs to a family of commands that expect input from the key board when used without arguments. Key
in following arithmetic expression and then use [ctrl-d] to quit bc:

$ bc
12 + 5
17 value displayed after computation
[Ctrl-d] The eof (end of) harecter
$_

 bc shows the output of the computation in the next line start bc again and then make multiple
calculations in the same line, using the ; as delimiter. The output of each computation is shown in
separate line:

12 * 12 ; 2 ^ 32
144
4294967296

 bc performs only integer computation and truncates the decimal portion. This shows up clearly when
you divide two numbers:

9/5
1 decimal portion truncated

 To enable floating-point computation, you have to set scale to the number of digit of precession:

scale = 2
17/7
2.42

 bc is quite useful in converting number from one base to another. Set ibase (input base) to 2.

ibase =2
11001010
202
203

 The reverse is also possible. This time with obase:

obase = 2
14
1110

6. script : Recording Your Session


This command virtually unknown to many UNIX users, let’s you “record” your login session in a file. This
command is not included in POSIX. But you’ll find it useful to store in a file all key stroke as well as output and
error message.

You can later view the file, if you are doing some important work and wish to keep a log of all your activities,
you should invoke this command immediately after you log in:

$ script
Script started, file is typescript

DON BOSCO CHITRADURGA Page 20


UNIX 2023
$_

The prompt returns and all your keystrokes that you now enter here get recorded in the file typescript. After
your recording is over, you can terminate the sessions by entering exit:

$ exit or use [ctrl-d]


Script done, file is typescript
$_

You can view this file with the cat command. script overwrites any previous typescript the may exist. If you
want to append to it, or want to use a different log file, you can consider using these arguments:

script –a Append to exiting file typescript


script logfile logs activities to file logfile

7. EMAIL BASICS
A electronic mail is one of the First application that UNIX users are familiar with. Email is fast and cheap and
can be used today to exchange graphics, sounds and video files.
An email message never appears on your terminal the moment it is received. It is deposited in your mail-box.
Even you are not logged in. the shell regular checks this mailbox and when it detects the arrival of new mail. It
issues a message similar to this:

You have new mail in /var/mail/ kumar

Here kumar is the user

You can see this message either when you log in or after a program run by you completes execution. It’s good
practice to see incoming mail immediately upon arrival rather than defer it for future viewing. After viewing it,
you can do these things with it:

 reply to the sender and all recipients


 forward it to others
 save it in a mailbox folder or separate file
 delete it
 print it
 add the address of the sender and all recipients to the address book
 call up a helper application to view it if it is not in plain text format

7.1 Mail Addressing Scheme


A recipient identified by her email address. The addressing scheme in the early days simply used the
recipient’s username as the email address. However, in a single network, the email address also uses the
machine name as a component. Here are the two forms:

mailx henry henry on same host henry on same host


mailx henry@saturn henry on networked host Saturn. Henry on networked host saturn

The second example uses a combination of the username and machine name (called hostname), this email
address is unique in a network. A similar form is also used in internet addressing except that the Internet uses
domain names (called FQDN) instead of simple hostnames:

mailx henry@heavens.com email address unique on the internet

8. mailx: THE UNIVERSAL MAILER


mailx is a character-based mail agent that can do that. mailx finds place in the POSIX specification, which no
longer required UNIX systems to support the earlier mail agent of all – the mail command.

DON BOSCO CHITRADURGA Page 21


UNIX 2023
There are two ways of invoking mailx – in the sending and receiving modes. In the sending mode, mailx is used
with the email address of the recipient as argument. In the receiving mode, you generally use it without
arguments to handle your received mail.

8.1 Sending Mail


In this sending mode, mailx turns interactive, prompting for the subject first. You have to key it in
before entering the message body. Use the standard input to key in your input and then use [ctrl-d]. This is how
user henry sends mail to Charlie:

$ mailx charlie charlie is on same host


Subject: New system
The new system will start functioning from next month.
Convert your files by next week – henry
[ctrl-d] some system use dot here
EOT system indicates end of text

Sending Mail Noninteractively : since we often need to send mail from a shell script, we can use a shell feature
called redirection to take the message body from a file and the – s option to specify the subject:

mailx –s “New System” charlie < message.txt


mailx can be used to send copies using the –c option. Multiple recipients should be enclosed in quotes:

mailx –s “New System” –c “jpm,sumit” charlie < message.txt


This command sends a message to chralie with copies to jpm and sumit.

8.2 Receiving Mail


All incoming mail is appended to the mailbox. This is a text file named after the user-id of the
recipient. UNIX systems maintain the mailbox in a directory which is usually /var/mail. Charlie’s mail is
appended to /var/mail/Charlie. By default, mailx reads this file for viewing received mail.

Referring to the message sent by henry, the shell on Charlie’s machine regularly checks his mailbox to
determine the receipt of new mail. If charlie is currently running a program, the shell waits for program
execution to finish before flashing the following message:

You have new mail in /var/mail/Charlie

When Charlie logs in, he may also see this message. He now has to invoke the mailx command in the receiving
mode to see the mail henry has sent him. The system first displays the header and some credentials of all
incoming mail that’s still held in the mailbox:

$ mailx
mailx version 5.0 wed jan 22 16:00:40 PST 2015 Type ? for help.
“/var/mail/Charlie”: 5 messages 2 new 5 unread

8.3 Mailx Internal Commands


Like any other commands in unix the mailx command supports a number of internal commands. Enter
? or help at this prompt to see the entire list.
 Replying a mail: the r command enables the recipient to reply when the sent message is on display on
the terminal.
 Saving message: with w command, you can save one or more messages in separate files.

Following are the Internal commands used by mailx:

Command Action
+ Prints next message
- Prints previous message
N Prints message numbered N
h Prints headers of all messages
dN Deletes message N

DON BOSCO CHITRADURGA Page 22


UNIX 2023
uN Undeletes message N
s flname Saves current message with headers in flname
w flname Saves current message without headers in flname
m user Forward mail to user
rN Replies to sender of message N
q Quits mailx program
! cmd Runs UNIX command cmd

9. passwd : CHANGING YOUR PASSWORD


If your account doesn’t have a password or has one that is already known to others, you should change it
immediately. This is done with the passwd command:

$ passwd
Passwd :changing password for kumar
Enter login password: ****** ASK FOR OLD PASSWORD
New password: *******
Re-enter new password: *******
passwd (SYSTEM) : passwd successfully changed for kumar

passwd expects you to respond three times. First, it promotes for the old password. Next it checks whether you
have entered a valid password and if you have, it then promotes for the new password. Enter the new password
using the password naming rules applicable to your system. Finally passwd asks you to reenter the new
password.

9.1 Password Framing Rules and Discipline


- Don’t choose a password similar to old one.
- Don’t use commonly used names like name of friend, relative pets etc. a system may check with its own
dictionary and throw out those passwords that are easily guessed.
- Use a mix of alphabetic or numeric characters.
- Don’t write down the password in an easily accessible document.
- Change the password regularly

10. who : WHO ARE THE USERS?

UNIX maintains an account of all users who are logged on to the system. It’s often good idea to know
their user-ids so you can mail them messages. The who command displays an informative listing of these users:

$ who
root console Aug 1 07:51 (:0)
kumar pts/10 Aug 1 07/56 (pc123.heavens.com)
sharma pts/6 Aug 1 02:10 (pc125.heavens.com)
project pts/8 Aug 1 02:16 (pc125.heavens.com)
sachin pts/14 Aug 1 08:36 (mercury.heavens.com)

The first column shows the usernames (or user-id) of five users currently working on the system. The
second column shows the device names of their respective terminals. These are actually the filenames associated
with the terminals. The third, fourth and fifth column show the date and time of logging in. the last column
shows the machine name where the user logged in.

UNIX commands to avoid cluttering the display with header information, this command does have a
header option (-H). this option prints the column headers, and when combined with the –u option, provide a
more detailed list:
$ who –Hu
NAME LINE TIME IDLE PID COMMENTS
root console Aug 1 07:51 0:48 11040 (:0)
kumar pts/10 Aug 1 07:56 0:33 11200 (pc123.heavens.com)
sachin pts/14 Aug 1 08:36 . 13678 (mercury.heavens.com)

DON BOSCO CHITRADURGA Page 23


UNIX 2023
Two users have logged out, so it seems. The first five columns are the same as before, but it’s the sixth one
(IDLE) that, a . (dot) against sachin shows that activity has occurred in the last one minute

before the command was invoked. Kumar seems to be idling for the last 33 minutes. The PID is the process-id, a
number that uniquely identifies a process.
One of the users shown in the first column is obviously the user who invoked the who command. To know that
specifically, use the arguments am and i with who:

$ who am i
Kumar pts/10 Aug 1 07:56 (pc123.heavens.com)

11. uname : KNOWING YOUR MACHINE’S CHARACTERISTIC


The uname command displays certain features of the operating system running on your machine. By
default, it simply displays the name of the operating system:
$ uname
SunOS
 The current Release ( -r) A UNIX command often varies across versions so much so that you’ll need
to use –r option to find out the version of your operating system:

$ uname –r
5.8

 The Machine Name (-n) If your machine is connected to a network, it must have a name (called Host
name). If your network is connected to the Internet, then this hostname is a component of your
machine’s domain name (a series of words separated by dots, like mercury.heavens.com). The – n
option tells you the hostname:

$ uname – n
mercury

12. tty : KNOWING YOUR TERMINAL


since UNIX treats even terminals as files, it’s reasonable to expect a command that tells you the
filename of the terminal you are using. It’s the tty(teletype) command, an obvious reference to the device that
has now become obsolete. The command is simple and no arguments:

$ tty
/dev/pts/10

13. stty : DISPLAYING AND SETTING TERMINAL CHARACTERISTICS


The stty command helps straighten these things out; it both display and changes setting.
stty uses a very large number of keywords. The –a (all) option displays the current settings.
$ stty –a

a. changing the setting:


 whether backspacing should erase character (echoe) If you have worked on a number of terminals,
you would have noticed that backspacing over a character sometimes removes it from sight and
sometimes doesn’t . This is decided by the keyword echoe. Since it is set here (no – prefix to it),
backspacing removes the character from display.
$ stty –echoe

 Entering a Password through a Shell Script (echo) This echo setting has to be manipulated to let
shell programs accept a password-like string that must not be displayed on the screen.
$ stty –echo

 Changing the Interrupt Key (intr) stty also sets the functions for some of the keys. For instance, if
you like to use [Ctrl-c] as the interrupt key instead of [Delete],

DON BOSCO CHITRADURGA Page 24


UNIX 2023
$ stty intr \^c

 Changing the End of File Key (eof ) When using mailx, you used [Ctrl-d] to terminate input. This eof
character is also selectable. Instead of [Ctrl-d], you can use [Ctrl-a] as the eof character:
$ stty eof \^a

 When Everything Else Fails (sane) stty also provides another argument to set the terminal
characteristics to values that will work on most terminals. Use the word sane a single argument to the
command:
$ stty sane

Questions

2 marks
1. Which command is used to know the sequence of directories list?
2. What are internal and external commands?
3. Name some examples of internal commands?
4. Name some examples of external commands?
5. Write the general syntax of command structure?
6. What is command structure?
7. What is “options”?
8. What is command line?
9. What is exception?
10. What is combining command with example?
11. What is manual page?
12. What is pager?
13. What is “man –k” command and write an example?
14. What is “aprops” command and write an example?
15. What is “whatis” command and write an example?
16. What is the shortcut key of interrupts a command?
17. What is the shortcut key of erase text?
18. What is the shortcut key of terminates login session?
19. What is the shortcut key of stops scrolling of screen output and locks keyboard?
20. What is the shortcut key of resumes scrolling of screen output and unlocks keyboard?
21. What is the shortcut key of kills command with existing it?
22. What is the shortcut key of alternative to enter key?
23. What is the shortcut key of suspend process and returns shell prompt?
24. What is the shortcut key of restores terminals to normal status?
25. What is cal command explain with an example?
26. What is date command explain with an example?
27. What is the function of % operator explain with an example?
28. What is d indicates in date command explain with an example?
29. What is y indicates in date command explain with an example?
30. What is m indicates in date command explain with an example?
31. What is h indicates in date command explain with an example?
32. What is D indicates in date command explain with an example?
33. What is T indicates in date command explain with an example?
34. What is echo command explain with an example?
35. What is escape sequence?
36. What is printf command explain with an example?
37. What is calculator command explain with an example?
38. What is mailx command explain with an example?
39. What is sending mail mode command explain with an example?
40. What is receiving mail mode command explain with an example?

DON BOSCO CHITRADURGA Page 25


UNIX 2023
41. What is password command explain with an example?
42. Which command is used to know the machine’s characteristic explain with an example?
43. Which command is used to know your terminals explain with an example?
44. Which command is used to display and setting terminal characteristics explain with an example?

5 marks
1. Explain the PATH command with example
2. Explain internal and external commands with example
3. Explain “Option” and “filename as arguments” with an example
4. Explain the concept of “exception” in unix
5. Briefly explain the flexibility of command usage
6. Explain Browsing the MAN page online
7. Explain the MAN documentation
8. Explain the following commands with an example
a. man –k
b. Aarops
c. whatis
9. Explain any 5 short cut keys commands
10. Explain the cal with syntax and with examples of various formats
11. Explain date and various formats briefly
12. Explain echo and their escape sequence various formats with example
13. Explain printf with an example
14. Explain calculator with example
15. Explain EMAIL basics
16. What are the internal command action used by mailx
17. Explain password and what are the password framing rules

10 marks
1. Explain the following terms
a. Internal and external commands
2. Explain Command structure (a. Options b. Filename as argument c. exception)
3. Explain the flexibility of command usage
4. Explain the man : browsing the manual page and man documentation
5. Write 10 shortcut keys of unix commands
6. Explain general purpose utilities

DON BOSCO CHITRADURGA Page 26


UNIX 2023
3.The File System:
THE FILE

The File is a container for storing information. UNIX treats directories and device as files as well. A directory is
simply a folder where you store filenames and other directories. All physical devices like the hard disk,
memory, CD-ROM, printer and modem are treated as files.
A simple description of the UNIX system is this:
“On a UNIX system, everything is a file; if something is not a file, it is a process.”
We have divided the files into three categories:
 Ordinary File: Also known as regular file. It contains only data as a stream of characters.
 Directory File: It’s commonly sais that a directory contains files and other directories, but strictly
speaking, it contains their names and a number associated with each name.
 Device File : All devices and peripherals are represented by files. To read or write a device, you have
to perform these operations on its associated file.

A. Ordinary (Regular) File


An ordinary file can be either a text file or a binary file. This is the most common file type.
 Text file
 Binary file

A text file contains only printable characters and you can view and edit them. All C and Java program
sources, shell scripts are text files. Every line of a text file is terminated with the newline character.

A binary file, on the other hand, contains both printable and nonprintable characters that cover the entire
ASCII range. The object code and executables that you produce by compiling C programs are binary files.
Sound and video files are also binary files.

B. Directory File
A directory contains no data, but keeps details of the files and subdirectories that it contains. A
directory file contains one entry for every file and subdirectory that it houses. Each entry has two components
namely, the filename and a unique identification number of the file or directory (called the inode number).
When you create or remove a file, the kernel automatically updates its corresponding directory by adding or
removing the entry (filename and inode number) associate d with the file.

C. Device File
Printing files, installing software from CD-ROMs or backing up of files to tape. All the operations on
the devices are performed by reading or writing the file representing the device. It is advantageous to treat
devices as files as some of the commands used to access an ordinary file can be used with device files as well.

Device filenames are found in a single directory structure, /dev. A device file is not really a stream of
characters. It is the attributes of the file that entirely govern the operation of the device. The kernel identifies a
device from its attributes and uses them to operate the device.

Filenames in UNIX (What’s in a FILENAME?)

On a UNIX system, a filename can consist of up to 255 characters. Files may or may not have extensions and
can consist of practically any ASCII character except the / and the Null character. You are permitted to use
control characters or other nonprintable characters in a filename. However, you should avoid using these
characters while naming a file.
It is recommended that only the following characters be used in filenames:
 Alphabets and numerals.
 The period (.), hyphen (-) and underscore (_).

UNIX imposes no restrictions on the extension. In all cases, it is the application that imposes that
restriction.
Eg;- A C Compiler expects C program filenames to end with .c, Oracle requires SQL scripts to have .sql
extension, and so forth. DOS/WINDOWS users must also keep these 2 points in mind:

DON BOSCO CHITRADURGA Page 27


UNIX 2023
 A file can have as many dots embedded in its name. A filename can also begin with or end with a dot.
 UNIX is case sensitive; cap01, Chap01 and CHAP01 are three different filenames that can coexist in
the same directory.

THE PARENT-CHILD RELATIONSHIP


All files in UNIX are “related” to one another. The file system in UNIX is a collection of all of these
related files such as Ordinary, Directory and Device files organized in a hierarchical structure. This system
also been adopted by DOS and Windows, and is visually represented in following fig.
The top is called root and is represented by a / [front slash]. root is actually a directory. it is used by
the system administrator to log in.
The root directory [/] has a number of subdirectories under it. These subdirectories in turn have more
subdirectories and other files under them. For example:- bin and usr are two subdirectories directly under /.
While a second bin and kumar are subdirectories under usr. Thus, home directory is the parent of kumar, while
/ is the parent of home, and grandparent of kumar. If you create a file login.sql under the kumar directory then
kumar will be the parent of this file.
It is also obvious that, in these parent-child relationships, the parent is always a directory. home and
kumar are both directories as they are both parents of at least one file or directory.

The HOME variable : THE HOME DIRECTORY


When you log onto the system, UNIX automatically places you in a directory called the home
directory. The shell variable HOME indicates the home directory of the user.

E.g., $ echo $HOME


/home/kumar
What you see above is an absolute pathname, which is a sequence of directory names starting from root (/).
The subsequent slashes are used to separate the directories.

pwd - print working directory : CHECKING YOUR CURRENT DIRECTORY


At any time you can determine where you are in the file system hierarchy with the pwd, print working
directory, command,
E.g. $ pwd

DON BOSCO CHITRADURGA Page 28


UNIX 2023
/home/frank/src

cd - change directory
You can change to a new directory with the cd, change directory, command. cd will accept both
absolute and relative path names.

Syntax

cd [directory]

Examples

cd changes to user's home directory


cd / changes directory to the system's root
cd .. goes up one directory level
cd ../.. goes up two directory levels
cd /full/path/name/from/root changes directory to absolute path named
(note the leading slash)
cd path/from/current/location changes directory to path relative to current
location (no leading slash)

mkdir - make a directory

You extend your home hierarchy by making sub-directories underneath it. This is done with the mkdir, make
directory, command. Again, you specify either the full or relative path of the directory.

Examples
mkdir patch Creates a directory patch under current directory
mkdir patch dbs doc Creates three directories under current directory
mkdir pis pis/progs pis/data Creates a directory tree with pis as a directory under
the current directory and progs and data as subdirectories under pis

Note the order of specifying arguments in example 3. The parent directory should be specified first, followed by
the subdirectories to be created under it.

The system may refuse to create a directory due to the following reasons:
1. The directory already exists.
2. There may be an ordinary file by the same name in the current directory.
3. The permissions set for the current directory don’t permit the creation of files and directories by the user.

rmdir - remove directory


A directory needs to be empty before you can remove it. If it’s not, you need to remove the files first. Also,
you can’t remove a directory if it is your present working directory; you must first change out of that directory.
You cannot remove a subdirectory unless you are placed in a directory which is hierarchically above the one
you have chosen to remove.

The rmdir (remove directory) command removes directory.


E.g.

rmdir patch Directory must be empty


rmdir pis pis/progs pis/data Shows error as pis is not empty. However rmdir
silently deletes the lower level subdirectories progs and data.

ABSOLUTE PATHNAMES
Many UNIX commands use file and directory names as arguments, which are presumed to exist in the
current directory.

DON BOSCO CHITRADURGA Page 29


UNIX 2023
If first character of a pathname is /, the file’s location must be determine with respect to root. Such a pathname
is called an absolute pathname. When you have more than one / in a pathname, for each such /, you have to
descend one level in the file system.

Eg: /home/kumar/login.sql

Here, kumar is one level below home, and two levels below root.
When you specify a file by using front slashes to demarcate the various levels, you have a mechanism of
identifying a file uniquely. No two files in a UNIX system can have identical absolute pathnames. You can have
two files with the same name, but in different directories; their pathnames will also be different.

a. Using the Absolute Pathname for a command


A UNIX command runs by executing its disk file. When you specify the date command, the system has to
locate the file date from a list of directories specified in the PATH variable, and then execute it. However, if you
know the location of a particular command, you can precede its name with the complete path. Since date resides
in /bin, you can also use the absolute pathnames:

$ /bin/date
Thu sep 1 09:30:43 IST 2015

RELATIVE PATHNAMES
You would have notice that in a previous example, we didn’t use an absolute pathname to move to the
directory progs. Nor did we use one as an argument to cat:
Cd progs
Cat login.sql
Here both progs and login.sql are presumed to exist in the current directory. Now, if progs also contains a
directory scripts under it, you still won’t need an absolute pathname to change to that directory:
Cd progs/ scripts
Here we have a pathname that has a /, but it is not an absolute pathname because it doesn’t begin with a /.

a. Using . and .. in Relative Pathnames


Navigation often becomes easier by using a common ancestor as reference. UNIX offers a shortcut—the
relative pathname –that uses either the current or parent directory as reference, and specifies the path relative
to it.
A relative pathname uses one of these cryptic symbols:
 . ( a single dot)--this represent the current directory
 .. (two dots)—this represents the parent directory

ls – LISTING DIRECTORY CONTENTS


The command to list your directories and files is ls. With options it can provide information about the
size, type of file, permissions, dates of file creation, change and access.

Syntax
ls [options] [argument]
ls options
ls has a large number of options.
 Output in Multiple Columns (-x) : When you have a several files, it’s better to display the filenames in
multiple columns. Use the –x option to produce a multicolumnar output.
$ ls -x

 Identifying Directories and Executables (-F) : To identify directories and executable files, the –F
option should be used. Combining this option with –x produces a multi columnar output.
$ ls -Fx

DON BOSCO CHITRADURGA Page 30


UNIX 2023
 Showing hidden Files Also (-a) : There are certain hidden files (filenames beginning with a dot), often
found in the home directory, that normally don’t show up in the listing. The –a (all) option lists all
hidden files as well:
$ ls -axF

 Listing Directory Contents :


$ ls –x helpdir progs
This time the contents of the directories are listed.

 Recursive Listing (-R) : The –R option lists all files and subdirectories in a directory tree.
$ ls –R

Options to ls

-a Lists all files, including those beginning with a dot (.).


-d Lists only names of directories, not the files in the directory
-F Indicates type of entry with a trailing symbol: executables with *, directories with / and
symbolic links with @
-R Recursive list
-u Sorts filenames by last access time
-t Sorts filenames by last modification time
-i Displays inode number
-l Long listing: lists the mode, link information, owner, size, last modification (time). If the file
is a symbolic link, an arrow (-->) precedes the pathname of the linked-to file.
The UNIX File System
The root directory has many subdirectories. The following table describes some of the subdirectories
contained under root.
Directory Content
/bin Common programs, shared by the system, the system administrator and the users.
/dev Contains references to all the CPU peripheral hardware, which are represented as files with
special properties.
/etc Most important system configuration files are in /etc, this directory contains data similar to
those in the Control Panel in Windows
/home Home directories of the common users.
/lib Library files, includes files for all kinds of programs needed by the system and the users.
/sbin Programs for use by the system and the system administrator.
/tmp Temporary space for use by the system, cleaned upon reboot, so don't use this for saving
any work!
/usr Programs, libraries, documentation etc. for all user-related programs.
/var

Questions
2marks
1. What is FILE?
2. What is ordinary file?
3. What is a directory file?
4. What is a device file?
5. What is binary file?
6. What is FILENAMES in UNIX?
7. What is absolute path
8. What is relative path
9. What is HOME variable?
10. Which command use to find the home directory?
11. What /(front slash) indicates?
12. Which command used to checking the current directory?
13. Which command used to changing the current directory?

DON BOSCO CHITRADURGA Page 31


UNIX 2023
14. Which command used to making the directory?
15. Which command used to remove the directory?
16. What is absolute path name?
17. What is relative path name?
18. What is .(dot) and ..(double dot) represents in a directory?
19. Which command used to listing a directory contents?
20. What –x indicates in a directory?
21. What –Fx indicates in a directory?
22. What –axF indicates in a directory?
23. What –R indicates in a directory?
24. What –x indicates in a directory?
25. What –a indicates in a directory?
26. What –d indicates in a directory?
27. What –F indicates in a directory?
28. What –u indicates in a directory?
29. What –t indicates in a directory?
30. What –i indicates in a directory?
31. What –l indicates in a directory?

5 marks
1. Explain the FILE and its types
2. Explain PARENT-CHILD relationship.
3. Explain absolute path name and relative path names
4. Explain listing directory contents.
5. What are all the common option for the directory? name any 5
6. Name the common option for the directory?
7. Explain UNIX file System [either can ask for 10 marks].

Handling Ordinary Files:

cat : DISPLAYING AND CREATING FILES

 cat is one of the most well-known commands of the UNIX system. It is mainly used to display the
contents of a small file on the terminal.
$ cat dept.lst
 cat also accepts more than one file as arguments.
cat chap01 chap02

a. cat Options (-v and –n)


Displaying Nonprinting Characters (-v): cat is normally used for displaying text files only.
Executables, when seen with cat, simply display junk. If you have nonprinting ASCII characters in your
input, you can use cat with the –v option to display these characters.

Numbering Lines (-n): The –n option numbers lines. C compilers indicate the line number where
errors are detected, and this numbering facility often helps a programmer in debugging programs.

b. Using cat to create a File


cat is also useful for creating a file. You should now know how to create small files. Enter the
command cat, followed by the > (the right chevron) character and the filename.
$ cat > foo
A > symbol fold owing the command means that the output goes to the filename following it. cat used
in this way represents a rudimentary editor.
[ctrl-d]
$_

DON BOSCO CHITRADURGA Page 32


UNIX 2023
When the command line is terminated with [enter], the prompt vanishes. cat now waits to take input
from the user. Enter the three lines, each followed by [enter]. Finally press [ctrl-d] to signify the end of input to
the system. This is the eof character used by UNIX systems and is shown in the stty output. When this character
is entered, the system understands that no further text input will be made. The file is written and the prompt
returned. To verify this, simply “cat” this file.

$ cat foo
A > symbol fold owing the command means that the output goes to the filename following it. cat used in this
way represents a rudimentary editor.
cat is a versatile command. it can be used to create, display, concatenate and append to files.

cp: COPYING A FILE

 The cp (copy) command copies a file or group of files.


 It creates an exact image of the file on disk with a different name.
 The syntax requires at least two filenames to be specified in the command line. When both are
ordinary files, the first is copied to the second:
cp chap01 unit1

 If the destination file (unit1) doesn’t exist, it will first be created before copying takes place. If not, it
will simply be overwritten without any warning from the system. Just check with the ls command
whether or not the file exists.
 If there is only one file to be copied, the destination can be either an ordinary or directory. You then
have the option of choosing your destination filename. The following example shows two ways of
copying a file to the progs directory:

cp chap01 progs / unit1 chap01 copied to unit1 under progs


cp chap01 progs chap01 retains its name under progs

 cp is often used with the shorthand, . (dot), to signify the current directory as the destination. For
instance, to copy the file .profile from / home / sharma to your current directory, you can use either of
the two commands:

cp / home / sharma / .profile .profile Destination is a file


cp / home / sharma / . Destination is the current directory

 cp can also be used to copy more than one file with a single invocation of the command. In that case,
the last filename must be a directory. For instance, to copy the files chap01, chap02 and chap03 to the
progs directory, you have to use cp:

cp chap01 chap02 chap03 progs

 The * to frame a pattern for matching more than one filename. If there were only three filenames in the
current directory having the common string chap, you can compress the above sequence using the * as
a suffix to chap:

cp chap* progs copies all files beginning with chap

a. cp options

Interactive Copying (-i):


 The –i (interactive) option warns the user before overwriting the destination file. If unit1 exists, cp
prompts for a response:

$ cp –i chap01 unit1
cp: overwrite unit1 (yes / no) ? y
A y at this prompt overwrites the file, any other response leaves it uncopied.

DON BOSCO CHITRADURGA Page 33


UNIX 2023

Copying Directory Structure (-R):


 Many UNIX commands are capable of recursive behavior.
 This means that the command can descend a directory and examine all files in its subdirectories.
 The cp –R command behaves recursively to copy an entire directory structure, say, progs to newprogs :
cp -R progs newprogs Here newprogs must not exist

rm: DELETING FILES

 The rm (remove) command deletes one or more files. It normally operates silently and should be used
with caution.
The following command deletes three files:

rm chap01 chap02 chap03 rm chap* could be dangerous to use!

 A file once deleted can’t be removed. rm won’t normally remove a directory, but it can remove files
from one. You can remove two chapters from the progs directory without having to “cd” to it.

rm progs / chap01 progs / chap02 or rm progs /chap0[12]

 You may sometimes need to delete all files in a directory as part of a cleanup operation. The *, when
used by itself, represents all files, and you can then use rm like this:

$ rm * all files gone!


$_

a. rm Options

Interactive Deletion (-i):


 Like in cp, the –i (interactive) option makes the command ask the user for confirmation before
removing each file:
$ rm –i chap01 chap02 chap03
rm: remove chap01 (yes / no)? ?y
rm: remove chap02 (yes / no)? ?n
rm: remove chap03 (yes / no)? [enter] no response –file not deleted
A y removes the file, any other response leaves the file undeleted.

Recursive Deletion (-r or –R):


 With the –r (or –R) option, rm performs a tree walk—a thorough recursive search for all
subdirectories and files within these subdirectories.
 At each stage, it deletes everything it finds. Rm won’t normally remove directories, but when used
with this option, it will. Therefore, when you invoke this command:
rm –r *
 You will delete all files in the current directory and all its subdirectories. If you don’t have a backup,
then these files will be lost forever.

Forcing Removal (-f ):


 rm prompts for removal if a file is write-protected.
 The –f option overrides this minor protection and forces removal. When you combine it with the –r
option, it could be the most risky thing to do :
rm -rf * Deletes everything in the current directory and below

mv: RENAMING FILES


 The mv command renames (moves) files. It has two distinct functions:
 It renames a file (or directory)
 It moves a group of files to a different directory
 mv doesn’t create a copy of the file; it merely renames it. No additional space is consumed on disk
during renaming. To rename the file chap01 to man01, you should use
mv chap01 man01

DON BOSCO CHITRADURGA Page 34


UNIX 2023
 If the destination file doesn’t exist, it will be created. mv simply replaces the filename in the existing
directory entry with the new name. By default, mv doesn’t prompt for overwriting the destination file if
it exists.
 A group of files can be moved to a directory. The following command moves three files to the progs
directory:
mv chap01 chap02 chap03 progs

 mv can also used to rename a directory, for instance, pis to perdir:


mv pis perdir

more : PAGING OUTPUT


 The man command displays its output a page at a time. This is possible because it sends its output to a
pager program. Linux also offers more but less is its standard pager.
 To view the file chap01, enter the command with the filename:

more chap01 press q to exit

 You will see the contents of chap01 on the screen, one page at a time. At the bottom of the screen,
you’ll also see the filename and percentage of that has been viewed:
--more--(17%)
 more has a couple of internal commands that don’t show up on the screen when you invoke them. q,
the command used to exit more, is an internal command.

a. Navigation

 Irrespective of version, more uses the space bar to scroll forward a page at a time. You can scroll by
small and large increments of lines or screens. To move forward one page, use f or the space bar and to
move back one page, use b

b. The Repeat Features

The Repeat Factor:


 Many navigation commands, including f and b, use a repeat factor.
 This is the term used in vi to prefix a number to a vi internal command. Use of the repeat factor as a
command prefix simply repeats the command that many times.
 This means you can use 10f for scrolling forward by 10 pages and 30b for scrolling back 30 pages.

Repeating The Last Command (.):


 more has a repeat command, the dot, that repeats the last command you used. If you scroll forward
with 10f, you can scroll another 10 pages by simply pressing a dot.

c. Searching for a pattern

 You can perform a search for a pattern with the / command followed by the string. For instance, to look
for the first while loop in your program, you will have to enter this:
/while press [enter] also
 You can repeat this search for viewing the next while loop section by pressing n, and you can do that
repeatedly until you have scanned the entire file. Move back with b to arrive at the first page.

d. Using more in a Pipeline

 The man syntax doesn’t indicate this, but we often use more to page the output of another command.
The ls output won’t fit on the screen if there are too many files, so the command has to be used like
this:
ls | more no filename with more
 we have a pipeline here of two commands where the output of one is used as the input of the other.
Table: Internal Commands of more and less

DON BOSCO CHITRADURGA Page 35


UNIX 2023
more less Action
spacebar or f spacebar or f or z one page forward
20f - 20 pages forward
b b one page back
15b - 15 pages back
[Enter] j or [Enter] one line forward
- k one line back
- p or 1G beginning of file
- G End of file
/ pat / pat searches forward for expression pat
n n repeats search forward
- ? pat searches back for expression pat
. (a dot) - repeats last command
v v starts up vi editor
!cmd !cmd executes UNIX command cmd
q q quit
h h view help

The lp subsystem: PRINTING A FILE


No user is allowed direct access to the printer. Instead, one has to spool (line up) a job along with others in a
print queue. Spooling ensures the orderly printing of jobs and relieves the user from the necessity of
administering the print resources. The spooling facility in System V is provided by the lp (line printing)
command.

The following lp command prints a single copy of the file rfc822.ps :

$ lp rfc822.ps
Rrequest id is prl-320 (1 file)
$_

Note that the prompt is returned immediately after the job is submitted.

lp notifies the request – id --a combination of the printer name (prl) and the job number (320) –which can later
be accessed with other commands. The hard copy of the file is often preceded by a title page mentioning the
username, request-id and date.

a. lp options

 lp accepts the above request because a default printer has been defined by the administrator. If it is not,
or if there is more than one printer in the system, you have to use the –d option with the printer name:
lp –dlaser chap01.ps can also provide space after –d

 The –t (title) option, followed by the title string, prints the title on the first page:
lp –t “First Chapter” chap01.ps

 After the file has been printed you can notify the user with the –m (mail) option. You can also print
multiple copies (-n):
lp –n3 –m chap01.ps prints three copies and mails user a message

b. Other Commands in the lp Subsystem

 The print queue is viewed with the lpstat command. By viewing this list, you can use the cancel
command to cancel any jobs submitted by you. cancel uses the reques-id or printer name as argument:

cancel laser cancels current job on printer laser


cancel prl-320 cancels job with request-id prl-320

DON BOSCO CHITRADURGA Page 36


UNIX 2023
file : KNOWING THE FILE TYPES

 UNIX provides the file command to determine the type of file, especially of an ordinary file. You can
use it with one or more filenames as arguments:

$ file archive.zip
Archive.zip: ZIP archive

 file correctly identifies the basic file types (regular, directory or device). For a regular file, it attempts
to classify it further. Using the * to signify all files, this is how file behaves on this system having
regular files of varying types:
$ file *

wc : COUNTING LINES, WORDS AND CHARACTERS

 UNIX features a universal word-counting program that also counts lines and characters. It takes one or
more filenames as arguments and displays a four-columnar output.
 Before you use wc on the file infile, just use cat to view its contents:

$ cat infile
I am the wc command
I count characters, words and lines
With options I can also make a selective count

 You can now use wc without options to make a “word count” of the data in the file.
$ wc infile
3 20 103 infile

 wc counts 3 lines, 20 words and 103 characters. The filename has also been shown in the fourth
column. The meaning of these terms should be clear:
 A line is any group of characters not containing a newline
 A word is a group of characters not containing a space, tab and newline.
 A character is the smallest unit of information, and includes a space, tab and newline

wc offers three options to make a specific count. The –l option counts only the number of lines, while the –w
and –c option count words and characters, respectively:

$ wc –l infile
3 infile number of lines

$ wc –w infile
20 infile number of words

$ wc –c infile
103 infile number of characters

When used with multiple filenames, wc produces a line for each file, as well as a total count.

$ wc chap01 chap02 chap03

od: DISPLAYING DATA IN OCTAL

 Many files contain non printing characters, and most UNIX commands don’t display them properly.
The file odfile contains some of these characters that don’t show up normally:

$ more odfile
White space includes a
The ^G character rings a bell
The ^L character skips a page

DON BOSCO CHITRADURGA Page 37


UNIX 2023
The apparently incompletes first line actually contains a tab. The od command makes these commands visible
by displaying the ASCII octal value of its input. The –b option displays this value for each character separately.

$ od –b odfile

0000000 127 150 151 164 145 040 163 160 141 143 145 040 151 156 143 154
0000020 165 144 145 163 040 141 040 011 012 124 150 145 040 007 040 143
-------output trimmed----------
Each line displays 16 bytes of data in octal, preceded by the offset (position) in the file of the first byte in the
line. In the absence of proper mapping it’s difficult to make sense of this output, but when the –b and –c
(character) options are combined, the output is:

$ od –bc odfile

0000000 127 150 151 164 145 040 163 160 141 143 145 040 151 156 143 154
Whitespaceiclu
0000020 165 144 145 163 040 141 040 011 012 124 150 145 040 007 040 143
u d e s a \t \n T h e 007 c
0000040 150 141 162 141 143 164 145 162 040 162 151 156 147 163 040 141
haracterringsa
0000060 040 142 145 154 154 012 124 150 145 040 014 040 143 150 141 162
b e l l \n T h e \f c h a r
----------

Each line is replaced with two. The octal representations are shown in the first line. The printable characters and
escape sequences are shown in second line.

Their various representations are:


 The tab character, [ctrl-i], is shown as \t and the octal value 011
 The bell character, [ctrl-d], is shown as 007. Some systems show it as \a
 The formfeed character, [ctrl-l], is shown as \f and 014
 The LF (linefeed or newline) character, [ctrl-j], is shown as \n and 012. Note that od makes the newline
character visible too.

cmp: COMPARING TWO FILES


 You may often need to know whether two files are identical so one of them can be deleted.
 There are three commands in the UNIX system that can tell you that. cmp command needs two
filenames as arguments:

$ cmp chap01 chap02


Chap01 chap02 differ: char 9, line1
 The two files are compared byte by byte, and the location of the first mismatch is echoed to the screen.
 If two files are identical, cmp displays no message, but simply returns the prompt. You can try it out
with two copies of the same file.

$ cmp chap01 chap02

comm: WHAT IS COMMON?


 Suppose you have two lists of people and you are asked to find out the names available in one and not
in the other, or even those common to both.
 comm is the command you need for this work. It requires two sorted files, and lists the differing entries
in different columns. Let’s try it on these two files:
$ cat file1 $ cat file2
c.k shukla anil aggarwal
chanchal singhvi barun sengupta
s.n dasgupta c.k shukla
sumit chakrobarty lalit chowdary
s.n dasgupta

DON BOSCO CHITRADURGA Page 38


UNIX 2023
Both files are sorted and have some differences. When you run comm, it displays a three-columnar output:

$ comm. file [12]


anil aggarwal
barun sengupta
c.k. shukla
chanchal singhvi
lalit chowdry
s.n dasgupta
sumit chakrobarty

The first column contains two lines unique to the first file, and the second column shows three lines unique to
the second file. The third column displays two lines common to both files.

diff : CONVERTING ONE FILE TO OTHER


 diff is the third command that can be used to display file differences. It also tells you which lines in one
file have to be changed to make the two files identical. When used with the same files, it produces a
detailed output:

$ diff file1 file2 or diff file[12]


0a1,2 Append after line 0 of first file
> anil aggarwal this line
> barun sengupta and this line
2c4 change line 2 of first file
< chanchal singhvi replacing this line
-- with
> lalit chowdary this line
4d5 delete line 4 of first file
< sumit chakrobarty containing this line

 diff uses certain special symbols and instructions to indicate the changes that are required to make two
files identical.
 Each instruction uses an address combined with an action that is applied to the first file.
 The instruction 0a1,2 means appending two lines after line 0, which become line1 and 2 in the second
file. 2c4 changes line 2 which is line 4 in the second file. 4d5 deleted line 4.

Dos2unix and unix2dos: CONVERING BETWEEN DOS AND UNIX


 Some UNIX systems feature has two utilities—dos2unix and unix2dos—for converting files between
DOS and UNIX. For instances,
 how we use dos2unix to convert this file foo to UNIX on a Solaris system:
dos2unix foo.dos foo
The output is written to foo.dos.

 Then how we use unix2dos to convert this file foo.dos to DOS


unix2dos foo foo.dos
The output is written to foo.dos.

COMPRESSING AND ARCHIVING FILES


Every UNIX system comes with some or all of the following compression and decompression utilities:
 gzip and gunzip (.gz)
 bzip2 and bunzip2 (.bz2)
 zip and unzip (.zip)

The extension acquired by the compressed filename is shown in parentheses. The degree of compression
that can be achieved depends on the type of file, its size and the compression program used. Large text files
compress more, but GIF and JPEG image files compress very little because they hold data in compressed form.

DON BOSCO CHITRADURGA Page 39


UNIX 2023
Apart from compressing, you’ll also need to group a set of files into a single file, called an archive. The tar
and zip commands can pack an entire directory structure into an archive. You can send this archive as a single
file, either using ftp or as an email attachment, to be used on a remote machine. An individual layer of
compression helps bring down the file size, the reason why tar is often used with gzip and bzip2 for creating a
compressed archive. zip handles both functions itself.

gzip and gunzip : COMPRESSING AND DECOMPRESSING FILES


 gzip is a very popular program, that works with one or more filenames. It provides the extension .gz to
the compressed filename and removes the original file.

For example: we run gzip on the file libc.html. we also note the file size, both before and after compression,
using wc –c which counts character.
$ wc –c libc.html
3875302 libc.html

$ gzip libc.html

$ wc –c libc.html gz
788096 libc.html.gz

a. gzip Options
Uncompressing a “gzipped” File (-d): To restore the original and uncompressed file, you have two options:
Use either gzip –d or gunzip with one or more filenames as arguments; the .gz extension is optional yet again:
gunzip libc.html.gz
gzip –d libc.html
gunzip libc.html.gz user_guide.ps.gz

Recursive Compression (-r): you can descend a directory structure and compress all files found in
subdirectories. You need the –r option, and the arguments to gzip must comprise at least one directory:

gzip –r progs

tar : THE ARCHIVAL PROGRAM

For creating a disk archive that contains a group of files or an entire directory structure, we need to use tar. The
command is taken up to back up files to tape, but in this section we need to know how the command is used to
create a disk archive. For this minimal use of tar we need to know these key options:

-c Create an archive
-x Extract files from archive
-t Display files in archive
-f arch Specify the archive arch

Only one of these key options can be used at a time.

a. Creating an Archive ( - C )

 To create an archive, we need to specify the name of the archive (with –f ), the copy or write operation
(-c) and the filenames as arguments.
 Additionally, we’ll use the –v (verbose) option to display the progress while tar works.
 This is how we create a file archive, archive.tar, from the two uncompressed files used previously:

$ tar -cvf archive.tar libc.html user_guide.ps


a libc.html 3785k - v (verbose) displays list
a user_guide.ps 364 a indicates append
By convention, we use the .tar extension, so you’ll remember to use the same tar command for extraction. We
created an archive containing two ordinary files, but tar also behaves recursively to back up one or more
directories. In the following example, tar fills the archive progs.tar with three directory structures:

DON BOSCO CHITRADURGA Page 40


UNIX 2023

tar –cvf progs.tar c_progs java_progs shell_scripts


Use the same tar command to extract files from this archive.

 Using gzip with tar If the created archive is very big, you may like to compress it with gzip:
 gzip archive.tar Archived and compressed

This creates a “tar-gzipped” file, archived.tar.gz.

b. Extracting Files from Archive (-x)

tar uses the –x option to extract files from an archive. You can use it right away on a .tar file, the one we just
used to archive three directories:

tar -xvf progs.tar Extracts the three directories

But to extract files from a .tar.gz file, you must first use gunzip to decompress the archive and then run tar:

$ gunzip archive.tar.gz Retrieves archive.tar


$ tar –xvf archive.tar Extracts files
x libc.html, 3875302 bytes, 7569 tape blocks x indicates extract
x user_guide.ps, 372267 bytes, 728 tape blocks

Selective extraction is also possible. Just follow the above command line with one or more filenames that have
to be extracted:
tar -xvf archive.tar user_guid.ps extracts only user_guide.ps

This extracts a single file from the archive.

c. Viewing the Archive

To view the contents of the archive, use the –t (table of contents) option. It doesn’t extract files. But simply
shows their attributes in a form that you’ll see more often later:

$ tar –tvf archive.tar


-rw-r- -r- - 102/10 3875302 aug 24 19:40 2015 libc.html
-rw-r- -r- - 102/10 372267 aug 24 19:482015 user_guide.ps

zip and unzip : COMPRESSING AND ARCHIVING TOGETHER


 zip generally doesn’t compress as much as bzip2 but it combines the compressing function of gzip
with the archival function of tar.
 So instead of using two commands to compress a directory structure, you can use only one—zip.
 zip requires the first argument to be the compressed filename; the remaining arguments are interpreted
as files and directories to be compressed.

$ zip archive.zip libc.html user_guide.ps


adding : libc.html (deflated 80%)
adding : user_gude.ps (deflated 66%)

Recursive Compression (-r): For Recursive behavior, zip uses the –r option. It descends the tree structure in the
same way tar does except that it also compresses files. You can easily compress your home directory in this
way:

cd ; zip -r sumit_home.zip . cd is same as cd $HOME


Using unzip : Files are restored with the unzip command, which in its simplest form, uses the compressed
filename as argument. unzip does a non-interactive restoration if it doesn’t overwrite any existing files:

$ unzip archive.zip

DON BOSCO CHITRADURGA Page 41


UNIX 2023
archive: archive.zip
inflating: libc.html
inflating: user_guide.ps

Viewing the Archive (-v) : You can view the compressed archive with the –v option. The list shows both the
compressed and decompressed size of each file in the archive along with the percentage of compression
achieved:

$ unzip -v archive.zip
Archive: archive.zip

Questions

2marks
1. which command used for displaying and creating file
2. what is the function of cat option –v and –n in displaying and creating file
3. what is the function of > in creating file
4. which command used to copying the file
5. What cp command?
6. What is –i and –R options in cp command?
7. Which command is used to removing a file?
8. What is rm command?
9. What is –r and –R options in rm command?
10. Which command is used to renaming a file?
11. What is mv command?
12. Which command is used to show the paging output
13. What is “more” in paging output
14. What is navigation?
15. What is repeat factor in paging output?
16. What is repeating the last command feature in paging output?
17. What is searching for a pattern in paging output?
18. What is pipeline in paging output?
19. Which command is used to PRINTING A FILE
20. What is lp command
21. Which command is used to KNOWING THE FILE TYPE
22. Which command is used to counting lines, words and character
23. Which command is used to DISPLAYNG DATA IN OCTAL
24.
5marks
1. Explain the command cat:DISPLAYING AND CREATING FILES
2. How to create a file? Explain with n example
3. Explain cp:COPYING A FILE? With an example.
4. Explain the options of cp commands
5. Explain rm:REMOVING FILE COMMAND? With an example.
6. Explain the options of rm commands
7. Explain mv:RENAMING A FILE COMMAND? With an example.
8. Explain more:PAGING OUTPUT? With an example.
9. Explain lp subsystem:PRINTING A FILE? With an example.
10. Explain lp command options
11. Explain file:KNOWING THE FILE TYPE? With an example.
12. Explain wc:COUNTING LINES, WORDS AND CHARECTERS? With an example.
13. Explani od: DISPLAYNG DATA IN OCTAL? With an example.

DON BOSCO CHITRADURGA Page 42


UNIX 2023
14. Explani cmp:COMPARING TWO FIELS? With an example.
15. Explani comm:WHAT IS COMMON? With an example.
16. Explani diff:CONVERTING ONE FILE TO ANOTHER? With an example.
17. Explain COMPRESSING ASND ARCHIVING FILE? With an example.
18. Explain gzip and gunzip:COMPRESSING ASND DECOMPRESSING FILES? With an
example.
19. Explain tar;THE ARCHIVAL PROGRAMM? With an example.
20. Explain zip and unzip: COMPRESSING ASND ARCHIVING FILE? With an example.

10marks
Note: any questions can be asked in combination of 2 or 3 questions as mentioned above 5 marks
for 10 marks.

4. Basic File Attributes:


The UNIX file system allows the user to access other files not belonging to them and without infringing on
security. A file has a number of attributes (properties) that are stored in the inode.

DON BOSCO CHITRADURGA Page 43


UNIX 2023

 ls –l to display file attributes (properties)


 Listing of a specific directory
 Ownership and group ownership
 Different file permissions

Listing File Attributes

ls command is used to obtain a list of all filenames in the current directory. The output in UNIX lingo
is often referred to as the listing. Sometimes we combine this option with other options for displaying other
attributes, or ordering the list in a different sequence. ls look up the file’s inode to fetch its attributes.

It lists seven attributes of all files in the current directory and they are:

 File type and Permissions: The file type and its permissions are associated with each file.
 Links: Links indicate the number of file names maintained by the system. This does not mean that
there are so many copies of the file.
 Ownership: File is created by the owner.
 Group ownership: Every user is attached to a group owner.
 File size : File size in bytes is displayed.
 Last Modification date and time: Last modification time is the next field. If you change only the
permissions or ownership of the file, the modification time remains unchanged.
 File name : In the last field, it displays the file name.

For example,
$ ls –l
total 72
-rw-r--r-- 1 kumar metal 19514 may 10 13:45 chap01
-rw-r--r-- 1 kumar metal 4174 may 10 15:01 chap02
-rw-rw-rw- 1 kumar metal 84 feb 12 12:30 dept.lst
-rw-r--r-- 1 kumar metal 9156 mar 12 1999 genie.sh
drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir
drwxr-xr-x 2 kumar metal 512 may 9 09:57 progs

Listing Directory Attributes : -d option

ls -d will not list all subdirectories in the current directory

For example,

$ ls –ld helpdir progs


drwxr-xr-x 2 kumar metal 512 may 9 10:31 helpdir
drwxr-xr-x 2 kumar metal 512 may 9 09:57 progs

Directories are easily identified in the listing by the first character of the first column, which here
shows a d. The significance of the attributes of a directory differs a good deal from an ordinary file. To see the
attributes of a directory rather than the files contained in it, use ls –ld with the directory name. Note that simply
using ls –d will not list all subdirectories in the current directory. Strange though it may seem, ls has no option
to list only directories.

File Ownership
When you create a file, you become its owner. Every owner is attached to a group owner. Several users
may belong to a single group, but the privileges of the group are set by the owner of the file and not by the
group members. When the system administrator creates a user account, he has to assign these parameters to the
user:

The user-id (UID) – both its name and numeric representation


The group-id (GID) – both its name and numeric representation

DON BOSCO CHITRADURGA Page 44


UNIX 2023

File Permissions
UNIX follows a three-tiered file protection system that determines a file’s access rights. It is displayed
in the following format:

Filetype owner (rwx) groupowner (rwx) others (rwx)

For Example:

-rwxr-xr-- 1 kumar metal 20500 may 10 19:21 chap02

rwx r-x r--


owner/user group owner others

The first group has all three permissions. The file is readable, writable and executable by the owner of
the file. The second group has a hyphen in the middle slot, which indicates the absence of write permission by
the group owner of the file. The third group has the write and execute bits absent. This set of permissions is
applicable to others.

You can set different permissions for the three categories of users – owner, group and others. It’s
important that you understand them because a little learning here can be a dangerous thing. Faulty file
permission is a sure recipe for disaster

Changing File Permissions:chmod

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 command, we can change
the file permissions and allow the owner to execute his file.
The command can be used in two ways:

 In a relative manner by specifying the changes to the current permissions


 In an absolute manner by specifying the final permissions

a. Relative Permissions
chmod only changes the permissions specified in the command line and leaves the other permissions
unchanged. Its syntax is:

chmod category operation permission filename(s)

chmod takes an expression as its argument which contains some letters and symbol:

 user category (user, group, others)


 operation to be performed (assign or remove a permission)
 type of permission (read, write, execute)

Category operation permission


u - user + assign r - read
g - group - remove w - write
o - others = absolute x - execute
a - all (ugo)

Let us discuss some examples:


Initially,
-rw-r--r-- 1 kumar metal 1906 sep 23:38 xstart
chmod u+x xstart
-rwxr--r-- 1 kumar metal 1906 sep 23:38 xstart

The command assigns (+) execute (x) permission to the user (u), other permissions remain unchanged.

DON BOSCO CHITRADURGA Page 45


UNIX 2023
chmod ugo+x xstart or
chmod a+x xstart or
chmod +x xstart

-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart

chmod accepts multiple file names in command line

chmod u+x note note1 note3

Let initially,
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 xstart

chmod go-r xstart

Then, it becomes
-rwx--x--x 1 kumar metal 1906 sep 23:38 xstart

b. 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:
 Read permission – 4 (octal 100)
 Write permission – 2 (octal 010)
 Execute permission – 1 (octal 001)

Binary Octal Permissions Significance


000 0 --- no permissions
001 1 --x execute only
010 2 -w- write only
011 3 -wx write and execute
100 4 r-- read only
101 5 r-x read and execute
110 6 rw- read and write
111 7 rwx read, write and execute

We have three categories and three permissions for each category, so three octal digits can describe a
file’s permissions completely. The most significant digit represents user and the least one represents others.
chmod can use this three-digit string as the expression.

Using relative permission, we have,

chmod a+rw xstart

Using absolute permission, we have,

chmod 666 xstart


chmod 644 xstart
chmod 761 xstart

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 change the file permissions. User can not
change other user’s file’s permissions. But the system administrator can do anything.

c. The Security Implications

DON BOSCO CHITRADURGA Page 46


UNIX 2023
Let the default permission for the file xstart is
-rw-r--r--
chmod u-rw, go-r xstart or
chmod 000 xstart

This is simply useless but still the user can delete this file
On the other hand,
chmod a+rwx xstart
chmod 777 xstart
-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

We can use chmod Recursively.


chmod -R a+x shell_scripts

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 * doesn’t 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)

A directory must never be writable by group and others

Example:
mkdir c_progs
ls –ld c_progs
drwxr-xr-x 2 kumar metal 512 may 9 09:57 c_progs

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.

Changing File Ownership


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

i. chown changing file owner and


ii. chgrp changing group owner

On BSD, only system administrator can use chown On other systems, only the owner can change both

chown
Changing ownership requires superuser permission, so use su command
ls -l note
-rwxr----x 1 kumar metal 347 may 10 20:30 note
chown sharma note; ls -l note
-rwxr----x 1 sharma metal 347 may 10 20:30 note

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

DON BOSCO CHITRADURGA Page 47


UNIX 2023
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 file’s group owner. No superuser permission is required.

ls –l dept.lst
-rw-r--r-- 1 kumar metal 139 jun 8 16:43 dept.lst

chgrp dba dept.lst; ls –l dept.lst


-rw-r--r-- 1 kumar dba 139 jun 8 16:43 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.

The vi Editor
To write and edit some programs and scripts, we require editors. UNIX provides vi editor for BSD
system – created by Bill Joy. Bram Moolenaar improved vi editor and called it as vim (vi improved) on Linux
OS.

VI BASICS
To add some text to a file, we invoke,
vi <filename>

In all probability, the file doesn’t exist, and vi presents you a full screen with the filename shown at the
bottom with the qualifier. The cursor is positioned at the top and all remaining lines of the screen show a ~.
They are non-existent lines. The last line is reserved for commands that you can enter to act on text. This line is
also used by the system to display messages. This is the command mode. This is the mode where you can pass
commands to act on text, using most of the keys of the keyboard. This is the default mode of the editor where
every key pressed is interpreted as a command to run on text. You will have to be in this mode to copy and
delete text
For, text editing, vi uses 24 out of 25 lines that are normally available in the terminal. To enter text,
you must switch to the input mode. First press the key i, and you are in this mode ready to input text.
Subsequent key depressions will then show up on the screen as text input.
After text entry is complete, the cursor is positioned on the last character of the last line. This is known
as current line and the character where the cursor is stationed is the current cursor position. This mode is used to
handle files and perform substitution. After the command is run, you are back to the default command mode. If
a word has been misspelled, use ctrl-w to erase the entire word.
Now press esc key to revert to command mode. Press it again and you will hear a beep. A beep in vi
indicates that a key has been pressed unnecessarily. Actually, the text entered has not been saved on disk but
exists in some temporary storage called a buffer. To save the entered text, you must switch to the execute mode
(the last line mode). Invoke the execute mode from the command mode by entering a: which shows up in the
last line.

The Modes Used By vi Editor


There are three modes used by the vi Editor.
 Command Mode: The default mode of the editor where every key pressed is interpreted as a
command to run on text. You will have to be in this mode to copy and delete text. Unnecessary
pressing of [esc] in this mode sounds a beep but also confirms that you are in this mode.
 Input Mode: Every key pressed after switching to this mode actually shows up as text. This mode is
invoked by pressing one of the keys i, a, r etc.
 ex Mode (or Last Line Mode): The mode used to handle files (like saving) and perform substitution.
Pressing a : in the command mode invokes this mode. You then enter an ex mode command followed
by [Enter]. After the command is run, you are back to the default command mode.

The Repeat Factor


vi provides repeat factor in command and input mode commands. Command mode command k moves
the cursor one line up. 10k moves cursor 10 lines up.

DON BOSCO CHITRADURGA Page 48


UNIX 2023
To undo whenever you make a mistake, press
Esc u
To clear the screen in command mode, press
ctrl-l
Don’t use (caps lock) - vi commands are case-sensitive
Avoid using the PC navigation keys

Input Mode – Entering and Replacing Text


We take up all the commands that let you enter the input mode from the command mode. When a key
of the input mode is pressed, it doesn’t appear on screen but subsequent key is depressions do.

We will consider the following commands:

 Insert and append ( i, a, I and A)


 Replace (r, R, s and S)
 Open a line (o and O)

Always keep in mind that after you have completed text entry using any of these commands (except r), you
must return to the command n=mode by pressing [Esc].

a. Inserting of Text ( i and a)

The simplest type of input is insertion of text. Just press


i Existing text will be shifted right

Pressing ‘i’ changes the mode from command to input mode. If the i command is invoked with the cursor
positioned on existing text, text on its right will be shifted further without being overwritten. The insertion of
text with i is shown in below, along with the position of the cursor.

Text insertion with i Text appending with a

There are other methods of inputting text. To append text to the right of the cursor position, we use

a, Existing text will also be shifted right

followed by the text you wish to key. After you have finished editing, press [Esc]. With i and a, you can append
several lines of text.

b. Insertion of Text at Line Extremes ( I and A)

I and A behave same as i and a, but at line extremes:


 I inserts text at the beginning of line.
 A appends text at end of line.

These two commands are suitable for converting code to comment lines in a C program.

DON BOSCO CHITRADURGA Page 49


UNIX 2023

Fig: Using I and A to create a comment line in a C program

c. Opening a New Line ( o and O)


In vi, you use o and O. To open a line below from anywhere in a line, simply press

o Opens new line below current line

This insert an empty line below the current line.

O also opens a line but above the current line. You are free to enter as much text as you choose, spanning
multiple lines if required. Press the [Esc] key after completing text input.

d. Replacing Text (r, s, R and S)

To change existing text, vi provides mainly four commands as shown in heading. To replace a single character
with another, you should use

r No [Esc] required

Followed by the character that replaces the one under the cursor. You can replace a single character
only in this way. Vi momentarily switches from command mode to input mode when r is pressed. It returns to
command mode as soon as the new character is entered. There’s no need to press [Esc] when using r and the
replacement character, since vi excepts a single character anyway.

When you want to replace the letter d with 10f in a printf statement in C, you need to replace one
character with three. In that case, press

s Replaces one character with many

To replace multiple characters, use a repeat factor. 3s replaces three with new text.

Use of s is shown below:

Fig: Replacing Text with s


R and S act in a similar manner compared to their lowercase ones except that they act on a larger group of
characters:
R Replaces all text on the right of the cursor position
S Replaces the entire line irrespective of cursor position

DON BOSCO CHITRADURGA Page 50


UNIX 2023
Use R is shown in fig:

Some of the input mode commands are:

COMMAND FUNCTION
i inserts text
a appends text
I inserts at beginning of line
A appends text at end of line
o opens line below
O opens line above
r replaces a single character
s replaces with a text
S replaces entire line

Saving Text and Quitting – The ex Mode


When you edit a file using vi, the original file is not distributed as such, but only a copy of it that is
placed in a buffer. From time to time, you should save your work by writing the buffer contents to disk to keep
the disk file current. When we talk of saving a file, we actually mean saving this buffer. You may also need to
quit vi after or without saving the buffer.

a. Saving You Work ( :w)


For extended sessions with vi, you must able to save the buffer and remain in the editor. From time to time,
you must use the :w command to write the buffer to disk. Enter a :, which appears on the last line of the screen,
then w and finally [Enter]:

:w [Enter]

“sometext”, 8 lines, 275 characters

You can now continue your editing work normally, but make sure that you execute this command regularly.

b. Saving and Quitting ( :x and :wq)


The previous command returns you to the command mode so you can continue editing. However, to save
and quit the editor, use the :x (exit) command instead:

: x [Enter]
“sometext:, 8 lines, 303 characters

You can also use : wq to save and quit the editor.

c. Aborting Editing (:q)


It’s also possible to abort the editing process and quit the editing mode without saving the buffer. The :q
(quit) command does that job:

:q [Enter]
$_

d. Writing Selected Lines

DON BOSCO CHITRADURGA Page 51


UNIX 2023
The :w command is an abbreviated way of executing the ex mode instruction :1, $w. w can be prefixed by
one or two address separated by a comma. The command

:10, 50w n2words.pl write 41 lines to another file

Saves lines 10 through 50 to the file n2words.pl. You can save a single line as well:

:5w n2words.pl write 5th line to another file

There are two symbols used with w that have special significance—the dot and $. The dot represents the current
line while $ represents the last line of the file. You can use them singly or in combination:

: .w tempfile saves current line (where cursor is positioned)


:$w tempfile saves last line
:.,$w tempfile saves current line through end

e. Escape to the UNIX Shell (:sh and [ctrl-z])


There are two ways; the first method is to use the ex mode command, :sh
:sh
$_

This returns a shell prompt. Execute cc or any UNIX command here and then return to the editor using [ctrl-d]
or exit.
If your shell supports job control, you can also suspend the current vi session. Just press [ctrl-z] and you will be
returned a shell prompt. Run your command and then use the fg command to return to the editor.

f. Recover from a Crash (:recover and –r)


The complaint usually also contains some advice regarding the salvage operation. You will be advised to
use either the ex mode command :recover, or vi –r foo to recover as much of foo as possible. After you have
done that, have a look at the buffer contents and satisfy yourself of the success of the damage control exercise. If
everything seems fine, save the buffer and remove the swap file if vi doesn’t do that on its own.

Some of the save and exit commands of the ex mode is:

Command Action
:W saves file and remains in editing mode
:x saves and quits editing mode
:wq saves and quits editing mode
:w <filename> save as
:w! <filename> save as, but overwrites existing file
:q quits editing mode
:q! quits editing mode by rejecting changes made
:sh escapes to UNIX shell
: recover recovers file from a crash

Navigation
A command mode command doesn’t show up on screen but simply performs a function.

a. To move the cursor in four directions (h, j, k and l)


vi provides the keys h, i, j and l to move the cursor in the four directions. These keys are placed adjacent to
one another in the middle row of the keyboard. Without a repeat factor, they move the cursor by one position.
Use these keys for moving the cursor vertically:

k moves cursor up
j moves cursor down
To move the cursor along a line, use these commands:
h moves cursor left
l moves cursor right

DON BOSCO CHITRADURGA Page 52


UNIX 2023

b. Word Navigation
Moving by one character is not always enough. You will often need to move faster along a line. vi
understands a word as a navigation unit which can be defined in two ways, depending on the key pressed. If
your cursor is a number of words away from your desired position, you can use the word-navigation commands
to go there directly.
There are three basic commands:
b moves back to beginning of word
e moves forward to end of word
w moves forward to beginning word

Example,
5b takes the cursor 5 words back
3w takes the cursor 3 words forward

c. Moving to Line Extremes


Moving to the beginning or end of a line is a common requirement.
To move to the first character of a line
0 or |
30| moves cursor to column 30

$ moves to the end of the current line

The use of these commands along with b, e, and w is allowed

d. Scrolling ( [Ctrl-f], [Ctrl-b], [Ctrl-d] and [Ctrl-u] )


Faster movement can be achieved by scrolling text in the window using the control keys. The two
commands for scrolling a page at a time are

ctrl-f scrolls forward


ctrl-b scrolls backward
10ctrl-f scroll 10 pages and navigate faster
ctrl-d scrolls half page forward
ctrl-u scrolls half page backward
The repeat factor can also be used here.

Fig: Navigation Along a Line


e. Absolute Movement (G)
The editor displays the total number of lines in the last line
Ctrl-g to know the current line number

DON BOSCO CHITRADURGA Page 53


UNIX 2023
40G goes to line number 40
1G goes to line number 1
G goes to end of file

Editing Text

The editing facilitates in vi are very elaborate and invoke the use of operators. They use operators, such as,
d delete
y yank (copy)

a. Deleting Text

x deletes a single character


dd delete entire line
yy copy entire line
6dd deletes the current line and five lines below

Fig: Deleting Lines with dd

b. Moving Text (p)


Moving text (p) puts the text at the new location.
p and P place text on right and left only when you delete parts of lines. But the same keys get associated
with “below” and “above” when you delete complete lines

c. Copying Text (y and p)


Copying text (y and p) is achieved as,
yy copies current line
10yy copies current line & 9 lines below

d. Joining Lines
J to join the current line and the line following it
4J joins following 3 lines with current line

Undoing Last Editing Instructions


In command mode, to undo the last change made, we use u

DON BOSCO CHITRADURGA Page 54


UNIX 2023
To discard all changes made to the current line, we use U

vim (LINUX) lets you undo and redo multiple editing instructions. u behaves differently here; repeated
use of this key progressively undoes your previous actions. You could even have the original file in front of you.
Further 10u reverses your last 10 editing actions. The function of U remains the same.
You may overshoot the desired mark when you keep u pressed, in which case use ctrl-r to redo your
undone actions. Further, undoing with 10u can be completely reversed with 10ctrl-r. The undoing limit is set by
the execute mode command: set undolevels=n, where n is set to 1000 by default.

Repeating the Last Command


The . (dot) command is used for repeating the last instruction in both editing and command mode
commands
For example:
2dd deletes 2 lines from current line and to repeat this operation, type. (dot)

Searching for a Pattern


/ search forward
? search backward
/printf : The search begins forward to position the cursor on the first instance of the word
?pattern : Searches backward for the most previous instance of the pattern

a. Repeating the Last Pattern Search


n repeats search in same direction of original search
n doesn’t necessarily repeat a search in the forward direction. The direction depends on the
search command used. If you used? printf to search in the reverse direction in the first place, then n
also follows the same direction. In that case, N will repeat the search in the forward direction, and not
n.

Search and repeat commands


Command Function
/pat searches forward for pattern pat
?pat searches backward for pattern pat
n repeats search in same direction along which previous search was made
N repeats search in direction opposite to that along which previous search was made

Substitution – search and replace


We can perform search and replace in execute mode using :s. Its syntax is,
:address/source_pattern/target_pattern/flags

:1,$s/director/member/g can also use % instead of 1,$


:1,50s/unsigned//g deletes unsigned everywhere in lines 1 to 50
:3,10s/director/member/g substitute lines 3 through 10
:.s/director/member/g only the current line
:$s/director/member/g only the last line

DON BOSCO CHITRADURGA Page 55


UNIX 2023
Interactive substitution: sometimes you may like to selectively replace a string. In that case, add the c parameter
as the flag at the end:

:1,$s/director/member/gc

Each line is selected in turn, followed by a sequence of carets in the next line, just below the pattern that
requires substitution. The cursor is positioned at the end of this caret sequence, waiting for your response.
The ex mode is also used for substitution. Both search and replace operations also use regular expressions for
matching multiple patterns.
The features of vi editor that have been highlighted so far are good enough for a beginner who should
not proceed any further before mastering most of them. There are many more functions that make vi a very
powerful editor. Can you copy three words or even the entire file using simple keystrokes? Can you copy or
move multiple sections of text from one file to another in a single file switch? How do you compile your C and
Java programs without leaving the editor? vi can do all this.

Questions:

2 marks
1. what is the function of the ls –l command?
2. Which command is used for listing a file attribute?
3. Define file attributes?
4. Which command is used for listing a directory attribute?
5. What is the function of the ls –d command?
6. What is FILE OWNERSHIP?
7. What is FILE PERMISSION?
8. What is “changing file permission”?
9. What is the function of “chmod” command?
10. What are the ways to change the file permission?
11. What is relative permission?
12. What is absolute permission?
13. What are +, - and = operators in changing file permission
14. What are the categories in changing file permission?
15. What are u, g and o in changing file permission?
16. What is r, w and x in changing file permission?
17. What is chown command?
18. What is chgrep command
19. What is vi editor?
20. What is command mode?
21. What is input mode?
22. What is execution mode?
23. What is repeat factor?
24.
5 marks
1. Explain the list of file attributes? With an example.
2. Explain the list directory attributes? With an example.
3. Explain FILE PERMISSION? With an example.
4. Explain chmod command? With an example
5. Explain the changing file permission? With an example
6. Explain “user, group and others” categories with suitable example
7. What are the operation modes in changing file permission mode?
8. Explain the operations of changing file permissions ? with an example
9. Give the permissions for the following user
-rw-r--r-- 1 kumar metal 1906 sep 23:38 xstart

DON BOSCO CHITRADURGA Page 56


UNIX 2023
a. Write permission for the group category
b. Execution permission to the other category
c. Remove read permission to user category
d. Execution permission to all group category
e. Remove read permission to all category
10. Explain octal significances of file permission
11. Explain ABSOLUTE PERMISSION? With an example
12. Explain security implication.
13. Explain directory permission? With an example
14. Explain changing owner permission
15. Explain the following command with example
a. Chown
b. chgrp
16. Explain the modes of vi editor
17. Explain inserting a text (i and a) ? with an example.
18. Explain inserting a line at extremes (I and A) ? with an example.
19. Explain opening a new line (o and O) ? with an example.
20. Explain Replacing text (r,s, R and S) ? with an example.
21. What are the types of input mode command?
22. Explain editing text (4 types)? With an example.
23. Explain undoing last editing instructions
24. Explain Searching for a pattern (types)? Wuth an examples each.
10 marks
1. Explain “Changing file permission mod (chmod)”? explain their types (relative and absolute)
with an example.
2. Explain Input mode and type of commands? With an example
3. Explain Saving a text and Quitting – the ex mode (6 Types)? with an example.
4. Explain Navigation (5 types)? With an example.

DON BOSCO CHITRADURGA Page 57


UNIX 2023
5. The Shell:
The shell and its interpretive cycle
The shell sits between you and the operating system, acting as a command interpreter. It reads your
terminal input and translates the commands into actions taken by the system. The shell is analogous to
command.com in DOS. When you log into the system you are given a default shell.

The original shell was the Bourne shell, sh. Every Unix platform will either have the Bourne shell, or a
Bourne compatible shell available.

Some of the more well known of these may be on your Unix system: the Korn shell, ksh, by David
Korn, C shell, csh, by Bill Joy and the Bourne Again SHell, bash, from the Free Software Foundations GNU
project, both based on sh, the T-C shell, tcsh, and the extended C shell, cshe, both based on csh.

The following activities are typically performed by the shell in its interpretive cycle:
 The shell issues the prompt and waits for you to enter a command.
 After a command is entered, the shell scans the command line for metacharacters and expands
abbreviations (like the * in rm *) to recreate a simplified command line.
 It then passes on the command line to the kernel for execution.
 The shell waits for the command to complete and normally can’t do any work while the command is
running.
 After the command execution is complete, the prompt reappears and the shell returns to its waiting role
to start the next cycle. You are free to enter another command.

Pattern Matching – The Wild-Cards


A pattern is framed using ordinary characters and a metacharacter (like *) using well-defined rules. The
pattern can then be used as an argument to the command, and the shell will expand it suitably before the
command is executed.
The metacharacters that are used to construct the generalized pattern for matching filenames belong to
a category called wild-cards.
The following table lists them:

Examples:
To list all files that begins with chap, use
$ ls chap*
To list all files whose filenames are six characters long and start with chap, use
$ ls chap??

Note: Both * and ? Operate with some restrictions. for example, the * doesn’t match all files beginning with a .
(dot) or the / of a pathname. If you wish to list all hidden filenames in your directory having at least three
characters after the dot, the dot must be matched explicitly.

$ ls .???*

However, if the filename contains a dot anywhere but at the beginning, it need not be matched explicitly.

Similarly, these characters don’t match the / in a pathname. So, you cannot use
$ cd /usr?local to change to /usr/local.

DON BOSCO CHITRADURGA Page 58


UNIX 2023
The character class
You can frame more restrictive patterns with the character class. The character class comprises a set of
characters enclosed by the rectangular brackets, [ and ], but it matches a single character in the class. The pattern
[abd] is character class, and it matches a single character – an a, b or d.

Examples:

$ls chap0[1-4] Matches chap01, chap02, chap04 and lists if found.


$ ls chap[x-z] Matches chapx, chapy, chapz and lists if found.

You can negate a character class to reverse a matching criteria. For example,

 To match all filenames with a single-character extension but not the .c ot .o files,
use *.[!co]

 To match all filenames that don’t begin with an alphabetic character,


use [!a-zA-Z]*

Matching totally dissimilar patterns


This feature is not available in the Bourne shell. To copy all the C and Java source programs from
another directory, we can delimit the patterns with a comma and then put curly braces around them.

$ cp $HOME/prog_sources/*.{c,java} .

The Bourne shell requires two separate invocations of cp to do this job.

$ cp /home/srm/{project,html,scripts/* .

The above command copies all files from three directories (project, html and scripts) to the current directory.

Escaping and Quoting


Escaping is providing a \ (backslash) before the wild-card to remove (escape) its special meaning.

For instance, if we have a file whose filename is chap* (Remember a file in UNIX can be names with virtually
any character except the / and null), to remove the file, it is dangerous to give command as rm chap*, as it will
remove all files beginning with chap. Hence to suppress the special meaning of *, use the command rm chap\*

To list the contents of the file chap0[1-3], use

$ cat chap0\[1-3\]

A filename can contain a whitespace character also. Hence to remove a file named
My Documend.doc, which has a space embedded, a similar reasoning should be followed:

$ rm My\ Document.doc

Quoting is enclosing the wild-card, or even the entire pattern, within quotes. Anything within these
quotes (barring a few exceptions) are left alone by the shell and not interpreted.

When a command argument is enclosed in quotes, the meanings of all enclosed special characters are
turned off.

Examples:

$ rm ‘chap*’ Removes fil chap*


$ rm “My Document.doc” Removes file My Document.doc

DON BOSCO CHITRADURGA Page 59


UNIX 2023
Redirection : The three standard files
The shell associates three files with the terminal – two for display and one for the keyboard. These files
are streams of characters which many commands see as input and output. When a user logs in, the shell makes
available three files representing three streams. Each stream is associated with a default device:

Standard input: The file (stream) representing input, connected to the keyboard.
Standard output: The file (stream) representing output, connected to the display.
Standard error: The file (stream) representing error messages that emanate from the command or shell,
connected to the display.

The standard input can represent three input sources:


 The keyboard, the default source.
 A file using redirection with the < symbol.
 Another program using a pipeline.

The standard output can represent three possible destinations:


 The terminal, the default destination.
 A file using the redirection symbols > and >>.
 As input to another program using a pipeline.

A file is opened by referring to its pathname, but subsequent read and write operations identify the file by a
unique number called a file descriptor. The kernel maintains a table of file descriptors for every process running
in the system. The first three slots are generally allocated to the three standard streams as,

0 – Standard input
1 – Standard output
2 – Standard error

These descriptors are implicitly prefixed to the redirection symbols.

Examples:

Assuming file2 doesn’t exist, the following command redirects the standard output to file myOutput and the
standard error to file myError.

$ ls –l file1 file2 1>myOutput 2>myError

To redirect both standard output and standard error to a single file use:

$ ls –l file1 file2 1>| myOutput 2>| myError OR


$ ls –l file1 file2 1> myOutput 2>& 1

Filters: Using both standard input and standard output


UNIX commands can be grouped into four categories viz.,
1. Directory-oriented commands like mkdir, rmdir and cd, and basic file handling commands like cp, mv and rm
use neither standard input nor standard output.
2. Commands like ls, pwd, who etc. don’t read standard input but they write to standard output.
3. Commands like lp that read standard input but don’t write to standard output.
4. Commands like cat, wc, cmp etc. that use both standard input and standard output.

Commands in the fourth category are called filters. Note that filters can also read directly from files
whose names are provided as arguments.

Example: To perform arithmetic calculations that are specified as expressions in input file calc.txt and redirect
the output to a file result.txt, use

$ bc < calc.txt > result.txt

DON BOSCO CHITRADURGA Page 60


UNIX 2023
/dev/null and /dev/tty : Two special files

/dev/null: If you would like to execute a command but don’t like to see its contents on the screen, you may wish
to redirect the output to a file called /dev/null. It is a special file that can accept any stream without growing in
size. It’s size is always zero.

/dev/tty: This file indicates one’s terminal. In a shell script, if you wish to redirect the output of some select
statements explicitly to the terminal. In such cases you can redirect these explicitly to /dev/tty inside the script.

Pipes
With piping, the output of a command can be used as input (piped) to a subsequent command.
$ command1 | command2

Output from command1 is piped into input for command2.


This is equivalent to, but more efficient than:

$ command1 > temp


$ command2 < temp
$ rm temp
Examples
$ ls -al | more
$ who | sort | lpr

When a command needs to be ignorant of its source


If we wish to find total size of all C programs contained in the working directory, we can use the
command,
$ wc –c *.c
However, it also shows the usage for each file(size of each file). We are not interested in individual statistics,
but a single figure representing the total size. To be able to do that, we must make wc ignorant of its input
source. We can do that by feeding the concatenated output stream of all the .c files to wc –c as its input:
$ cat *.c | wc –c

Creating a TEE:tee
tee is an external command that handles a character stream by duplicating its input. It saves one copy in
a file and writes the other to standard output. It is also a filter and hence can be placed anywhere in a pipeline.
Example: The following command sequence uses tee to display the output of who and saves this output in a file
as well.

$ who | tee users.lst

Command substitution
The shell enables the connecting of two commands in yet another way. While a pipe enables a
command to obtain its standard input from the standard output of another command, the shell enables one or
more command arguments to be obtained from the standard output of another command. This feature is called
command substitution.

Example:
$ echo Current date and time is `date`

Observe the use of backquotes around date in the above command. Here the output of the command
execution of date is taken as argument of echo. The shell executes the enclosed command and replaces the
enclosed command line with the output of the command.

Similarly the following command displays the total number of files in the working directory.

$ echo “There are `ls | wc –l` files in the current directory”

Observe the use of double quotes around the argument of echo. If you use single quotes, the back quote
is not interpreted by the shell if enclosed in single quotes.

DON BOSCO CHITRADURGA Page 61


UNIX 2023
Shell variables
Environmental variables are used to provide information to the programs you use. You can have both
global environment and local shell variables. Global environment variables are set by your login shell and new
programs and shells inherit the environment of their parent shell. Local shell variables are used only by that
shell and are not passed on to other processes. A child process cannot pass a variable back to its parent process.
To declare a local shell variable we use the form variable=value (no spaces around =) and its evaluation
requires the $ as a prefix to the variable.

Example:
$ count=5
$ echo $count
5

A variable can be removed with unset and protected from reassignment by readonly. Both are shell internal
commands.

Note: In C shell, we use set statement to set variables. Here, there either has to be whitespace on both sides of
the = or none at all.

$ set count=5
$ set size = 10

Uses of local shell variables


1. Setting pathnames: If a pathname is used several times in a script, we can assign it to a variable and use it as
an argument to any command.

2. Using command substitution: We can assign the result of execution of a command to a variable. The
command to be executed must be enclosed in backquotes.

3. Concatenating variables and strings: Two variables can be concatenated to form a new variable.

Example:
$ base=foo ; ext=.c
$ file=$base$ext
$ echo $file // prints foo.c

Quetions:

2 marks
1. What is shell?
2. What is wild-card
3. What is escaping
4. What is quoting
5. What is redirection
6. What is filters
7. What is pipes
8. What is TEE command
9. What is command substitution
10.
5 marks
1. Explain the shell and its interpretive cycle?
2. Explain the pattern matching?
3. Name the wild cards matches in pattern matching?
4. Explain escaping and quoting
5. What is redirection and explain thee standards
6. Explain Filters using both standard input and standard output?

DON BOSCO CHITRADURGA Page 62


UNIX 2023
7. Explain Pipes? With an example
8. Explain creating TEE :tee.
9. Explain shell variables
10 marks
1. Anything ask from 5 marks questions as a combination of 2 quetions.

DON BOSCO CHITRADURGA Page 63


UNIX 2023
6. The Process:
A process is an OS abstraction that enables us to look at files and programs as their time image.

PROCESS BASICS
A process is a simply an instance of a running program. A process is said to be born when the program
starts execution and remains alive as long as the program is active, after execution is complete, the process is
said to be die.
UNIX is a multiuser and multitasking operating system. Multiuser means that several people can use
the computer system simultaneously (unlike a single-user operating system, such as MS-DOS). Multitasking
means that UNIX, like Windows NT, can work on several tasks concurrently; it can begin work on one task and
take up another before the first task is finished.
When you execute a program on your UNIX system, the system creates a special environment for that
program. This environment contains everything needed for the system to run the program as if no other program
were running on the system. Stated in other words, a process is created. A process is a program in execution. A
process is said to be born when the program starts execution and remains alive as long as the program is active.
After execution is complete, the process is said to die.
The kernel is responsible for the management of the processes. It determines the time and priorities that
are allocated to processes so that more than one process can share the CPU resources.

These attributes are maintained by the kernel in a data structure known as process table. Two important
attributes of a process are:

1. The Process-Id (PID): Each process is uniquely identified by a unique integer called the PID, that is
allocated by the kernel when the process is born. The PID can be used to control a process.
2. The Parent PID (PPID): The PID of the parent is available as a process attribute.

There are three types of processes viz.,

1. Interactive: Initiated by a shell and running in the foreground or background


2. batch: Typically a series of processes scheduled for execution at a specified point in time
3. daemon: Typically initiated at boot time to perform operating system functions on demand, such as LPD,
NFS, and DNS

a. The Shell Process


As soon as you log in, a process is set up by the kernel. This process represents the login shell, which can be
either sh(Bourne Shell), ksh(korn Shell), bash(Bourne Again Shell) or csh(C Shell).

b. Parents and Children

When you enter an external command at the prompt, the shell acts as the parent process, which in turn starts
the process representing the command entered. Since every parent has a parent, the ultimate ancestry of any
process can be traced back to the first process (PID 0) that is set up when the system is booted. It is analogous to
the root directory of the file system. A process can have only one parent. However, a process can spawn
multiple child processes.

c. Wait or not Wait?

A parent process can have two approaches for its child:


 It may wait for the child to die so that it can spawn the next process. The death of the child is intimated
to the parent by the kernel. Shell is an example of a parent that waits for the child to terminate.
However, the shell can be told not to wait for the child to terminate.
 It may not wait for the child to terminate and may continue to spawn other processes. init process is an
example of such a parent process.

ps: Process Status

UNIX has several commands that enable you to examine processes and modify their state. The most frequently
used command is ps, which prints out the process status for processes running on your system. The following
table shows the options available with ps command.

DON BOSCO CHITRADURGA Page 64


UNIX 2023

POSIX BSD Significance


-f F Full listing showing PPID of each
process
-e or –A Aux All processes (user and system)
processes
-u user U user Processes of user user only
-a Processes of all users excluding
processes not associated with
terminal
-l l Long listing showing memory
related information
-t term t term Processes running on the terminal
term

Examples
$ ps
PID TTY TIME CMD
4245 pts/7 00:00:00 bash
5314 pts/7 00:00:00 ps

The output shows the header specifying the PID, the terminal (TTY), the cumulative processor time (TIME) that
has been consumed since the process was started, and the process name (CMD).

$ ps -f
UID PID PPID C STIME TTY TIME COMMAND
root 14931 136 0 08:37:48 ttys0 0:00 rlogind
sartin 14932 14931 0 08:37:50 ttys0 0:00 -sh
sartin 15339 14932 7 16:32:29 ttys0 0:00 ps –f

The header includes the following information:

UID – Login name of the user


PID – Process ID
PPID – Parent process ID
C – An index of recent processor utilization, used by kernel for scheduling
STIME – Starting time of the process in hours, minutes and seconds
TTY – Terminal ID number
TIME – Cumulative CPU time consumed by the process
CMD – The name of the command being executed

System processes (-e or –A):


Apart from the processes a user generates, a number of system processes keep running all the time. Most of
them are not associated with any controlling terminal.
They are spawned during system startup and some of them start when the system goes into multiuser mode.
These processes are known as daemons because they are called without a specific request from a user.
To list all processes running on your mechine, use the –e or –A option to ps.

$ ps –e

PID TTY TIME CMD


0 ? 0:34 sched
1 ? 41:55 init
23274 Console 0:03 sh
272 ? 2:47 cron
7015 term/12 20:04 vi

DON BOSCO CHITRADURGA Page 65


UNIX 2023
Mechanism of Process Creation
There are three distinct phases in the creation of a process and uses three important system calls viz.,
fork, exec, and wait. The three phases are discussed below:
 Fork: A process in UNIX is created with the fork system call, which creates a copy of the process that
invokes it. The process image is identical to that of the calling process, except for a few parameters like
the PID. The child gets a new PID.
 Exec: The forked child overwrites its own image with the code and data of the new program. This
mechanism is called exec, and the child process is said to exec a new program, using one of the family
of exec system calls. The PID and PPID of the exec’d process remain unchanged.
 Wait: The parent then executes the wait system call to wait for the child to complete. It picks up the
exit status of the child and continues with its other functions. Note that a parent need not decide to wait
for the child to terminate.

When a process is forked, the child has a different PID and PPID from its parent. However, it inherits most of
the attributes of the parent.

How the shell created


When the system moves to multiuser mode, init forks and exec a getty for every active communication port (or
line). Each of these gettys prints the login prompt on the respective terminal and then goes off to sleep.
When a user attempts to login, getty wakes up and fork-exec the login program to verify the login name and
password entered.
On successful login, login forks-exec the process representing the login shell. repeated overlying ultimately
result in init becoming the immediate ancestor of the shell as can be seen from the sequences;

Init getty login shell


Fork fork-exec fork-exec

Internal and External Commands


From the process viewpoint, the shell recognizes three types of commands:

1. External commands: Commonly used commands like cat, ls etc. The shell creates a process for each of
these commands while remaining their parent.
2. Shell scripts: The shell executes these scripts by spawning another shell, which then executes the commands
listed in the script. The child shell becomes the parent of the commands that feature in the shell.
3. Internal commands: When an internal command is entered, it is directly executed by the shell. Similarly,
variable assignment like x=5, doesn’t generate a process either.

Process States and Zombies


At any instance of time, a process is in a particular state. A process after creation is in the runnable state. Once
it starts running, it is in the running state. When a process requests for a resource (like disk I/O), it may have to
wait. The process is said to be in waiting or sleeping state. A process can also be suspended by pressing a key
(usually Ctrl-z).
When a process terminates, the kernel performs clean-up, assigns any children of the exiting process to be
adopted by init, and sends the death of a child signal to the parent process, and converts the process into the
zombie state.
A process in zombie state is not alive; it does not use any resources nor does any work. But it is not allowed to
die until the exit is acknowledged by the parent process.
It is possible for the parent itself to die before the child dies. In such case, the child becomes an orphan and the
kernel makes init the parent of the orphan. When this adopted child dies, init waits for its death.

Running Jobs in Background


There are two ways of starting a job in the background – with the shell’s & operator and the nohup command.

a. &: No Logging out

Ordinarily, when the shell runs a command for you, it waits until the command is completed. During this
time, you cannot communicate with the shell. You can run a command that takes a long time to finish as a
background job, so that you can be doing something else. To do this, use the & symbol at the end of the
command line to direct the shell to execute the command in the background.

DON BOSCO CHITRADURGA Page 66


UNIX 2023
$ sort –o emp.dat emp.dat &
[1] 1413 The job’s PID

Note:
1. Observe that the shell acknowledges the background command with two numbers. First number [1] is the job
ID of this command. The other number 1413 is the PID.
2. When you specify a command line in a pipeline to run in the background, all the commands are run in the
background, not just the last command.
3. The shell remains the parent of the background process.

b. nohup: Log out Safely

A background job executed using & operator ceases to run when a user logs out. This is because, when you
logout, the shell is killed and hence its children are also killed. The UNIX system provides nohup statement
which when prefixed to a command, permits execution of the process even after the user has logged out. You
must use the & with it as well.

The syntax for the nohup command is as follows:

nohup command-string [input-file] output-file &

If you try to run a command with nohup and haven’t redirected the standard error, UNIX automatically places
any error messages in a file named nohup.out in the directory from which the command was run.
In the following command, the sorted file and any error messages are placed in the file nohup.out.

$ nohup sort emp.lst &


1252
Sending output to nohup.out

Note that the shell has returned the PID (1252) of the process.

When the user logs out, the child turns into an orphan. The kernel handles such situations by
reassigning the PPID of the orphan to the system’s init process (PID 1) - the parent of all shells. When the user
logs out, init takes over the parentage of any process run with nohup. In this way, you can kill a parent (the
shell) without killing its child.

nice: Job Execution with Low Priority

The nice command is used to control background process dispatch priority.


The idea behind nice is that background jobs should demand less attention from the system than interactive
processes.
Background jobs execute without a terminal attached and are usually run in the background for two reasons:
1. the job is expected to take a relatively long time to finish, and
2. the job's results are not needed immediately.

Interactive processes, however, are usually shells where the speed of execution is critical because it
directly affects the system's apparent response time. It would therefore be nice for everyone (others as well as
you) to let interactive processes have priority over background work.
nice values are system dependent and typically range from 1 to 19.
A high nice value implies a lower priority. A program with a high nice number is friendly to other programs,
other users and the system; it is not an important job. The lower the nice number, the more important a job is
and the more resources it will take without sharing them.

Example:

$ nice wc –l hugefile.txt
OR
$ nice wc –l hugefile.txt &

DON BOSCO CHITRADURGA Page 67


UNIX 2023
The default nice value is set to 10.

We can specify the nice value explicitly with –n number option where number is an offset to the default. If the –
n number argument is present, the priority is incremented by that amount up to a limit of 20.
Example: $ nice –n 5 wc –l hugefile.txt &

Killing Processes with Signals

The user of the command may press the interrupt key or send a kill command to the process, or the
controlling terminal may become disconnected from the system. In UNIX, any of these events can cause a
signal to be sent to the process. The default action when a process receives a signal is to terminate.
When a process ends normally, the program returns its exit status to the parent. This exit status is a number
returned by the program providing the results of the program's execution.
Sometimes, you want or need to terminate a process.
The following are some reasons for stopping a process:

 It’s using too much CPU time.


 It’s running too long without producing the expected output.
 It’s producing too much output to the screen or to a disk file.
 It appears to have locked a terminal or some other session.
 It’s using the wrong files for input or output because of an operator or programming error.
 It’s no longer useful.

If the process to be stopped is a background process, use the kill command to get out of these situations. To stop
a command that isn’t in the background, press <ctrl-c>.

To use kill, use either of these forms:

kill PID(s) OR kill –s NUMBER PID(s)


To kill a process whose PID is 123 use,

$ kill 123

To kill several processes whose PIDs are 123, 342, and 73 use,

$ kill 123 342 73

Issuing the kill command sends a signal to a process. The default signal is SIGTERM signal (15).
UNIX programs can send or receive more than 20 signals, each of which is represented by a number. (Use kill –
l to list all signal names and numbers)

If the process ignores the signal SIGTERM, you can kill it with SIGKILL signal (9) as,

$ kill -9 123
OR
$ kill –s KILL 123

The system variable $! stores the PID of the last background job. You can kill the last background job without
knowing its PID by specifying $ kill $!

Job Control

A job is a name given to a group of processes that is typically created by piping a series of commands
using pipeline character. You can use job control facilities to manipulate jobs. You can use job control facilities
to,
1. Relegate a job to the background (bg)
2. Bring it back to the foreground (fg)
3. List the active jobs (jobs)
4. Suspend a foreground job ([Ctrl-z])

DON BOSCO CHITRADURGA Page 68


UNIX 2023
5. Kill a job (kill)

The following examples demonstrate the different job control facilities.


Assume a process is taking a long time. You can suspend it by pressing [Ctrl-z].

[1] + Suspended wc –l hugefile.txt


A suspended job is not terminated. You can now relegate it to background by,

$ bg

You can start more jobs in the background any time:

$ sort employee.dat > sortedlist.dat &


[2] 530

$ grep ‘director’ emp.dat &


[3] 540

You can see a listing of these jobs using jobs command,

$ jobs
[3] + Running grep ‘director’ emp.dat &
[2] - Running sort employee.dat > sortedlist.dat &
[1] Suspended wc –l hugefile.txt

You can bring a job to foreground using fg %jobno OR fg %jobname as,

$ fg %2
OR
$ fg %sort

Questions:
2 marks
1. What is process?
2. What is multiuser?
3. What is multitasking?
4. What is process ID (PID)?
5. What is parent process ID (PPID)?
6. Name the types of process?
7. What is interactive process?
8. What is batch process?
9. What is daemon process?
10. What is shell process?
11. What is parent and child process?
12. What is Wait and NOT wait process.
13. What is process status
14. What is Zombie status
15. Explain Running Job in background (2 types)?
16. Explain nice: Job execution with low priority? With an example.
17. Explain Killing Process with signal? With an example.
18. Explain Job Control steps and their commands (5 types)
5 marks
1. Explain the Process and its attributes?
2. Explain the process status? With an example
3. Explain the following terms
a. Shell process
b. Parent and child process
c. Wait and NOT wait process
4. Explain System process(-e or -A)

DON BOSCO CHITRADURGA Page 69


UNIX 2023
5. Explain the mechanism of process creation.
6. Explain the creation of Shell.
7. Explain Internal and External Commands.
8.
10 marks
1. What are the functions of following terms
a. UID
b. PID
c. PPID
d. C
e. STIME
f. TTY
g. TIME
h. CMD
2. Anything ask from 5 marks questions as a combination of 2 quetions.

DON BOSCO CHITRADURGA Page 70


UNIX 2023
7. Essential Shell Programming:
Before Starting Linux Shell Script Programming you must know
 Kernel
 Shell
 Process
 Redirectors, Pipes, Filters etc.

What's Kernel
Kernel is heart of Linux O/S. It manages resource of Linux O/S. Resources means facilities available in Linux.
For eg. Facility to store data, print data on printer, memory, file management etc . Kernel decides who will use
this resource, for how long and when. It runs your programs (or set up to execute binary files) It's Memory
resident portion of Linux.
It performance following task :-

 I/O management
 Process management
 Device management
 File management
 Memory management

What's Linux Shell


Computer understand the language of 0's and 1's called binary language, In early days of computing, instruction
are provided using binary language, which is difficult for all of us, to read and write. So in O/s there is special
program called Shell. Shell accepts your instruction or commands in English and translate it into computers
native binary language.

This is what Shell Does for US

You type Your command and shell convert it as

It's environment provided for user interaction. Shell is an command language interpreter that executes
commands read from the standard input device (keyboard) or from a file. Linux may use one of the following
most popular shells (In MS-DOS, Shell name is COMMAND.COM which is also used for same purpose, but it's
not as powerful as our Linux Shells are!)

Any of the above shell reads command from user (via Keyboard or Mouse) and tells Linux O/s what
users want. If we are giving commands from keyboard it is called command line interface ( Usually in-front of $
prompt, This prompt is depend upon your shell and Environment that you set or by your System Administrator,
therefore you may get different prompt ).
NOTE: To find your shell type following command

$ echo $SHELL

DON BOSCO CHITRADURGA Page 71


UNIX 2023
Linux Common Commands
NOTE: the following commands are briefly explanation only as we have already studied in previous chapter.
The purpose is if we use this command we will be more familiar with the shell and secondly, we need some of
these command in our Shell script. If you want to get more information or help for this command try following
commands For e.g. To see help or options related with date command try

$ date –help // help gives more information of the command

or To see help or options related with ls command (Here you will screen by screen help, since help of ls
command is quite big that can't fit on single screen )

$ ls --help | more

Syntax: command-name --help


Syntax: man command-name
Syntax: info command-name

See what happened when you type following

$ man ls
$ info bash

NOTE: In MS-DOS, you get help by using /? clue or by typing help command as

C:\> dir /?
C:\> date /?
C:\> help time
C:\> help date
C:\> help

DON BOSCO CHITRADURGA Page 72


UNIX 2023

What is Processes
Process is any kind of program or task carried out by your PC. For e.g. $ ls -lR , is command or a
request to list files in a directory and all subdirectory in your current directory. It is a process. A process is
program (command iven by user) to perform some Job. In Linux when you start process, it gives a number
(called PID or process- d), PID starts from 0 to 65535.

Why Process required


Linux is multi-user, multitasking o/s. It means you can run more than two process simultaneously if you wish.
or e.g.. To find how many files do you have on your system you may give command like

$ ls / -R | wc –l
This command will take lot of time to search all files on your system. So you can run such command in
Background or simultaneously by giving command like

DON BOSCO CHITRADURGA Page 73


UNIX 2023
$ ls / -R | wc -l &

The ampersand (&) at the end of command tells shells start command (ls / -R | wc -l) and run it in background
akes next command immediately. An instance of running command is called process and the number printed by
shell is called process-id (PID), this PID can be use to refer specific running process.

Redirection of Standard output/input or Input – Output redirection


Mostly all command gives output on screen or take input from keyboard, but in Linux it's possible to send
output to file or to read input from file.
For e.g. $ ls command gives output to screen; to send output to file of ls give command , $ ls > filename. It
means put output of ls command to filename. There are three main redirection symbols >,>>,<

(1) > Redirector Symbol

Syntax: Linux-command > filename

To output Linux-commands result to file. Note that If file already exist, it will be overwritten else new file is
created.

For e.g. To send output of ls command give


$ ls > myfiles

Now if 'myfiles' file exist in your current directory it will be overwritten without any type of warning. (What if I
want to send output to file, which is already exist and want to keep information of that file without loosing
previous information/data?, For this Read next redirector)

(2) >> Redirector Symbol

Syntax: Linux-command >> filename

To output Linux-commands result to END of file. Note that If file exist , it will be opened and new information /
data will be written to END of file, without losing previous information/data, And if file is not exist, then new
file is created.

For e.g. To send output of date command to already exist file give
$ date >> myfiles

(3) < Redirector Symbol

Syntax: Linux-command < filename

DON BOSCO CHITRADURGA Page 74


UNIX 2023
To take input to Linux-command from file instead of key-board.

For e.g. To take input for cat command give


$ cat < myfiles

Pips
A pipe is a way to connect the output of one program to the input of another program without any
temporary file.

Filter
If a Linux command accepts its input from the standard input and produces its output on standard
output is know as a filter. A filter performs some kind of process on the input and gives output.
For e.g.. Suppose we have file called 'hotel.txt' with 100 lines data, And from 'hotel.txt' we would like
to print contains from line number 20 to line number 30 and store this result to file called 'hlist' then give
command

$ tail +20 < hotel.txt | head -n30 >hlist

Here head is filter which takes its input from tail command (tail command start selecting from line number 20 of
given file i.e. hotel.txt) and passes this lines to input to head, whose output is redirected to 'hlist' file.

Introduction to Shell Programming:


Definition of Shell:
Shell is an agency that sits between the user and the UNIX system.
Description:
Shell is the one which understands all user directives and carries them out. It processes the commands
issued by the user. The content is based on a type of shell called Bourne shell.

Shell Scripts
When groups of command have to be executed regularly, they should be stored in a file, and the file
itself executed as a shell script or a shell program by the user. A shell program runs in interpretive mode. It is
not complied with a separate executable file as with a C program but each statement is loaded into memory
when it is to be executed. Hence shell scripts run slower than the programs written in high-level language. .sh is
used as an extension for shell scripts. However the use of extension is not mandatory.

DON BOSCO CHITRADURGA Page 75


UNIX 2023
Shell scripts are executed in a separate child shell process which may or may not be same as the login
shell.
Example: script.sh

#! /bin/sh
# script.sh: Sample Shell Script
echo “Welcome to Shell Programming”
echo “Today’s date : `date`”
echo “This months calendar:”
cal `date “+%m 20%y”` #This month’s calendar.
echo “My Shell :$SHELL”

The # character indicates the comments in the shell script and all the characters that follow the # symbol are
ignored by the shell. However, this does not apply to the first line which beings with #. This because, it is an
interpreter line which always begins with #! Followed by the pathname of the shell to be used for running the
script. In the above example the first line indicates that we are using a Bourne Shell.

To run the script we need to first make it executable. This is achieved by using the chmod command as shown
below:

$ chmod +x script.sh Then invoke the script names:


$ script.sh Once this is done, we can see the following output :

Welcome to Shell Programming


Today’s date: Thur April 27 11:11:45 IST 2023
This month’s calendar:
April 2023
Su Mo Tu We Th Fr Sa
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31

My Shell: /bin/Sh

As stated above the child shell reads and executes each statement in interpretive mode. We can also
explicitly spawn a child of your choice with the script name as argument:

sh script.sh

Note: Here the script neither requires a executable permission nor an interpreter line.

read: Making scripts interactive

The read statement is the shell’s internal tool for making scripts interactive (i.e. taking input from the
user). It is used with one or more variables. Inputs supplied with the standard input are read into these variables.
For instance, the use of statement like

read name

the script to pause at that point to take input from the keyboard. Whatever is entered by you will be stored in the
variable name.

Example: A shell script that uses read to take a search string and filename from the terminal.

DON BOSCO CHITRADURGA Page 76


UNIX 2023
#! /bin/sh
# emp1.sh: Interactive version, uses read to accept two inputs
#
echo “Enter the pattern to be searched: \c” # No newline
read pname
echo “Enter the file to be used: \c” # use echo –e in bash
read fname
echo “Searching for pattern $pname from the file $fname”
grep $pname $fname
echo “Selected records shown above”

Running of the above script by specifying the inputs when the script pauses twice:

$ emp1.sh
Enter the pattern to be searched : director
Enter the file to be used: emp.lst
Searching for pattern director from the file emp.lst
9876 Jai Sharma Director Productions
2356 Rohit Director Sales
Selected records shown above

Using Command Line Arguments


Shell scripts also accept arguments from the command line. Therefore e they can be run non
interactively and be used with redirection and pipelines. The arguments are assigned to special shell variables.
Represented by $1, $2, etc; similar to C command arguments argv[0], argv[1], etc. The following table lists the
different shell parameters.

Shell parameter Significance


$1, $2… Positional parameters representing command line arguments
$# Number of arguments specified in command line
$0 Name of the executed command
$* Complete set of positional parameters as a single string
“$ @” Each quoted string treated as separate argument
$? Exit status of last command
$$ PID of the current shell
$! PID of the last background job.

Table: shell parameters

exit and Exit Status of Command


To terminate a program exit is used. Nonzero value indicates an error condition.
Example 1:
$ cat foo
Cat: can’t open foo
Returns nonzero exit status. The shell variable $? Stores this status.

Example 2:
grep director emp.lst > /dev/null:echo
$? 0
Exit status is used to devise program logic that braches into different paths depending on success or failure of a
command

The logical Operators && and ||


The shell provides two operators that aloe conditional execution, the && and ||.
Usage:
cmd1 && cmd2
cmd1 || cmd2
The && delimits two commands. cmd 2 executed only when cmd1 succeeds.

DON BOSCO CHITRADURGA Page 77


UNIX 2023
Example1:
$ grep ‘director’ emp.lst && echo “Pattern found”
Output:
9876 Jai Sharma Director Productions
2356 Rohit Director Sales
Pattern found

Example 2:
$ grep ‘clerk’ emp.lst || echo “Pattern not found”
Output:
Pattern not found

Example 3:
grep “$1” $2 || exit 2
echo “Pattern Found Job Over”

The if Conditional
The if statement makes two way decisions based on the result of a condition. The following forms of if
are available in the shell:

Form 1 Form 2 Form 3


if command is successful if command is successful if command is successful
then then then
execute commands execute commands execute commands
else fi elif command is successful
execute commands then... then...
fi else...
fi

If the command succeeds, the statements within if are executed or else statements in else block are executed (if
else present).

Example:

#! /bin/sh
if grep “^$1” /etc/passwd 2>/dev/null
then
echo “Pattern Found”
else
echo “Pattern Not Found”
fi

Output1:
$ emp3.sh ftp
ftp: *.325:15:FTP
User:/Users1/home/ftp:/bin/true Pattern Found

Output2:
$ emp3.sh mail
Pattern Not Found

Using test and [ ] to Evaluate Expressions


Test statement is used to handle the true or false value returned by expressions, and it is not possible with if
statement. Test uses certain operators to evaluate the condition on its right and returns either a true or false exit
status, which is then used by if for making decisions.
Test works in three ways:
 Compare two numbers.
 Compares two strings or a single one for a null value.
 Checks files attributes.

DON BOSCO CHITRADURGA Page 78


UNIX 2023

Test doesn’t display any output but simply returns a value that sets the parameters $?
a. Numeric Comparison
Operator Meaning
== -eq Equal to
!= -ne Not equal to
-gt Greater than
-ge Greater than or equal to
-lt Less than
-le Less than or equal
Operators always begin with a – (Hyphen) followed by a two word character word and enclosed on
either side by whitespace. Numeric comparison in the shell is confined to integer values only, decimal values are
simply truncated.

Ex:
$x=5;y=7;z=7.2
1. $test $x –eq $y; echo $?
1 Not equal
2. $test $x –lt $y; echo $?
0 True
3. $test $z –gt $y; echo $?
1 7.2 is not greater than
4. $test $z –eq $y ; echo $y
0 7.2 is equal to 7

Example 3 and 4 shows that test uses only integer comparison.

The script emp.sh uses test in an if-elif-else-fi construct (Form 3) to evaluate the shell parameter $#.

#!/bin/sh
#emp.sh: using test, $0 and $# in an if-elif-else-fi construct
#
If test $# -eq 0; then
Echo “Usage : $0 pattern file” > /dev/tty
Elfi test $# -eq 2 ;then
Grep “$1” $2 || echo “$1 not found in $2”>/dev/tty
Else
echo “You didn’t enter two arguments” >/dev/tty
fi

It displays the usage when no arguments are input, runs grep if two arguments are entered and displays
an error message otherwise. Run the script four times and redirect the output every time

$emp31.sh>foo
Usage : emp.sh pattern file

$emp31.sh ftp>foo
You didn’t enter two arguments

$emp31.sh henry /etc/passwd>foo


Henry not found in /etc/passwd

$emp31.sh ftp /etc/passwd>foo


$cat foo
ftp:*:325:15:FTP User:/user1/home/ftp:/bin/true

b. String Comparison
Test command is also used for testing strings. Test can be used to compare strings with the following set
of comparison operators as listed below.

DON BOSCO CHITRADURGA Page 79


UNIX 2023
Test True if
s1=s2 - String s1=s2
s1!=s2 - String s1 is not equal to s2
-n stg - String stg is not a null string
-z stg - String stg is a null string
stg - String stg is assigned and not null
s1= =s2 - String s1=s

Example:

#!/bin/sh
#emp1.sh checks user input for null values finally turns emp.sh developed previously
#
If [ $# -eq 0 ] ; then
echo “Enter the string to be searched :\c”
read pname
if [ -z “$pname” ] ; then # -z checks for null string
echo “You have not entered th e string”; exit 1
fi
echo “Enter the filename to be used :\c”
read flname
if [ ! –n “$flname” ] ; then # -n is same as -z
echo “ You have not entered the flname” ; exit 2
fi
emp.sh “$pname” “$flname” # Runs the script that will do the job
else
emp.sh $* # we will change $* to “$@” soon
fi

Output1:
$emp1.sh

Enter the string to be searched :[Enter]


You have not entered the string

Output2:
$emp1.sh

Enter the string to be searched :root


Enter the filename to be searched :/etc/passwd

Root:x:0:1:Super-user:/:/usr/bin/bash

When we run the script with arguments emp1.sh bypasses all the above activities and calls emp.sh to perform all
validation checks

$emp1.sh jai
You didn’t enter two arguments

$emp1.sh jai emp,lst


9878|jai sharma|director|sales|12/03/56|70000

$emp1.sh “jai sharma” emp.lst

You didn’t enter two arguments, Because $* treats jai and sharma are separate arguments. And $# makes a
wrong argument count. Solution is replace $* with “$@” (with quote” and then run the script.

DON BOSCO CHITRADURGA Page 80


UNIX 2023
c. File Tests
Test can be used to test various file attributes like its type (file, directory or symbolic links) or its permission
(read, write. Execute, SUID, etc).

Example:

$ ls –l emp.lst
-rw-rw-rw- 1 kumar group 870 jun 8 15:52 emp.lst

$ [-f emp.lst] ; echo $? Ordinary file


0 yes
$ [-x emp.lst] ; echo $? Not an executable.
1 No
$ [! -w emp.lst] || echo “False that file not writeable”
False that file is not writable.

Example: filetest.sh

#! /bin/usr
#
if [! –e $1] : then
Echo “File doesnot exist”
elif [! –r S1]; then
Echo “File not readable”
elif[! –w $1]; then
Echo “File not writable”
else
Echo “File is both readable and writable”\
Fi

Output:
$ filetest.sh emp3.lst
File does not exist
$filetest.sh emp.lst
File is both readable and writable
The following table depicts file-related Tests with test:

Test True if File


-f file ------------ File exists and is a regular file
-r file ------------ File exists and readable
-w file ------------ File exists and is writable
-x file ------------ File exists and is executable
-d file ------------ File exists and is a directory
-s file ------------ File exists and has a size greater than zero
-e file ------------ File exists (Korn & Bash Only)
-u file ------------ File exists and has SUID bit set
-k file ------------ File exists and has sticky bit set
-L file ------------ File exists and is a symbolic link (Korn & Bash Only)
f1 –nt f2 ------------ File f1 is newer than f2 (Korn & Bash Only)
f1 –ot f2 ------------ File f1 is older than f2 (Korn & Bash Only)
f1 –ef f2 ------------ File f1 is linked to f2 (Korn & Bash Only)

The case Conditional


The case statement is the second conditional offered by the shell. It doesn’t have a parallel either in C
(Switch is similar) or perl. The statement matches an expression for more than one alternative, and uses a
compact construct to permit multiway branching. case also handles string tests, but in a more efficient manner
than if.

DON BOSCO CHITRADURGA Page 81


UNIX 2023
Syntax:

case expression in
Pattern1) commands1;;
Pattern2) commands2;;
Pattern3) commands3;;

esac

Case first matches expression with pattern1. if the match succeeds, then it executes commands1, which may be
one or more commands. If the match fails, then pattern2 is matched and so forth. Each command list is
terminated with a pair of semicolon and the entire construct is closed with esac (reverse of case).

Example:

#! /bin/sh
# echo “
Menu\n
1. List of files\n2. Processes of user\n3. Today’s Date
4. Users of system\n5.Quit\nEnter your option: \c”
read choice
case “$choice” in

1) ls –l;;
2) ps –f ;;
3) date ;;
4) who ;;
5) exit ;;
*) echo “Invalid option” # ;; not really required for the last option
esac

Output:

$ menu.sh

Menu
1. List of files
2. Processes of user
3. Today’s Date
4. Users of system
5. Quit
Enter your option: 3
Mon Oct 8 08:02:45 IST 2007
Note:
 case can not handle relational and file test, but it matches strings with compact code. It is very effective
when the string is fetched by command substitution.
 case can also handle numbers but treats them as strings.

a. Matching Multiple Patterns:


case can also specify the same action for more than one pattern . For instance to test a user response for
both y and Y (or n and N).

Example:
Echo “Do you wish to continue? [y/n]: \c”
Read ans
Case “$ans” in
Y | y );; null statement, no action to be performed
N | n) exit ;;
esac

DON BOSCO CHITRADURGA Page 82


UNIX 2023
b. Wild-Cards: case uses them:
case has a superb string matching feature that uses wild-cards. It uses the filename matching metacharacters *, ?
and character class (to match only strings and not files in the current directory).

c. expr: Computation and String Handling


The Broune shell uses expr command to perform computations. This command combines the following two
functions:
 Performs arithmetic operations on integers
 Manipulates strings

c.1. Computation:
expr can perform the four basic arithmetic operations (+, -, *, /), as well as modulus (%) functions.

Examples:
$ x=3 y=5
$ expr 3+5
8
$ expr $x-$y
-2
$ expr 3 \* 5
Note:\ is used to prevent the shell from interpreting * as metacharacter 15

$ expr $y/$x
1
$ expr 13%5
3
expr is also used with command substitution to assign a variable.

Example1:
$ x=6 y=2 : z=`expr $x+$y`
$ echo $z
8

Example2:
$ x=5
$ x=`expr $x+1`
$ echo $x
6
c.2 String Handling:
expr is also used to handle strings. For manipulating strings, expr uses two expressions separated by a
colon (:). The string to be worked upon is closed on the left of the colon and a regular expression is placed on its
right. Depending on the composition of the expression expr can perform the following three functions:

1. Determine the length of the string.


2. Extract the substring.
3. Locate the position of a character in a string.

1. Length of the string:


The regular expression .* is used to print the number of characters matching the pattern .
Example1:
$ expr “abcdefg” : ‘.*’
7

Example2:

while echo “Enter your name: \c” ;do


read name
if [`expe “$name” :’.*’` -gt 20] ; then
echo “Name is very long”

DON BOSCO CHITRADURGA Page 83


UNIX 2023
else
break
fi
done

2. Extracting a substring:
expr can extract a string enclosed by the escape characters \ (and\).
Example:
$ st=2007
$ expr “$st” :’..\(..\)’
07 Extracts last two characters.

3. Locating position of a character:


expr can return the location of the first occurrence of a character inside a string.
Example:
$ stg = abcdefgh ; expr “$stg” : ‘[^d]*d’

4 Extracts the position of character d

$0: Calling a Script by Different Names


There are a number of UNIX commands that can be used to call a file by different names and doing
different things depending on the name by which it is called. $0 can also be to call a script by different names.

Example:
#! /bin/sh
# lastfile=`ls –t*.c | head -n 1`
command=$0 # assigning a special parameter to a variable - OK
exe=`expr $lastfile: ‘\(.*\).c’` # Removes .c; foo.c becomes foo
case $command in
*runc) $exe ;; # runs executable
*vic) vi $lastfile;;
*comc) cc –o $exe $lastfile &&
Echo “$lastfile compiled successfully”;;
esac

After this create the following three links:


ln comc.sh comc
ln comc.sh runc
ln comc.sh vic

Output:
$ comc
hello.c compiled successfully.

While: Looping
To carry out a set of instruction repeatedly shell offers three features namely while, until and for.

Synatx:

while condition is true


do
Commands
done

The commands enclosed by do and done are executed repadetedly as long as condition is true.

Example:

DON BOSCO CHITRADURGA Page 84


UNIX 2023
#! /bin/usr
ans=y
while [“$ans”=”y”]
do
echo “Enter the code and description : \c” > /dev/tty
read code description
echo “$code $description” >>newlist
echo “Enter any more [Y/N]”
read any
case $any in
Y* | y* ) answer =y;;
N* | n*) answer = n;;
*) answer=y;;
esac
done

Input:
Enter the code and description : 03 analgestics
Enter any more [Y/N] :y
Enter the code and description : 04 antibiotics
Enter any more [Y/N] : [Enter]
Enter the code and description : 05 OTC drugs
Enter any more [Y/N] : n

Output:
$ cat newlist
03 | analgestics
04 | antibiotics
05 | OTC drugs

for: Looping with a List

for is also a repetitive structure.


Synatx:
for variable in list
do
Commands
done

list here comprises a series of character strings. Each string is assigned to variable specified.

Example:
for file in ch1 ch2; do
> cp $file ${file}.bak
> echo $file copied to $file.bak
>done

Output:

ch1 copied to ch1.bak


ch2 copied to ch2.bak

a. Sources of list:
 List from variables: Series of variables are evaluated by the shell before executing the loop:
Example:
$ for var in $PATH $HOME; do echo “$var” ; done
Output:
/bin:/usr/bin;/home/local/bin;

DON BOSCO CHITRADURGA Page 85


UNIX 2023
/home/user1

 List from command substitution: Command substitution is used for creating a list. This is used when
list is large.
Example:
$ for var in `cat clist`

 List from wildcards: Here the shell interprets the wildcards as filenames.

Example:

for file in *.htm *.html ; do


sed ‘s/strong/STRONG/g
s/img src/IMG SRC/g’ $file > $$
mv $$ $file
gzip $file
done

 List from positional parameters:

Example: emp.sh

#! /bin/sh
for pattern in “$@”; do
grep “$pattern” emp.lst || echo “Pattern $pattern not
found”
done

Output:

$emp.sh 9876 “Rohit”


9876 Jai Sharma Director Productions
2356 Rohit Director Sales

basename: Changing Filename Extensions


They are useful in chaining the extension of group of files. Basename extracts the base filename from an
absolute pathname.

Example1:
$basename /home/user1/test.pl
Ouput:
test.pl

Example2:
$basename test2.doc doc
Ouput:
test2

Example3: Renaming filename extension from .txt to .doc


for file in *.txt ; do
leftname=`basename $file .txt` Stores left part of
filename mv $file ${leftname}.doc
done

set and shift: Manipulating the Positional Parameters


The set statement assigns positional parameters $1, $2 and so on, to its arguments. This is used for picking up
individual fields from the output of a program.

Example 1:

DON BOSCO CHITRADURGA Page 86


UNIX 2023
$ set 9876 2345 6213
$-
This assigns the value 9876 to the positional parameters $1, 2345 to $2 and 6213 to $3. It also sets the other
parameters $# and $*.

Example 2:
$ set `date`
$ echo $*
Mon Oct 8 08:02:45 IST 2007

Example 3:
$ echo “The date today is $2 $3, $6”
The date today is Oct 8, 2007

Shift: Shifting Arguments Left


Shift transfers the contents of positional parameters to its immediate lower numbered one. This is done as many
times as the statement is called. When called once, $2 becomes $1, $3 becomes S2 and so on.

Example 1:
$ echo “$@” $@ and $* are interchangeable
Mon Oct 8 08:02:45 IST 2007

$ echo $1 $2 $3
Mon Oct 8

$shift
$echo $1 $2 $3
Mon Oct 8 08:02:45

$shift 2 Shifts 2 places $echo $1 $2 $3


08:02:45 IST 2007

Set -- : Helps Command Substitution


Inorder for the set to interpret - and null output produced by UNIX commands the – option is used . If not used –
in the output is treated as an option and set will interpret it wrongly. In case of null, all variables are displayed
instead of null.

Example:
$set `ls –l chp1`

Output:
-rwxr-xr-x: bad options

Example2:
$set `grep usr1 /etc/passwd`
Correction to be made to get correct output are:
$set -- `ls –l chp1`
$set -- `grep usr1 /etc/passwd`

The Here Document (<<)


The shell uses the << symbol to read data from the same file containing the script. This is referred to as a here
document, signifying that the data is here rather than in aspirate file. Any command using standard input can slo
take input from a here document.

Example:
mailx kumar << MARK
Your program for printing the invoices has been
executed on `date`.Check the print queue
The updated file is

DON BOSCO CHITRADURGA Page 87


UNIX 2023
$flname MARK
The string (MARK) is delimiter. The shell treats every line following the command and delimited by MARK as
input to the command. Kumar at the other end will see three lines of message text with the date inserted by
command. The word MARK itself doesn’t show up.

a. Using Here Document with Interactive Programs:

A shell script can be made to work non-interactively by supplying inputs through here document.
Example:

$ search.sh <<
END > director
>emp.lst
>END

Output:

Enter the pattern to be searched: Enter the file to be used: Searching for director from file
emp.lst

9876 Jai Sharma Director Productions


2356 Rohit Director Sales

Selected records shown above.


The script search.sh will run non-interactively and display the lines containing “director” in the file emp.lst.

trap: interrupting a Program


Normally, the shell scripts terminate whenever the interrupt key is pressed. It is not a good programming
practice because a lot of temporary files will be stored on disk. The trap statement lets you do the things you
want to do when a script receives a signal. The trap statement is normally placed at the beginning of the shell
script and uses two lists:
trap ‘command_list’ signal_list
When a script is sent any of the signals in signal_list, trap executes the commands in command_list.
The signal list can contain the integer values or names (without SIG prefix) of one or more signals – the ones
used with the kill command.

Example: To remove all temporary files named after the PID number of the shell:

trap ‘rm $$* ; echo “Program Interrupted” ; exit’ HUP INT TERM trap is a signal handler. It first removes all
files expanded from $$*, echoes a message and finally terminates the script when signals SIGHUP (1), SIGINT
(2) or SIGTERM(15) are sent to the shell process running the script.

A script can also be made to ignore the signals by using a null command list.
Example:
trap ‘’ 1 2 15

Programs
1)
#!/bin/sh
IFS=“|”
While echo “enter dept code:\c”; do
Read dcode
Set -- `grep “^$dcode”<<limit
01|ISE|22
02|CSE|45
03|ECE|25
04|TCE|58
limit`
Case $# in

DON BOSCO CHITRADURGA Page 88


UNIX 2023
3) echo “dept name :$2 \n empid:$
3\n” *) echo “invalid
code”;continue
esac
done
Output:
$valcode.sh
Enter dept code:88
Invalid code
Enter dept code:02
Dept name : CSE
Emp-id :45
Enter dept code:<ctrl-c>

2)
#!/bin/sh
x=1
While [$x –le 10];do
echo “$x”
x=`expr $x+1`
done
#!/bin/sh
sum=0
for I in “$@” do
echo “$I”
sum=`expr $sum + $I`
done
Echo “sum is $sum”

3)
#!/bin/sh
sum=0
for I in `cat list`; do
echo “string is $I”
x= `expr “$I”:’.*’`
Echo “length is $x”
Done

4)
This is a non-recursive shell script that accepts any number of arguments and prints them
in a reverse order.
For example if A B C are entered then output is C B A.
#!/bin/sh
if [ $# -lt 2 ]; then
echo "please enter 2 or more arguments"
exit
fi
for x in $@
do
y=$x" "$y
done echo
"$y"

Run1:
[root@localhost shellprgms]# sh sh1a.sh 1 2 3 4 5 6 7
7654321

Run2: [root@localhost shellprgms]# sh ps1a.sh this is an argument


argument an is this

DON BOSCO CHITRADURGA Page 89


UNIX 2023
5)The following shell script to accept 2 file names checks if the permission for these files are
identical and if they are not identical outputs each filename followed by permission.
#!/bin/sh
if [ $# -lt 2 ]
then
echo "invalid number of arguments"
exit
fi
str1=`ls -l $1|cut -c 2-10`
str2=`ls -l $2|cut -c 2-10`
if [ "$str1" = "$str2" ]
then
echo "the file permissions are the same: $str1"
else
echo " Different file permissions "
echo -e "file permission for $1 is $str1\nfile permission for $2 is $str2"
fi

Run1:
[root@localhost shellprgms]# sh 2a.sh ab.c xy.c
file permission for ab.c is rw-r--r--
file permission for xy.c is rwxr-xr-x

Run2:
[root@localhost shellprgms]# chmod +x ab.c
[root@localhost shellprgms]# sh 2a.sh ab.c xy.c
the file permissions are the same: rwxr-xr-x

6)This shell function that takes a valid directory name as an argument and recursively
descends all the subdirectories, finds the maximum length of any file in that hierarchy
and writes this maximum value to the standard output.
#!/bin/sh
if [ $# -gt 2 ]
then
echo "usage sh flname dir"
exit
fi
if [ -d $1 ]
then
ls -lR $1|grep -v ^d|cut -c 34-43,56-69|sort -n|tail -1>fn1
echo "file name is `cut -c 10- fn1`"
echo " the size is `cut -c -9 fn1`"
else
echo "invalid dir name"
fi

Run1:
[root@localhost shellprgms]# sh 3a.sh
file name is a.out
the size is 12172

DON BOSCO CHITRADURGA Page 90

You might also like