Unix Full Notes-1
Unix Full Notes-1
Unix Full Notes-1
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
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.
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.
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.
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.
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
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
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:
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]:
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 :
$ date [ enter]
Thu Mar 16 11:22:33 IST 2023
$_
The date command instructs the machine to display the current date and time.
$ tput clear
The screen clears and the prompt and cursor are positioned at the top-left corner.
$ cal 3 2023
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
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
$ 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).
$ 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:
$ 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.
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.
And then evaluate the value of this variable with the echo command and a $-prefixed variable name:
$ 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.
The most popular GNU/Linux flavours include Red Hat, Caldera, SuSE, Debian and Mandrake.
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.
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.
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.
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.
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.
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.
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?
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)
$ 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:
The easiest way of knowing the location of an executable program is to use the type command:
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.
$ 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.
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
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:
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
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.
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.
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.
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.
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
Example:
$ whatis cp
cp cp(1) - copy files
Keystroke or Function
command
[Ctrl-h] Erases text
[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
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
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.
$ 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
$ date +%h
Mar
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.
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:
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 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
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
obase = 2
14
1110
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
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:
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:
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 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:
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:
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:
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:
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
Command Action
+ Prints next message
- Prints previous message
N Prints message numbered N
h Prints headers of all messages
dN Deletes message N
$ 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.
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)
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)
$ 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
$ tty
/dev/pts/10
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],
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?
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
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 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.
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:
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
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.
ABSOLUTE PATHNAMES
Many UNIX commands use file and directory names as arguments, which are presumed to exist in the
current directory.
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.
$ /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 /.
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
Recursive Listing (-R) : The –R option lists all files and subdirectories in a directory tree.
$ ls –R
Options to ls
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?
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].
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
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.
$ 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.
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 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 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:
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:
a. cp options
$ cp –i chap01 unit1
cp: overwrite unit1 (yes / no) ? y
A y at this prompt overwrites the file, any other response leaves it uncopied.
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:
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.
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:
a. rm Options
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
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.
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
$ 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
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:
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 *
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.
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
$ 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.
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 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.
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.
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
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
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:
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
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:
But to extract files from a .tar.gz file, you must first use gunzip to decompress the archive and then run tar:
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
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:
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:
$ unzip archive.zip
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.
10marks
Note: any questions can be asked in combination of 2 or 3 questions as mentioned above 5 marks
for 10 marks.
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
For example,
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:
File Permissions
UNIX follows a three-tiered file protection system that determines a file’s access rights. It is displayed
in the following format:
For Example:
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
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:
a. Relative Permissions
chmod only changes the permissions specified in the command line and leaves the other permissions
unchanged. Its syntax is:
chmod takes an expression as its argument which contains some letters and symbol:
The command assigns (+) execute (x) permission to the user (u), other permissions remain unchanged.
Let initially,
-rwxr-xr-x 1 kumar metal 1906 sep 23:38 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)
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.
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.
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
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)
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.
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
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
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.
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].
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.
There are other methods of inputting text. To append text to the right of the cursor position, we use
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.
These two commands are suitable for converting code to comment lines in a C program.
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.
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
To replace multiple characters, use a repeat factor. 3s replaces three with new text.
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
:w [Enter]
You can now continue your editing work normally, but make sure that you execute this command regularly.
: x [Enter]
“sometext:, 8 lines, 303 characters
:q [Enter]
$_
Saves lines 10 through 50 to the file n2words.pl. You can save a single line as well:
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:
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.
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.
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
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
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
d. Joining Lines
J to join the current line and the line following it
4J joins following 3 lines with current line
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.
: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
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.
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.
Examples:
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]
$ cp $HOME/prog_sources/*.{c,java} .
$ cp /home/srm/{project,html,scripts/* .
The above command copies all files from three directories (project, html and scripts) to the current directory.
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\*
$ 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:
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.
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
Examples:
Assuming file2 doesn’t exist, the following command redirects the standard output to file myOutput and the
standard error to file myError.
To redirect both standard output and standard error to a single file use:
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
/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
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.
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.
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.
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
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?
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.
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.
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.
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
$ ps –e
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.
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.
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.
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.
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.
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.
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.
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 &
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 &
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:
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>.
$ kill 123
To kill several processes whose PIDs are 123, 342, and 73 use,
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])
$ bg
$ jobs
[3] + Running grep ‘director’ emp.dat &
[2] - Running sort employee.dat > sortedlist.dat &
[1] Suspended wc –l hugefile.txt
$ 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)
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
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
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
$ 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
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.
$ 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
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.
To output Linux-commands result to file. Note that If file already exist, it will be overwritten else new file is
created.
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)
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
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
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.
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.
#! /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:
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.
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.
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
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
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:
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
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
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
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.
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
Output2:
$emp1.sh
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
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.
Example:
$ ls –l emp.lst
-rw-rw-rw- 1 kumar group 870 jun 8 15:52 emp.lst
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:
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.
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
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:
Example2:
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.
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
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:
The commands enclosed by do and done are executed repadetedly as long as condition is true.
Example:
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
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:
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;
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:
Example: emp.sh
#! /bin/sh
for pattern in “$@”; do
grep “$pattern” emp.lst || echo “Pattern $pattern not
found”
done
Output:
Example1:
$basename /home/user1/test.pl
Ouput:
test.pl
Example2:
$basename test2.doc doc
Ouput:
test2
Example 1:
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
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
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`
Example:
mailx kumar << MARK
Your program for printing the invoices has been
executed on `date`.Check the print queue
The updated file is
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
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
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
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