Ilovepdf Merged
Ilovepdf Merged
Ilovepdf Merged
Operating Systems
The structure of a Computer system can be visualized as having four basic components:
1. Hardware – Provides basic computing resources - CPU, memory, I/O devices
2. Operating system – Controls and coordinates use of hardware among various applications and users
3. Application programs – Define the ways in which the system resources are used to solve the computing
problems of the users
4. Users – Anybody who desires work to be done by a computer system. People, machines, other computers
Types of OS
OS are classified into following different types depending on their capability of processing.
These are simple operating system designed to manage one task at a time ,for use by a single user for a
standalone single computer for performing a single task .
These OS allow execution of more than one task or process concurrently by dividing the processor time amongst
different tasks.
Multi-programming OS:
These OS allow more than one programs to run at the same time .
These are designed to respond to an event within a predetermined time. These operating systems are used to
control processes
Embedded OS:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 1/3
8/10/2021 iON| Digital Learning
Embedded in a device in the ROM. They are specific to a device and are less resource intensive.
Function Of OS
The OS performs basic tasks such as controlling and allocating memory, prioritizing system requests, controlling
input and output devices, facilitating networking, and managing files
Introduction to UNIX OS
Supports C, Fortran, Basic, Pascal, COBOL, Lisp, Prolog, Java, Ada compilers
The UNIX operating system has a layered architecture having three main components
• Kernel
• Shell
• Utilities
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 2/3
8/10/2021 iON| Digital Learning
Kernel is a collection of programs mostly written in C which allocate the system resources and coordinate all the
details of the computer's internals.
Functions of Kernel:
It allocates time and memory to programs and handles the file store and communications
Interacts directly with the hardware through device drivers
Provides sets of services to programs
Manages memory, controls access, maintains file system, handles interrupts, allocates resources of the
computer
System calls
The system calls are functions used in the kernel itself. UNIX system calls are used to manage the file system,
control processes, and to provide interpocess communication.
File structure related calls -For example create, open,read, write,lseek,dup etc.
Process Related calls -For example fork,exec,wait,exit etc
Inter process related calls - For example pipe,msgget,msgsnd etc.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 3/3
8/10/2021 iON| Digital Learning
Ordinary Files
Directory Files
Special Files (device, pipe, fifo, socket).
Absolute path name - file location is determined with respect to the root.
Relative path name - file location is determined with respect to the current directory.
The Unix File System (UFS) looks hierarchical but it is actually a directed a-cyclic graph because files can be
shared.
/home – It holds user’s home directories. In other UNIX systems, this can be /usr directory.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 1/3
8/10/2021 iON| Digital Learning
/bin – It holds many of the basic Linux programs; bin stands for binaries, files that are executable.
/dev – It holds device files. All info sent to /dev/null is thrown into trash. Your terminal is one of the /dev/tty
files.
Unix divides physical disks into logical disks called partition which is comprising of a set of consecutive
cylinders.
The UFS resides on a single partition.
Each file system contains four blocks :
a) boot block : This block is located in the first few sectors of a file system. It contains the initial
bootstrap program used to load the operating system.Typically, the first sector contains
a bootstrap program that reads in a larger bootstrap program from the next few sectors,
and so forth.
b) super block : This block describes the state of the file system such as the total size of the partition,
the block size, pointers to a list of free blocks, the inode number of the root directory,
magic number or file signature (the first few bytes of a file which are unique to a
particular file type), etc.
c) inode list : A linear array of inodes (index nodes). This is a data structure which describes the
attributes of a file. There is a one to one mapping of files to inodes and vice versa.
An inode is identified by it's "inode number". Users uses file names to refer to a file but
Unix represent files in terms of inodes.
For each file created in the system, an inode is also created. Inode is a disk file record of 64 bytes that maintains
the permanent attributes of a file.
An inode is permanent and it exists until the corresponding file is removed from the system.
Whenever a file is opened, its inode is brought into main memory. The active inode is kept there until the file is
closed and is used to locate the beginning of an open file on disk and to verify that every I/O request comes from
a valid user as per specified access permissions.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 3/3
8/10/2021 iON| Digital Learning
Objective
This module is to have an understanding of different user types in UNIX and their access rights.
Course Content
User types
File access permissions
Changing file access permissions using symbolic mode
Changing file access permissions using octal mode
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 1/4
8/10/2021 iON| Digital Learning
Read
Write
Execute
These three modes or permissions have different meaning for a file and a directory;
Symbolic mode
Absolute mode
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 2/4
8/10/2021 iON| Digital Learning
chmod command options:
With symbolic representation, permission set can be added, deleted, or specified using the operators listed in the following table:
Example:
Let us assume there is a file name "abc" , whose initial permissions are only read permission to user and group and others. We will
provide full permissions to all three types of users.
Let us verify the existing file permissions using ls -l command as below and observe the next steps, like changing the permissions and
verifying the changes.
chmod u+wx,g+wx abc , adds write and execute permission to user and group to the file abc
chmod g-x abc, removes execute permission from group from the file abc.
chmod o=r-- abc, provides read permission and removes write and execute permission from others for file abc
The second way to modify permissions with the chmod command is to use a number to specify each set of permissions for the file.
Each permission is assigned a value, as the following table shows, and the total of each set of permissions provides a number for that
set.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 3/4
8/10/2021 iON| Digital Learning
Examples: Observe the following sequence of command execution statements and analyze the output
If we observe the output, initially owner have read and write permissions and group and others have only read permission
2. Let us remove all the permission from all users for abc file, as below
chmod 0 abc
Let us verify the changed permissions
ls -l abc
---------- 1 735873 oinstall 0 Feb 7 12:37 abc
Yes all permissions are taken away from all users on the file abc
3. To provide all permissions to the user, read permission to group and write and execute permissions for Others,
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 4/4
8/10/2021 iON| Digital Learning
echo
Syntax:
echo [OPTION]... [STRING]...
Example:
Good Morning
passwd command
The passwd command is used to change passwords for user accounts. A normal user may only
change the password for his or her own account, while the superuser may change the password for
any account.
Options:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 1/7
8/10/2021 iON| Digital Learning
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 2/7
8/10/2021 iON| Digital Learning
uname command
Options:
uname command:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 3/7
8/10/2021 iON| Digital Learning
Used to get the information about the current system (the name, version and other details)
$uname
$ uname -s
$ uname -r
who command:
who command prints information about all users who are currently logged in.
Syntax:
who [OPTION]... [ FILE ] [ am i ]
find command:
find is one of the powerful utility of Unix (or Linux). It can search
the entire file-system to locate files and directories according to the specific
search criteria. Other than searching files in can also perform actions(executing
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 4/7
8/10/2021 iON| Digital Learning
$ find [option] [action]
Examples :
1 . File Type based Search:
>> The following command finds the file named Abc in current(.) directory and
all its sub-directories
Searches for the file Abc with any extension only in directories Dir1 and Dir2
4 . Size Based Search : Using find files can be searched based on its size .
>> To find the files with size more than 10Mb the command will be
./Abc.dat
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 5/7
8/10/2021 iON| Digital Learning
./Abc
7 . Perform Action on the search result : Using exec option any action (command)
can be executed on the search result.
>> To remove all the files having extension .swp from the current directory the
command will be :
bc command:
That means typing the command bc on a Unix command prompt and entering a mathematical
expression, such as (1 + 4) * 2 , whereupon 10 will be the output
While used with files , bc starts by processing code from all the files listed on the command line in
the order listed. After all files have been processed, bc reads from the standard input. All code is
executed as it is read.
Example:
$ echo "2+5" | bc
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 6/7
8/10/2021 iON| Digital Learning
tty command:
Prints the file name of the terminal connected in standard input.
Syntax : tty [OPTION]...
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 7/7
8/10/2021 iON| Digital Learning
One of the first end-user programs hosted on the system and standardized in UNIX-based systems
ever since is the vi editor. ed is a line editor for the UNIX operating system.
vi Editor:
vi (pronounced "vee-eye") is short for "vi"sual editor. It is a visual editor used to create and edit
text files.
It displays a window into the file being edited that shows 24 lines of text. vi lets you add, change
and delete text, but does not provide formatting capabilities like centering lines or indenting
paragraphs.
vi []
For cursor movement within the vi editor, we can not use the mouse . We have to use key
commands. To use a key command, user has to be in the command mode. To go to command mode,
press the Esc key.
The new or modified file is saved when user exits from vi. But it is also possible to exit without
saving the file.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 1/4
8/10/2021 iON| Digital Learning
To use a command to exit from vi , after pressing the Esc key ,colon : is to be pressed.
The cursor moves to bottom of screen whenever a colon (:) is typed. This type of command is
completed by hitting the (or ) key.
:q! quit vi even though latest changes have not been saved for
this vi call
i
insert text before the cursor,until is hit
I
insert text at beginning of current line,
until hit
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 2/4
8/10/2021 iON| Digital Learning
Ncc change (replace) the next N lines, starting with the current line,
or stopping when is hit
cNc
D delete the remainder of the line, starting with current cursor position
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 3/4
8/10/2021 iON| Digital Learning
Nyy copy (yank, cut) the next N lines, including the current line, into the buffer
p put (paste) the line(s) in the buffer into the text after the current line
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 4/4
8/10/2021 iON| Digital Learning
touch command
Touch command is used in Unix to create an empty file.
Touch command is used to change the timestamps (access time and modification time of a file).
cat command
Use of cat command:
'cat' command is used to display a file content.
Syntax:
$ cat data
This is an unix file
This is line two
$
cat with single redirection(cat >filename) , creates a new file if no file with the given file name
already exists and overrites the file if a file with given file name already exists.
cat with double redirection (cat >>filename) appends the content given through standard input to the
file.
cp command
cp command is used to copy one file's content to another file.
Syntax: $ cp [source filename] [destination filename]
Switches:
1. cp -i [sourcefilename] [destination filename] This command copies the content of a file
interactively.
mv command
mv command is used for:
1. for renaming a file in same directory.
2. for moving a file to a different directory.
Syntax:
$ mv [sourcefilename] [destinationfilename]
$ mv [source_directory] [destination_directory]
Directory Commands:
Every item in the UNIX filesystem tree is either a file, or a directory. A directory is like a file folder.
A directory can contain files, and other directories. A directory contained within another is called
the child of the other. A directory in the filesystem tree may have many children, but it can only
have one parent. A file can hold information, but cannot contain other files, or directories.
The place in the file system tree where an user is located is called the current
working directory (which can be listed using pwd command).
ls Command
Lists the contents of any directory (current directory if no directory specified).
Sorts entries without option
$ ls [option(s)] [filename]
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 2/3
8/10/2021 iON| Digital Learning
Syntax :
Syntax:
Example: To show only lines common between file1 and file2, command is
diff - This command is used to compare two files line by line. The output indicates how the lines in
each file are different, and the steps that will be involved in changing file1 to file2.
The change commands are in the format [range][acd][range]. The range on the left may be a line
number or a comma-separated range of line numbers referring to file1, and the range on the right
similarly refers to file2. The character in the middle indicates the action i.e. add, change or delete.
‘LaR’ – Add lines in range ‘R’ from file2 after line ‘L’ in file1.
‘FcT’ – Change lines in range ‘F’ of file1 to lines in range ‘T’ of file2.
‘RdL’ – Delete lines in range ‘R’ from file1 that would have appeared at line ‘L’ in file2
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 3/3
8/10/2021 iON| Digital Learning
USAGE OF WinSCP
Introduction to WinSCP
WinSCP (Windows Secure CoPy) is a popular , free and open-source Secure File Transfer application (SFTP) for Windows computers .It is used to transfer files securely
between remote server and local computer and vice verse. It includes support for SCP file transfers, and has the ability to create secure tunnels via SSH. Secure FTP
(SFTP) is similar to FTP, but with SFTP the entire session is encrypted, so that passwords are never sent in the clear text , and are therefore much less vulnerable to
interception. That is ,for transferring files securely, we can use Secure copy or SCP protocol, it uses Secure Shell (SSH), which encrypts the content before transferring
data over network. It runs over TCP port 22 by default.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 1/5
8/10/2021 iON| Digital Learning
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 2/5
8/10/2021 iON| Digital Learning
Following are the Steps to be performed for Downloading file/Directory from Server
Step - A1 : Select the file to transfer in the Remote pane,
Step - A2 : Right click → Select Copy or move option
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 3/5
8/10/2021 iON| Digital Learning
Step - A3 : Select the destination folder in the local machine(can be changed by selecting Browse button )
Following are the steps to be followed for uploading files to Remote Server
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 4/5
8/10/2021 iON| Digital Learning
Step - B3 : Select the desired location from drop down list and select copy
All the session information can be saved for future use and faster access.
The information can be saved in following ways :
>> Server IP
>> User name,
>> Password(Not recommended)
>> Communication protocol
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 5/5
8/10/2021 iON| Digital Learning
Filters
Filter is a programm in unix . It takes its input from another program, performs some operation on
that input, and writes the result to the standard output. Thus the common use of filters is to modify
or restructure output.
tr – translate
head
This command list the beginning of a file to standard output. The default is 10 lines, but a different
number can be specified. The command has a number of options.
Syntax:
Options:
-c Prints the first N bytes of file; with leading -, prints all but the last N bytes of the file.
-n Prints first N lines; with leading - print all but the last N lines of each file.
Example: To display the first 10 lines of the file myfile.txt. $head myfile.txt
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 1/10
8/10/2021 iON| Digital Learning
tail
List the (tail) end of a file to stdout. The default is 10 lines, but this can be changed with the -n
option. Commonly used to keep track of changes to a system log-file, using the -f option, which
outputs lines appended to the file.
Syntax:
Example:
$tail myfile
more
more command allows to view text files or other output in a scrollable manner. When can command
is used to view a very long file, all the output scrolls off the top of your screen and only the last
page can be viewed. more command solves this problem by allowing the output of cat command
one screenful of data at a time.
Syntax:
Options:
-num This option specifies an integer which is the screen size (in lines).
-d more will prompt the user with the message "[Press space to continue, 'q' to quit.]" and will
display "[Press 'h' for instructions.]" instead of ringing the bell when an illegal key is pressed.
-l more usually treats ^L (form feed) as a special character, and will pause after any line that
contains a form feed. The -l option will prevent this behavior.
-p Do not scroll. Instead, clear the whole screen and then display the text.
tr
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 2/10
8/10/2021 iON| Digital Learning
Syntax:
Translate, squeeze, and/or delete characters from standard input, writing to standard output.
Options:
t. : truncates set1
$ echo "Hello"|tr -c e a
Output : aeaaaa
In the above example , except the character “e” other characters are replaced by a
Example: Squeez , we can squeeze more than one occurrence of continuous characters with single
occurrence.
sort
sort command reorders the lines of a file in ascending or descending order.
Syntax:
Options:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 3/10
8/10/2021 iON| Digital Learning
-n sort numerically
Examples:
Below examples will help you to understand sort used with different options:
Example 1:
To sort on the 2nd field of file named “list” we have to use the below command:
Output:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 4/10
8/10/2021 iON| Digital Learning
19
20
49
00
If we sort it as below:
$sort list
Output is :
19
20
200
49
$sort –n list
Output:
19
20
49
200
Output :
200
49
20
19
Syntax:
$sort –u filename
Unix
Unix
Linux
Linux
Solaris
Axis
Axis
$sort –u list
Output:
Unix
Linux
Solaris
Axis
uniq
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 6/10
8/10/2021 iON| Digital Learning
uniq command is used to suppress the duplicate lines from a file. It discards all the successive
identical lines except one from the input and writes the output.
Syntax:
Options:
The default behavior of the uniq command is to suppress the duplicate line. Note that, you have to
pass sorted input to the uniq, as it compares only successive lines.
If the lines in the file are not in sorted order, then use the sort command and then pipe the output to
the uniq command.
Count of lines:
The -c option is used to find how many times each line occurs in the file. It prefixes each line with
the count.
You can print only the lines that occur more than once in a file using the -d option. The -D option
prints all the duplicate lines.
the -f option is used to skip the first N columns in comparison. Here the fields are delimited by the
space character.
cut
This command is used for text processing. You can use this command to extract portion of text from
a file by selecting columns.
Syntax:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 7/10
8/10/2021 iON| Digital Learning
The following example displays 2nd character from each line of a file test.txt.
Range of characters can also be extracted from a file by specifying start and end position delimited
with -.
The following example extracts first 3 characters of each line from a file called test.txt
Either start position or end position can be passed to cut command with -c option.
Following example extracts from 3rd character to end of each line from test.txt file.
The option -d specifies what delimiter that is used in the input file.
The following example displays only first field of each lines from /etc/passwd file using the field
delimiter: (colon). In this case, the 1st field is the username.
paste
This is the command for merging together different files into a single, multi-column file. In
combination with cut, useful for creating system log files.
Syntax:
join
This utility allows merging two files in a meaningful fashion, which essentially creates a simple
version of a relational database.
The command join operates on exactly two files, but pastes together only those lines with a
common tagged field (usually a numerical label), and writes the result to standard output.
The files to be joined should be sorted according to the tagged field for the matchups to work
properly.
Example:
100 Shoes
200 Laces
300 Socks
$cat file2
100 $40.0
200 $1.00
300 $2.00
Pipe
In unix , you can connect two commands together so that the output from one program becomes the
input of the next program. Two or more commands connected in this way form a pipe. In shell the
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 9/10
8/10/2021 iON| Digital Learning
Purpose of Pipes :
Using pipe you can construct powerful unix command lines by combining basic unix commands.
UNIX commands are powerful; however by using pipe you can combine them together, to
accomplish complex tasks with ease.
Through the standard output of one command (the command to the left of the pipe) gets sent as
standard input to another command (the command to the right of the pipe). Pipe functions in a
similar manner like the output redirection in UNIX (using > symbol to redirect the standard output
of a command to a file. However, the pipe is different because it is used to pass the output of a
command to another command, not a file.
Example:
$ cat apple.txt | wc
3 4 21
In this example, the contents of the file apple.txt are sent through pipe to wc (word count)
command. The wc command then does its job and counts the lines, words, and characters in the file.
You can combine many commands with pipes on a single command line. Here's an example where
the characters, words, and lines of the file apple.txt is sent to wc and then the output of wc mailed to
[email protected] with the subject line "The count."
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 10/10
8/10/2021 iON| Digital Learning
awk
awk is a scripting language which is used for processing or analyzing text files.
awk is used for grouping of data based on either a column or field, or on a set of columns.
It derives its name from the first letter of the last name of its three authors namely Alfred V. Aho, Peter J.Weinberger
and Brian W. Kernighan.
awk can be used for reporting data in a useful manner. It searches one or more files to see if they contain lines that
match specified patterns and then perform associated actions. awk is an advanced filter.
Simple awk Filtering
Syntax of awk:
By default, awk prints all lines of a file, so to print every line of above created file , use below command:
~$ awk '{print}' awk_file
Name,Marks,Max_Marks
Peter,200,1000
Sam,500,1000
Greg,1000
Abharam,800,1000
Henry,600,1000
Peter,400,1000
BEGIN and END blocks are helpfull in displaying information before and after executing actual awk script.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 1/7
8/10/2021 iON| Digital Learning
BEGIN block is evaluated before awk starts processing the actual awk script; it’s an excellent place to initialize the
FS (field separator) variable, print a heading, or initialize other global variables.
Declaring variables.
Initialization variables for doing increment/decrements operations in main AWK code.
Printing Headings/info before actual AWK code output.
END block is evaluated after all the lines in the awk script have been processed.
Typically, the END block is used to perform final calculations or print summaries that should appear at the end of the
output stream.
awk tool is mainly used for reporting some data in useful manner. Without these BEGIN and END
blocks the output will be meaningless.
Consider db.txt which contains below data:
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
This is a block of code which is executed before executing actual awk script.
BEGIN block Syntax
awk ‘BEGIN{awk initializing code}{actual AWK code}’ filename.txt
Example: Print a meaning full info before actual AWK output.
~$ awk ‘BEGIN{print “########################\nThis is the output of
filtered
data\n########################”}{print $0}’ db.txt
Output:
##########################
This is the output of filtered data
##########################
Jones 2143 78 84 77
Gondrol 2321 56 58 45
RinRao 2122234 38 37
Edwin 253734 87 97 95
Dayan 24155 30 47
This is the block which is executed after executing all the awk code.
Example:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 2/7
8/10/2021 iON| Digital Learning
Output:
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
#########################
Completed printing filtered data
#########################
Example:
Output:
#########################
This is the output of filtered data
#########################
Jones 21 78 84 77
Gondrol 23 56 58 45
RinRao 25 21 38 37
Edwin 25 87 97 95
Dayan 24 55 30 47
########################
Completed printing filtered data
awk is supplied with good number of built-in variables which comes in handy when working with data files. We will
see usages of awk built-in variables with one or two examples . These variable are used to format the output of an
awk command.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 3/7
8/10/2021 iON| Digital Learning
In order to make it simple we can divide above inbuilt variables in to groups on basis of their operations.
Group1: FS(input field separator), OFS(Output Field Separator)
Group2: RS(Row separator) and ORS(Output Record Separator)
Group3: NR, NF and FNR
Group4: FILENAME variable
This variable is useful in storing the input field separator. By default AWK can understand only spaces, tabs as input
and output separators. But if your file contains some other character as separator other than these mention one’s, awk
cannot understand them.
For example UNIX password file which contain ‘:’ as a separator. So in order to mention the input filed separator we
use this inbuilt variable. We will see what issue we face if we don’t mention the field separator for our db.txt.
Output:
John,29,MS,IBM,M,Married
Barbi,45,MD,JHH,F,Single
Mitch,33,BS,BofA,M,Single
Tim,39,Phd,DELL,M,Married
Lisa,22,BS,SmartDrive,F,Married
Example:
Display only 1st and 4th column and with $ as field separator for the output .
~$ awk ‘BEGIN{FS=”,”;OFS=” $ “}{print $1,$4}’ db.txt
Output:
John $ IBM
Barbi $ JHH
Mitch $ BofA
Tim $ DELL
Lisa $ SmartDrive
Note: Space is give before and after $ in OFS variable to show better output.
RS (Row separator)
Row Separator is helpful in defining separator between rows in a file. By default awk takes row separator as new line.
We can change this by using RS built-in variable.
Example:
Convert a sentence to a word per line. We can use RS variable for doing it.
~$ echo “This is how it works” | awk ‘BEGIN{RS=” ”}{print $0}’
Output:
This
is
how
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 4/7
8/10/2021 iON| Digital Learning
it
Works
Example:
Print all the company names in single line which are in 4th column.
~$ awk -F’,’ ‘BEGIN{ORS=” “}{print $4}’ db.txt
Output:
IBM JHH BofA DELL SmartDrive
NF
This variable keeps information about total fields in a given row. The final
value of a row can be represented with $NF.
Output:
5
5
4
5
4
NR
This variable keeps the value of present line number. This will come handy when you want to print line numbers in a
file.
Example:
Print line number for each line in a given file.
~$ awk ‘{print NR, $0}’ abc.txt
Output:
1 Jones 2143 78 84 77
2 Gondrol 2321 56 58 45
3 RinRao 2122234 38 37
4 Edwin 253734 87 97 95
5 Dayan 24155 30 47
This can be treated as cat command -n option for displaying line number for a file
FNR
This variable keeps count of number of lines present in a given file/data. This will come handy when
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 5/7
8/10/2021 iON| Digital Learning
you want to print no of line present in a given file. This command is equivalent to wc -l command.
Example:
Print total number of lines in a given file.
~$ awk ‘END{print FNR}’ abc.txt
Output:
5
FILENAME
This variable contain file awk command is processing.
Example:
Print filename for each line in a given file.
~$ awk ‘{print FILENAME, NR, $0}’ abc.txt
Output:
abc.txt 1 Jones 2143 78 84 77
abc.txt 2 Gondrol 2321 56 58 45
abc.txt 3 RinRao 2122234 38 37
abc.txt 4 Edwin 253734 87 97 95
abc.txt 5 Dayan 24155 30 47
A function is a self-contained computation that accepts a number of arguments as input and returns some value. awk
has a number of built-in functions in two groups: arithmetic and string functions.
Arithmetic Functions
Nine of the built-in functions can be classified as arithmetic functions. Most of them take a numeric argument and
return a numeric value. Below table summarizes these arithmetic functions with some Examples.
Examples:
~$ awk 'BEGIN{
print sqrt(16);
print sqrt(0);
print sqrt(-12);
}'
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 6/7
8/10/2021 iON| Digital Learning
Output:
4
0
nan
Here nan stands for not a valid number.
String Functions
The built-in string functions are much more significant and interesting than the numeric functions. Because awk is
essentially designed as a string-processing language, a lot of its power derives from these functions. Below table lists
the string functions found in awk. awk's Built-In String Functions
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 7/7
8/10/2021 iON| Digital Learning
Shell Programming
The UNIX shell program interprets user commands, which are either directly entered by the user, or
which can be read from a file called the shell script or shell program. Shell scripts are interpreted,
not compiled. The shell reads commands from the script line by line and searches for those
commands on the system.
Shell script
A good shell script will have comments, (a statement preceded by a pound sign, #) , describing the
purpose of the statement.
In a script we can use conditional tests, such as value A is greater than value B, loops or iterative
statements to execute some steps repetitively or to navigate through a list of data or records and do
processing. We can use files to read and store data. Can use variables to read and store data. A script
may include functions also.
When a script is executed, the shell reads the commands one by one and executes them .
We can create the simple shell script file by using vi editor or cat command like,
$ vi test.sh
Below mentioned shebang statement should be the first statement in the shell script as it tells the
system that the commands mentioned in the shell script are to be executed by the shell /bin/sh
#!/bin/sh
Consider the shell script with just two commands pwd & ls.
$cat test.sh
#!/bin/bash
pwd
ls
Shell scripts are basically used for automating processes that we repeat at the prompt .
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 1/20
8/10/2021 iON| Digital Learning
Automating administration tasks such as adding new users, removing obsolete users etc
Find out all logged in users and what they are doing.
Find out if all necessary network services are running or not. For example if web server failed
then send an alert to system administrator via a pager or an email.
Find out all failed login attemp. If login attempt are continued repeatedly from same network
IP, automatically block all those IPs accessing your network/service via firewall.
$ sh filename
Or
$ ./filename
In this case we have to modify the file access permissions of the shell script before execution.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 2/20
8/10/2021 iON| Digital Learning
Arithmetic Operators.
Relational Operators.
Boolean Operators.
String Operators.
Example:
#!/bin/sh
val=`expr 2 + 2`
Output:
$ Total value : 4
There must be spaces between operators and expressions for example 2+2 is not correct,
where as it should be written as 2 + 2.
Complete expression should be enclosed between ``, called inverted commas to execute expr
command correctly.
Arithmetic Operators
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 3/20
8/10/2021 iON| Digital Learning
It is very important to note here that all the conditional expressions would be put inside square
braces with one spaces around them, for example [ $a == $b ] is correct where as [$a==$b] is
incorrect.
Relational Operators
Below are relational operators which are specific to numeric values. These operators would not
work for string values unless their value is numeric.
For example, following operators would work to check a relation between 10 and 20 as well as in
between "10" and "20" but not in between "ten" and "twenty".
-eq Check if the values of 2 operands are equal or not, [ $a -eq $b ] is false
if yes then condition becomes true.
-ne Check if the values of 2 operands are equal or not, [ $a -eq $b ] is true
if values are not equal then condition becomes true.
-gt Check if the value of left operand is greater than [ $a -gt $b ] is false
the value of right operand, if yes then condition
becomes true.
-lt Check if the value of left operand is less than the [ $a -lt $b ] is true
value of right operand, if yes then condition
becomes true.
-ge Check if the value of left operand is greater than or [ $a -ge $b ] is false
equal to the value of right operand, if yes then
condition becomes true.
-le Check if the value of left operand is less than or [ $a -le $b ] is true
equal to the value of right operand, if yes then
condition becomes true.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 4/20
8/10/2021 iON| Digital Learning
It is very important to note here that all the conditional expressions would be put inside square
braces with one spaces around them, for example [ $a <= $b ] is correct where as [$a <= $b] is
incorrect.
Boolean Operators
String Operators
These are string operators. Assume variable a holds "abc" and variable b holds "efg" then:
Assume a variable file holds an existing file name "test" whose size is 100 bytes and has read, write
and execute permission on:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 5/20
8/10/2021 iON| Digital Learning
Symbol used to replace or represent one or more characters. Wildcards or wild characters are either
asterisk (*), which represent one or more characters or question mark (?), which represent a single
character.
Wild card
Meaning Examples
/Shorthand
Matches any string or group of $ ls * will show all files
*
characters.
$ ls a* will show all files whose first
name is starting with letter 'a'
$ ls *.c will show all files having
extension .c
$ ls ut*.c Will show all files having
extension .c but file name must
begin with 'ut'.
? Matches any single character. $ ls ? will show all files whose names
are 1 character long
$ ls fo? will show all files whose names
are 3 character long and file
name begin with fo
[...] Matches any one of the enclosed $ ls Will show all files beginning
characters [abc]* with letters a,b,c
Example:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 6/20
8/10/2021 iON| Digital Learning
$ ls /bin/[a-c]*
Output:
The Metacharacters
Unix Shell provides various metacharacters which have special meaning while using them in any
Shell Script and causes termination of a word unless quoted.
Example:
? Matches with a single character while listing files in a directory and an * would match more than
one characters.
Here is a list of most of the shell special characters (also called metacharacters):
A character may be quoted (i.e., made to stand for itself) by preceding it with a \.
Example:
Hello ./test.sh: line 2: Word: command not found shell returned 127
Hello; Word
The $ sign is one of the metacharacters, so it must be quoted to avoid special handling by the shell:
I have $1200
Quote Description
Single quote All special characters between these quotes lose their special meaning.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 7/20
8/10/2021 iON| Digital Learning
Double quote Most special characters between these quotes lose their special meaning
with these exceptions:
\$
\'
\"
\\
Backslash Any character immediately following the backslash loses its special
meaning.
Back Quote Anything in between back quotes would be treated as a command and
would be executed.
Putting a backslash in front of each special character is tedious and makes the line difficult to read:
There is an easy way to quote a large group of characters. Put a single quote ( ') at the beginning and
at the end of the string:
Any characters within single quotes are quoted just as if a backslash is in front of each character. So
now this echo command displays properly.
If a single quote appears within a string to be output, you should not put the whole string within
single quotes instead you would precede that using a backslash (\) as follows:
Try to execute the following shell script. This shell script makes use of single quote:
VAR=ZARA
So this is not what you wanted to display. It is obvious that single quotes prevent variable
substitution. If you want to substitute variable values and to make invert commas work as expected
then you would need to put your commands in double quotes as follows:
VAR=ZARA
Double quotes take away the special meaning of all characters except the following:
Putting any Shell command in between back quotes would execute the command
Syntax: var=`command`
Example:
Following would execute date command and produced result would be stored in DATA variable.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=1499400… 9/20
8/10/2021 iON| Digital Learning
While writing a shell script, there may be situations when you need to adopt one path out of many
available paths. In such cases you need to make use of conditional statements that allow your
program to make correct decisions and perform right actions.
Unix Shell supports conditional statements, which are used to perform different actions based on
different conditions.
If..else statements
We can use “if..else” statement which can be used as decision making statement to select an option
from a given set of options.
if...fi statement
if...else...fi statement
if...elif...else...fi statement
Syntax:
if...fi statement
if [condition]
then
command(s)
fi
if...else...fi statement
if [ condition(s) ] then
command(s)
else
command(s)
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 10/20
8/10/2021 iON| Digital Learning
fi
if...elif...else...fi statement
if [ condition(s) ]
then
command(s)
then
command(s)
else
command(s)
fi
We can use test command as condition of if condition as used in the below script.
$cat if_test.sh
#!/bin/sh
read ans
then
exit
else
fi
case...esac Statement
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 11/20
8/10/2021 iON| Digital Learning
We can use multiple if...elif statements to perform a multiway branch. However, this is not always
the best solution, especially when all of the branches depend on the value of a single variable.
Unix Shell supports case...esac statement which handles exactly this situation, and it does so more
efficiently than repeated if...elif statements.
The interpreter checks each case against the value of the expression until it finds a match. If nothing
matches, goes with the default condition.
Syntax:
case word in pattern1)
;;
pattern2)
;;
pattern3)
;;
esac
Here string word is compared against every pattern until a match is found and the statement(s)
following the match pattern executes. If shell cannot find any match, the case statement exits
without performing any action. When statement(s) part is executed. The command ;; indicates
program flow should jump to the end of the entire case statement.
Example:
#!/bin/sh
COURSE=”DB”
case “$COURSE” in
;;
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 12/20
8/10/2021 iON| Digital Learning
;;
;;
esac
Output:
Oracle is a DB
Iterative Statements/Loop :
Loops are a powerful programming tool that enables you to execute a set of commands repeatedly.
while loop
for loop
until loop
while loop
Here condition is evaluated by shell at the beginning of each iteration. If the resulting value is true,
given command(s) are executed. If condition is false then no command would be executed and
program would jump to the next line after done statement.
Syntax:
while condition
command(s)
done
Example:
a=0
while [ $a -lt 3 ]
do
echo $a
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 13/20
8/10/2021 iON| Digital Learning
a=`expr $a + 1`
done
Output:
until loop
Here condition is evaluated by shell. If the resulting value is false, given command(s) are executed.
If condition is true then no command would be executed and program would jump to the next line
after done statement.
Syntax:
do
command(s)
done
Example:
a=0
until [ ! $a -lt 3 ] do
echo $a a=`expr $a + 1`
done
Output:
for loop
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 14/20
8/10/2021 iON| Digital Learning
For loop operate on lists of items. It repeats a set of commands for every item in a list.
Syntax:
do
done
Example:
for var in 0 1 2 3 4 5
do
echo $var
done
Output:
5
Example:
do
cat ${filename}
done
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 15/20
8/10/2021 iON| Digital Learning
Command "break" causes the termination of a loop and “continue” resumes execution at its top.
String Handling
The expr is quite handy for finding the length of a string and extracting a sub-string:
$ str=”abcdefghijk” ;
$ echo $n
11
expr gave how many times any character (.*) occurs. This feature is very useful in validating data
entry.
Extracting a sub-string:
$ str=”abcdefghijk” ;
gh
Note that there are 6 dots preceding the sequence \(..\). This advanced regular expression signifies
that the first six characters of the string are to be ignored and extraction should start from the 7th
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 16/20
8/10/2021 iON| Digital Learning
character. Two dots inside \(..\) suggests that this extraction is limited to two characters only
(backslashes override the usual interpretation of ‘()’).
$ str="abcdefghijk"
cdefghijk
$ str=”abcdefghijk” ;
Below will give the last occurrence of character 'a' from string str.
6. Command Line Arguments
To make a shell script a generalized script, we should avoid hard coding. User should be able to
provide the values required for processing as input while running the shell script. To facilitate this
we have to use command line arguments.
The statement we write in the command prompt starting with the command followed by list of
arguments is the command line argument.
Example:
A set of shell variables are there to represent the command line arguments.
$0 – represents the command name (first word on command line). For above example “ls”
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 17/20
8/10/2021 iON| Digital Learning
$1 - the first argument of command (second word/symbol ).For the above example dir1
$2 – the second argument to the command. For the above example, dir2
The command-line arguments $1, $2, $3...$9 are also called positional parameters, with $0 pointing
to the actual command/program/shell script and $1, $2, $3, ...$9 as the arguments to the command.
$cat test.sh
#!/bin/sh
Let’s execute the above shell script “test.sh”. Remember to change File access permission of script
to execute
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 18/20
8/10/2021 iON| Digital Learning
To get input from the keyboard, you use the read command. The read command takes input from the
keyboard and assigns it to a variable.
Example: read.sh
#!/bin/sh
read text
Note that “-n” given to the echo command causes it to keep the cursor on the same line; i.e., it does
not output a carriage return at the end of the prompt.
Next, we invoke the read command with “text” as its argument. What this does is wait for the user
ro type something followed by a carriage return (the Enter key) and then assign whatever was typed
to the variable text.
Execution:
$sh read.sh
Let us assume, we have one shell script which requires exactly 2 arguments to execute. Shell script
should throw proper error message in case user has not given exactly two arguments.
Let us have a small shell script "test.sh" to show how to implement validations.
#!/bin/sh
if [ $# -ne 2 ]
then
else
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 19/20
8/10/2021 iON| Digital Learning
$ ./test.sh
$ sh test.sh abc
For scripts where input expected is a file , validations can be incorporated for file tests like whether
the file given as input exists or not, whether it is empty or not, whether it is readable or not and so
on as per requirements.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=149940… 20/20
8/10/2021 iON| Digital Learning
Environment Variables:
Values of some of these variables are set by the system, some can be set by the user, some of them
by the shell, or any program that loads another program.
When a user logs in to the system, the shell undergoes a twostep process called initialization to set
up the environment. This process involves the shell reading two files −/etc/profile and .profile in the
home directory of the user.
The file /etc/profile is maintained by the system administrator of the Unix machine and contains
shell initialization information required by all users on a system.
The file .profile is under the control of the user. User can add as much shell customization
information as he/she wants to this file. The minimum set of information that a user need to
configure includes −
When a process is created, UNIX opens 3 streams stdin/stdout/stderr for basic communication with
respect to the process control terminal. In addition, it knows the current working directory for
performing file I/O.
Each login shell maintains a description of the environment as a table of pointers to strings.
A global shell environment pointer called environ is maintained by UNIX kernel and it can be used
by a process to access its own table
The shell does not directly use this table, but it creates a child process and calls exec() system call to
execute a command program that uses the table inherited from the shell parent. Children inherit the
entire execution environment from the parent.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 1/3
8/10/2021 iON| Digital Learning
Some examples of the environment variables are the USER, LOGNAME, HOME, PATH, PS1,
PS2, TERM MAIL, etc.
The HOME Variable
It specifies an associated directory with every user in a UNIX system. If the HOME variable for the
user Sita contains /usr/sita/stores, every time Sita logs in, she is taken to the directory stores.
The variable HOME is referenced the same way:
$ echo ${HOME}
The PATH Variable
contains a list of all full path-names (separated by a colon) of directories that are to be searched for
an executable program. For example, the command
$PATH=.:/usr/bin:/bin specifies directories to be searched for any executable file or a command file
(current directory, /usr/bin and /bin, in that order).
The PS1 Variable
The system prompt may be changed by setting the value of this variable to the desired prompt:
$ PS1=“Hello>”
Hello> #can be changed only at the UNIX command line, not within a shell script.
The PS2 Variable: prompt string for continued command line (default ‘> ‘).
The LOGNAME Variable
contains user’s login name. Its contents cannot be changed by the user, but can be displayed:
echo “${LOGNAME}”
The TERM Variable
Names the kind of terminal you are using; setting it helps to manage your screen more effectively,
say,
$ TERM=vt100
The PWD Variable
The current working directory can be displayed:
echo “${PWD}”
In fact the whole environment table can be displayed.
$IFS: String of characters which are used as word separators in command line ( space, tab, newline
chars).
The MAIL Variable
Names the standard file where your mail is kept
Some of above variables like HOME and LOGNAME are set automatically each time the user logs
in. The others, however, have to be set. The .profile is used for this purpose as it is executed as soon
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 2/3
8/10/2021 iON| Digital Learning
the user logs in. A sample .profile file would look like:
PATH=.:/bin:/usr/bin
export HOME PATH PS1 MAIL
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 3/3
8/10/2021 iON| Digital Learning
A process in UNIX is a program in execution with definite life-time and well-defined hierarchy.
The context of a process is a snapshot of its current run-time environment that consists of the current state
of the processor registers and
User program image - Process execution on user data structure that represents an application and is
manipulated by its own functions (user mode execution).
System image - Process execution on system’s data structures that represent the resources (memory, files,
devices) and supported by the kernel routines. Depending upon resource requirement and availability,
process’s states are controlled by executing the kernel routines accessed through system calls (system
mode execution).
The kernel maintains a process table to manage all processes. The two data structures per process are the
user structure and the process structure.
The kernel process is the first (root) process that comes into existence when the system is booted. Its
process_id and group_id are both 0.
In establishing a multi-programming environment, the kernel process creates the init process with its
process_id of 1 and group_id, 0, showing that process 1 is part of process 0. The init process creates and
manages terminal processes for active terminals in the system.
At the time of creation, each terminal process belongs to process group 0 and is made to execute a
program called getty. Once each terminal process (now called a getty process) establishes communication
with the associated terminal, it displays a login message and waits for a user to input a user name (Process
group of the init process).
When a user starts a dialogue, the getty process receives the user name and leaves the task of validating
the user’s password to another program called login. The same terminal process is now called a login
process.
The login process, after validating the user’s password, calls a command line program, the login shell to
run in the same process. The same terminal process is now called a shell process.
Each shell process now establishes a new process group and becomes ready to process the user
commands. A shell process is the initiating process by which each terminal maintains the user session.
While interpreting a command, the shell creates an execution thread and then assigns the requested
command program to this new process.
Both the shell and the new process proceed independently in separate execution threads. The parent shell
process normally waits until child process completes its execution.
A process require certain resources to accomplish its intended tasks. They are
>> CPU time
>> System memory
>> Disk Files
>> I/O devices.
Process state:- The state may be new, ready, running, waiting, halted, and so on.
Program counter:- The counter indicates the address of the next instruction to be
executed for this process.
CPU registers:- The registers vary in number and type, depending on the computer
architecture. They include accumulators, index registers, stack pointers, and general-purpose registers, plus any
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 1/5
8/10/2021 iON| Digital Learning
condition-code information.
CPU-scheduling information:- This information includes a process priority, pointers to scheduling queues, and
any other scheduling parameters.
Memory-management information:- This information may include such information as the value of the base
and limit registers, the page tables, or the segment tables, depending on the memory system used by the OS.
Accounting information:- This information includes the amount of CPU and real time used, time limits,
account numbers, job or process numbers, and so on.
I/O status information:- This information includes the list of I/O devices allocated to the process, a list of open
files, and so on.
Process state:
A process may present in one of the following state.
New:- The process is being created or just created.
Ready:- The process is waiting to be assigned/allocated to a processor time.
Running:- Instructions of the process are being executed by processor.
Waiting:- The process is waiting for some other event/process to execute.
Terminated:- The process has finished its execution.
ps command:
The ps (i.e., process status) command is used to provide information about the currently running processes in the
system.
When ps is used without any option four columns of information labeled PID,TTY, TIME and CMD for at least
two processes, the shell and ps will be visible.
PID :- The process are identified by a 5 digit number known as PID (Process Identification Number).
TTY :- Is the name of the console or terminal that the user logged into (Stands for terminal type now
but originally stood for teletype).
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 2/5
8/10/2021 iON| Digital Learning
TIME :- Is the amount of CPU time in minutes and seconds that the process has been running.
CMD :- is the name of the command that launched the process.
The information that ps -aux command provides about each process are :
Types of process:
Orphan Process
When a child process is killed, parent process gets the notification via a signal. Parent then, can continue other
task. However if the parent process is killed before, its child, is called an orphan process.
Zombie Process
When a process finished its execution and exit status not received by the parent ( or parent did not read the exit
status till now), the process state becomes zombie.
The process is dead (not to be scheduled for further execution) but cannot becompletely removed from process
table, until it has been determined that exit status is no longer needed.
Daemon Process
Some programs are not designed to be run with continuous user input and disconnect from the terminal when
task completed. For example, a web server responds to web requests, rather than user input. Mail servers are
another example of this type of application. These types of programs are known as daemons.
Starting a Process:
In Foreground: By default every process starts in foreground, ie. Gets the input from keyboard and sends
the output in monitor. But in this case till the process completes its execution no other process can be
started in foreground.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 3/5
8/10/2021 iON| Digital Learning
A process running in foreground can be send to background using the following steps:
Process running in the background can be taken into foreground using the following steps:
>> Find the job id of the process by the command jobs
>> Use fg %to get the job to foreground
Stopping/Killing a Process
Use the ps command to find out the process-id for the process
Use the command kill to terminate it
Use the command kill -9 to kill a process forcefully.
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 4/5
8/10/2021 iON| Digital Learning
While a process is executing/running , if the owner tries to log off the process ,process will get killed.
Sometimes a job or command takes a long time to complete and it is required the job to be completed without
interruption. This situation can be handled in two different ways:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 5/5
8/10/2021 iON| Digital Learning
Sometimes it may be required to execute certain Job or task on specific time. It may be not possible for any user to start or
execute the job physically on that particular time due to other business requirements. Again if similar task need to be executed
periodically on a regular interval, then the task becomes very hectic. Unix internally has solutions to handle this type of
situations using utilities such as cron and at.
Crontab
The word 'crontab' in UNIX stand for chron(chronograph) table, or time table for Unix system .
The crontab is a special table where it is possible to specify commands and time or interval to execute the command once or
repeatedly. Commands can be any executable programs, for example, a script can be written to take regular backup of all the
required tables in database. A crontab instruction can be created to execute the script on the specified time. The cron daemon
reads the table and executes the commands at the times specified.
Syntax
Components of Crontab
The basic format of a crontab schedule consists of 6 fields, placed on a single line and separated by spaces, formatted as
follows:
The various fields and their utility are explained in the image below:
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 1/2
8/10/2021 iON| Digital Learning
Example:
To executes an incremental backup shell script "takeBackup.sh" available in your home directory at 11:00 on every day the
cron entry will be as below:
00 11 * * * ~/takeBackup.sh
To schedule the same job for every minute, the cron entry will be as follows:
* * * * * ~/takeBackup.sh
https://g91.tcsion.com/LX/contents/content_home?content_player=true&LaunchFrom=iHUB&newWindow=Y&User-Agent=Computer&usrid=14994006… 2/2