0% found this document useful (0 votes)
27 views

OS - UNIT - 1 Notes

Operating system unit 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

OS - UNIT - 1 Notes

Operating system unit 1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

Unit I

Introduction to Operating System


What is an Operating System?

Operating System is a fully integrated set of specialized programs that handle all the operations of
the computer. It controls and monitors the execution of all other programs that reside in the
computer, which also includes application programs and other system software of the computer.
Examples of Operating Systems are Windows, Linux, Mac OS, etc.

An Operating System (OS) is a collection of software that manages computer hardware resources
and provides common services for computer programs. The operating system is the most important
type of system software in a computer system.

The software that contains the core components of the operating system is called the kernel.

Fig. Operating System

Computer System Components :

1. Hardware – provides basic computing resources (CPU, memory, I/O devices).

2. Operating system – controls and coordinates the use of the hardware among the various
application programs for the various users.

OS has two parts : (a)Kernel (b)Shell.

(a)Kernel is an active part of an OS i.e., it is the part of OS running at all times. It is a programs
which can interact with the hardware. Ex: Device driver, dll files, system files etc.

(b) Shell is called as the command interpreter. It is a set of programs used to interact with the
application programs. It is responsible for execution of instructions given to OS (called commands).

Operating System | Ms. Snehal Shinde 1


Unit I

3. Applications programs – Define the ways in which the system resources are used to solve the
computing problems of the users (compilers, database systems, video games, business programs).

4. Users (people, machines, other computers).

Objectives of Operating Systems


 Convenient to use: One of the objectives is to make the computer system more convenient to
use in an efficient manner.

 User Friendly: To make the computer system more interactive with a more convenient
interface for the users.

 Easy Access: To provide easy access to users for using resources by acting as an intermediary
between the hardware and its users.

 Management of Resources: For managing the resources of a computer in a better and faster
way.

 Controls and Monitoring: By keeping track of who is using which resource, granting resource
requests, and mediating conflicting requests from different programs and users.

 Fair Sharing of Resources: Providing efficient and fair sharing of resources between the
users and programs.

Functions of Operating System


1) Process Management-

A process is a program in execution. A process needs certain resources, including CPU time,
memory, files, and I/O devices, to accomplish its task.

The operating system is responsible for the following activities in connection with process
management :

 Process creation and deletion


 Process suspension and resumption
 Provision of mechanisms for: • process synchronization • process communication

2) Memory Management-

Memory is a large array of words or bytes, each with its own address. It is a repository of quickly
accessible data shared by the CPU and I/O devices. Main memory is a volatile storage device. It
loses its contents in the case of system failure.

The operating system is responsible for the following activities in connections with memory
management:

 Keep track of which parts of memory are currently being used and by whom
 Decide which processes to load when memory space becomes available
 Allocate and de-allocate memory space as needed

3) File Management-

Operating System | Ms. Snehal Shinde 2


Unit I

A file is a collection of related information defined by its creator. Commonly, files represent
programs (both source and object forms) and data.

The operating system is responsible for the following activities in connections with file
management:

 File creation and deletion


 Directory creation and deletion
 Support of primitives for manipulating files and directories
 Mapping files onto secondary storage
 File backup on stable (non-volatile) storage media.

4) I/O System Management-

The I/O system consists of:

 A buffer-caching system
 A general device-driver interface
 Drivers for specific hardware devices

5) Secondary-Storage Management-

Since main memory (primary storage) is volatile and too small to accommodate all data and
programs permanently, the computer system must provide secondary storage to back up main
memory. Most modern computer systems use disks as the principle on-line storage medium, for
both programs and data.

The operating system is responsible for the following activities in connection with disk
management:

 Free space management


 Storage allocation
 Disk scheduling

6) Networking (Distributed Systems)-

 Network Communication: Think of them as traffic cops for your internet traffic. Operating
systems help computers talk to each other and the internet. They manage how data is
packaged and sent over the network, making sure it arrives safely and in the right order.
 Settings and Monitoring: Think of them as the settings and security guard for your internet
connection. They also let you set up your network connections, like Wi-Fi or Ethernet, and
keep an eye on how your network is doing. They make sure your computer is using the
network efficiently and securely, like adjusting the speed of your internet or protecting your
computer from online threats.

7) Security-

The operating system uses password protection to protect user data and similar other techniques.
it also prevents unauthorized access to programs and user data. The operating system provides
various techniques which assure the integrity and confidentiality of user data. The following
security measures are used to protect user data:

Operating System | Ms. Snehal Shinde 3


Unit I

 Protection against unauthorized access through login.

 Protection against intrusion by keeping the firewall active.

 Protecting the system memory against malicious access.

 Displaying messages related to system vulnerabilities.

The Evolution of Operating Systems


Below are four generations of operating systems :

1. The First Generation (1940 to early 1950s)

In 1940, an operating system was not included in the creation of the first electrical computer. Early
computer users had complete control over the device and wrote programs in pure machine
language for every task. During the computer generation, a programmer can merely execute and
solve basic mathematical calculations. An operating system is not needed for these computations.

2. The Second Generation (1955 – 1965)

GMOSIS, the first operating system (OS) was developed in the early 1950s. For the IBM Computer,
General Motors has created the operating system. Because it gathers all related jobs into groups or
batches and then submits them to the operating system using a punch card to finish all of them, the
second-generation operating system was built on a single-stream batch processing system. Control
is transferred to the operating system upon each job’s completion, whether it be routinely or
unexpectedly.

The operating system cleans up after each work is finished before reading and starting the
subsequent job on a punch card. Large, professionally operated machines known as mainframes
were introduced after that.

3. The Third Generation (1965 – 1980)

Operating system designers were able to create a new operating system in the late 1960s that was
capable of multiprogramming—the simultaneous execution of several tasks in a single computer
program.

In order to create operating systems that enable a CPU to be active at all times by carrying out
multiple jobs on a computer at once, multiprogramming has to be introduced. With the release of
the DEC PDP-1 in 1961, the third generation of minicomputers saw a new phase of growth and
development.

4. The Fourth Generation (1980 – Present Day)

The fourth generation of personal computers is the result of these PDPs. The evolution of the
personal computer is linked to the fourth generation of operating systems. Nonetheless, the third-
generation minicomputers and the personal computer have many similarities. At that time,
minicomputers were only slightly more expensive than personal computers, which were highly
expensive.

Developments Leading to Modern Operating Systems

Operating System | Ms. Snehal Shinde 4


Unit I

Operating Systems have evolved in past years. It went through several changes before getting its
original form. These changes in the operating system are known as the evolution of operating
systems. OS improve itself with the invention of new technology. Basically, OS added the feature of
new technology and making itself more powerful. Let us see the evolution of operating system year-
wise in detail:

1. No OS – (0s to 1940s)

As we know that before 1940s, there was no use of OS. Earlier, people are lacking OS in their
computer system so they had to manually type instructions for each tasks in machine language(0-1
based language). At that time, it was very hard for users to implement even a simple task. It was
very time consuming and also not user-friendly. Because not everyone had that much level of
understanding to understand the machine language and it required a deep understanding.

2. Batch Processing Systems - (1940s to 1950s)

With the growth of time, batch processing system came into the market. Now Users had facility to
write their programs on punch cards and load it to the computer operator. And then operator make
different batches of similar types of jobs and then serve the different batch (group of jobs) one by
one to the CPU. CPU first executes jobs of one batch and they jump to the jobs of other batch in a
sequence manner.

3. Multiprogramming Systems - (1950s to 1960s)

Multiprogramming was the first operating system where actual revolution began. It provides user
facility to load the multiple programs into the memory and provide a specific portion of memory to
each program. When one program is waiting for any I/O operations (which take much time) at that
time the OS give permission to CPU to switch from previous program to other program(which is
first in ready queue) for continuous execution of program with interrupt.

4. Time-Sharing Systems -(1960s to 1970s)

Time-sharing systems is extended version of multiprogramming system. Here one extra feature was
added to avoid the use of CPU for long time by any single program and give access of CPU to every
program after a certain interval of time. Basically OS switches from one program to another
program after a certain interval of time so that every program can get access of CPU and complete
their work.

5. Introduction of GUI -(1970s to 1980s)

With the growth of time, Graphical User Interfaces (GUIs) came. First time OS became more user-
friendly and changed the way of people to interact with computer. GUI provides computer system
visual elements which made user’s interaction with computer more comfortable and user-friendly.
User can just click on visual elements rather than typing commands. Here are some feature of GUI
in Microsoft’s windows icons, menus and windows.

6. Networked Systems – (1980s to 1990s)

At 1980s,the craze of computer networks at its peak .A special type of Operating Systems needed to
manage the network communication . The OS like Novell NetWare and Windows NT were
developed to manage network communication which provide users facility to work in collaborative
environment and made file sharing and remote access very easy.

Operating System | Ms. Snehal Shinde 5


Unit I

7. Mobile Operating Systems – (Late 1990s to Early 2000s)

Invention of smartphones create a big revolution in software industry, to handle the operation of
smartphones, a special type of operating systems were developed. Some of them are: iOS and
Android etc. These operating systems were optimized with the time and became more powerful.

8. AI Integration – (2010s to ongoing)

With the growth of time, Artificial intelligence came into picture. Operating system integrates
features of AI technology like Siri, Google Assistant, and Alexa and became more powerful and
efficient in many ways. These AI features with operating system create a entire new feature like
voice commands, predictive text, and personalized recommendations.

The development of Microsoft and the Windows operating system was a significant influence in the
creation of personal computers. In 1975, Microsoft developed the first Windows operating system.
Bill Gates and Paul Allen had the idea to advance personal computers after releasing the Microsoft
Windows OS. As a result, the MS-DOS was released in 1981, but users found it extremely
challenging to decipher its complex commands. Windows is now the most widely used and well-
liked operating system available. Following then, Windows released a number of operating systems,
including Windows 95, Windows 98, Windows XP, and Windows 7, the most recent operating
system. The majority of Windows users are currently running Windows 10. Apple is another well-
known operating system in addition to Windows.

Types of Operating System


1. Batch Operating Systems :

Fig. Batch OS

A batch operating system grabs all programs and data in the batch form and then processes them.
The main aim of using a batch processing system is to decrease the setup time while submitting
similar jobs to the CPU. A batch monitor is started for executing all pooled jobs, after reading them.
These jobs are divided into groups, and finally, precede the same jobs in a similar batch. Now all
batched jobs are ready for execution one by one, and due to this system enhances the system
utilization while decreasing the turnaround time.

Operating System | Ms. Snehal Shinde 6


Unit I

Examples: Payroll System, Bank Invoice System, Transactions Process, Daily Report, Research
Segment, Billing System

Advantages:

 Batch processing system consumes less time for executing all jobs.
 Sharing of the batch system for multiple users is possible.
 The idle time of the batch system is much less.

Disadvantages:

 Computer operators must be trained to use batch systems.


 It is difficult to debug batch systems.
 Batch systems are sometimes costly.It is difficult to debug batch systems.

2. Time-sharing Operating Systems :

A time-shared operating system uses CPU scheduling and multiprogramming to provide each user
with a small portion of a time-shared computer. Each user has at least one separate program in
memory. When a process executes, it executes for only a short time before it either finishes or
needs to perform input/output. In time-sharing operating systems several jobs must be kept
simultaneously in memory, so the system must have memory management and protection.

Examples: Windows 2000 server, Windows NT server, Unix, Linux

Fig. Time Sharing OS

Advantages:

 Each task gets an equal opportunity.


 Fewer chances of duplication of software.
 CPU idle time can be reduced.

Disadvantages:

 Reliability problem.
 One must have to take of the security and integrity of user programs and data.
 Data communication problem.
Operating System | Ms. Snehal Shinde 7
Unit I

3. Distributed operating system :

A distributed operating system allows the distribution of entire systems on a couple of center
processors, and it serves multiple real-time products as well as multiple users. All processors are
connected by valid communication mediums such as high-speed buses and telephone lines, and in
which every processor contains its local memory along with other local processors. A distributed
OS is capable of sharing its computational capacity and I/O files while allowing virtual machine
abstraction to users.

Fig. Distributed OS

Examples : Solaris, OSF/1, Micros, DYNIX, Locus, Mach

Advantages :

 Increasing data availability across the entire system.


 It reduces the probability of data corruption because all data is replicated across all sites.
 It helps in the reduction of data processing time.

Disadvantages :

 It is hard to implement adequate security in a distributed operating system since the nodes
and connections must be secured.
 Gathering, processing, presenting, and monitoring hardware use metrics for big clusters can
be a real issue.

4. Network operating systems :

An operating system that provides connectivity among several autonomous computers is called
a network operating system. A configuration for a network operating system is a collection of
personal computers along with a common printer, server, and file server for archival storage, all
tied together by a local network. There are two basic types of network operating systems, discussed
as follows:

 Peer-to-Peer Network Operating Systems: allow users to share network resources saved in a
common, accessible network location. In this architecture, all devices are treated equally in

Operating System | Ms. Snehal Shinde 8


Unit I

terms of functionality. Peer-to-peer usually works best for small to medium LANs and is
cheaper.
 Client/Server Network Operating Systems: provide users with access to resources through a
server. All functions and applications are unified under one file server that can be used to
execute individual client actions regardless of physical location.

Fig. Network OS

Advantages :

 Highly stable centralized servers


 Security concerns are handled through servers

Disadvantages :

 Servers are costly


 User has to depend on a central location for most operations
 Maintenance and updates are required regularly

5. Real-time Operating Systems :

A real-time operating system (RTOS) is an operating system that runs multi-threaded applications
and can meet real-time deadlines. Most RTOSes include a scheduler, resource management, and
device drivers. Note that when we talk about “deadlines”, we do not necessarily mean “fast”.
Instead, this means we can determine when certain tasks will execute before runtime.

Examples: Airline traffic control systems, Command Control Systems., Airlines reservation system,
Heart Peacemaker, Network Multimedia Systems, Robotics.

Operating System | Ms. Snehal Shinde 9


Unit I

Fig. Real Time OS

Advantages :

 Priority-Based Scheduling.
 Easier Testing.
 Code Reuse.
 Improved Efficiency.

Disadvantages :

 Limited Tasks
 Use Heavy System resources.
 Complex Algorithms.

6. Mobile Operating System :

A mobile operating system is an operating system that helps run application software on mobile
devices. It is the same kind of software as the famous computer operating systems Linux and
Windows, but they are light and simple to some extent. The operating systems found on
smartphones include Symbian OS, IOS, Blackberry OS, Windows Mobile, Palm WebOS, Android, and
Maemo.

Fig. Mobile OS

Examples: Android, IOS, HarmonyOS, PalmOS

Operating System | Ms. Snehal Shinde 10


Unit I

Advantages:

 Convenience inoperability.
 It also includes open-source platforms.
 Gives notification ease.

Disadvantages:

 Instability.
 It also includes poor battery quality.
 Not sufficient computational power.

7. Multiprogramming Operating System :

A multiprogramming operating system runs multiple programs on a single processor computer. If a


program waits for an I/O transfer, the other programs are ready to use the CPU. As a result, various
jobs may share CPU time. However, the execution of their jobs is not defined to be in the same
period. A multiprogramming OS is of the following two types:

Multitasking OS: Enables execution of multiple programs at the same time. The operating system
accomplishes this by swapping each program in and out of memory one at a time. When a program
is switched out of memory, it is temporarily saved on disk until it is required again.

Multiuser Operating System: This allows many users to share processing time on a powerful central
computer from different terminals. The operating system accomplishes this by rapidly switching
between terminals, each of which receives a limited amount of processor time on the central
computer.

Fig. Multiprogramming OS

Examples: Apps like office, chrome, etc., Microcomputers like MP/M, XENIX, and ESQview.,
Windows O/S, UNIX O/S

Advantages:
Operating System | Ms. Snehal Shinde 11
Unit I

 It may help to run various jobs in a single application simultaneously.


 The resources are utilized smartly.

Disadvantages :

 It is highly complicated and sophisticated.


 The CPU scheduling is required.
 The harder task is to handle all processes and tasks.

8. Multiprocessor Operating System :

Multiprocessor operating system utilizes multiple processors, which are connected to physical
memory, computer buses, clocks, and peripheral devices (touchpad, joystick, etc.). The main
objective of using a multiprocessor OS is to consume high computing power and increase the
execution speed of the system. The four major components, used in the Multiprocessor Operating
System are CPU, Input Output Processor, Input/Output Devices, Memory Unit.

Fig. Multiprocessor OS

Advantages:

 Great Reliability.
 Cost-Effective System.
 Parallel Processing.

Disadvantages:

 It is more expensive due to its large architecture.


 Its speed can get degraded due to failing any one processor.

Virtual Machine
 Virtual machine abstracts the hardware of our personal computer such as CPU, disk drives,
memory, NIC (Network Interface Card) etc, into many different execution environments as per
our requirements, hence giving us a feel that each execution environment is a single computer.
For example, Virtual Box.
 When we run different processes on an operating system, it creates an illusion that each
process is running on a different processor having its own virtual memory, with the help of CPU
scheduling and virtual-memory techniques. There are additional features of a process that
cannot be provided by the hardware alone like system calls and a file system. The virtual
Operating System | Ms. Snehal Shinde 12
Unit I

machine approach does not provide these additional functionalities but it only provides an
interface that is same as basic hardware. Each process is provided with a virtual copy of the
underlying computer system.
 We can create a virtual machine for several reasons, all of which are fundamentally related to
the ability to share the same basic hardware yet can also support different execution
environments, i.e., different operating systems simultaneously.
 The main drawback with the virtual-machine approach involves disk systems. Let us suppose
that the physical machine has only three disk drives but wants to support seven virtual
machines. Obviously, it cannot allocate a disk drive to each virtual machine, because virtual-
machine software itself will need substantial disk space to provide virtual memory and
spooling. The solution is to provide virtual disks.
 Users are thus given their own virtual machines. After which they can run any of the operating
systems or software packages that are available on the underlying machine. The virtual-
machine software is concerned with multi-programming multiple virtual machines onto a
physical machine, but it does not need to consider any user-support software. This arrangement
can provide a useful way to divide the problem of designing a multi-user interactive system,
into two smaller pieces.

Fig. Virtual Machine

Advantages:

 There are no protection problems because each virtual machine is completely isolated from
all other virtual machines.
 Virtual machine can provide an instruction set architecture that differs from real
computers.
 Easy maintenance, availability and convenient recovery.

Disadvantages:

 When multiple virtual machines are simultaneously running on a host computer, one virtual
machine can be affected by other running virtual machines, depending on the workload.
 Virtual machines are not as efficient as a real one when accessing the hardware.

Operating System | Ms. Snehal Shinde 13


Unit I

Shell Scripting
If we are using any major operating system, we are indirectly interacting with the shell. While
running Ubuntu, Linux Mint, or any other Linux distribution, we are interacting with the shell by
using the terminal. In this article we will discuss Linux shells and shell scripting so before
understanding shell scripting we have to get familiar with the following terminologies:

 Kernel
 Shell
 Terminal

What is Kernel?

The kernel is a computer program that is the core of a computer’s operating system, with complete
control over everything in the system. It manages the following resources of the Linux system –

1. File management
2. Process management
3. I/O management
4. Memory management
5. Device management etc.

It is often mistaken that Linus Torvalds has developed Linux OS, but actually, he is only responsible
for the development of the Linux kernel.

What is Shell?

A shell is a special user program that provides an interface for the user to use operating system
services. Shell accepts human-readable commands from users and converts them into something
which the kernel can understand. It is a command language interpreter that executes commands
read from input devices such as keyboards or from files. The shell gets started when the user logs in
or starts the terminal.

Fig. Linux Shell

Shell can be accessed in two ways –


Operating System | Ms. Snehal Shinde 14
Unit I

1) Command Line Shell


2) Graphical shell

1) Command Line:

Shell can be accessed by users using a command line interface. A special program called Terminal in
Linux/macOS, or Command Prompt in Windows OS is provided to type in the human-readable
commands such as “cat”, “ls” etc. and then it is being executed. The result is then displayed on the
terminal to the user. A terminal in Ubuntu 16.4 system looks like this –

Fig. Linux command line

2) Graphical User Interface:

It provide means for manipulating programs based on the graphical user interface (GUI), by
allowing for operations such as opening, closing, moving, and resizing windows, as well as
switching focus between windows. Window OS or Ubuntu OS can be considered as a good example
which provides GUI to the user for interacting with the program. Users do not need to type in
commands for every action. A typical GUI in the Ubuntu system –

Fig. GUI Shell

Operating System | Ms. Snehal Shinde 15


Unit I

Types of Shells:

1. The Bourne Shell (sh) :

 Developed at AT&T Bell Labs by Steve Bourne, the Bourne shell is regarded as the first UNIX
shell ever. It is denoted as sh. It gained popularity due to its compact nature and high speeds
of operation.
 This is what made it the default shell for Solaris OS.
 The complete path-name for the Bourne shell is /bin/sh and /sbin/sh.
 By default, it uses the prompt # for the root user and $ for the non-root users.
 However, the Bourne shell has some major drawbacks like it doesn’t have in-built
functionality to handle logical and arithmetic operations and cannot recall previously used
commands.

2. The GNU Bourne-Again Shell (bash) :

 More popularly known as the Bash shell, the GNU Bourne-Again shell was designed to be
compatible with the Bourne shell. It incorporates useful features from different types of
shells in Linux such as Korn shell and C shell.
 It allows us to automatically recall previously used commands and edit them with help of
arrow keys, unlike the Bourne shell.
 The complete path-name for the GNU Bourne-Again shell is /bin/bash. By default, it uses
the prompt bash-VersionNumber# for the root user and bash-VersionNumber$ for the non-
root users.

3. The C Shell (csh) :

 The C shell was created at the University of California by Bill Joy. It is denoted as csh. It was
developed to include useful programming features like in-built support for arithmetic
operations and a syntax similar to the C programming language.
 Further, it incorporated command history which was missing in different types of shells in
Linux like the Bourne shell. Another prominent feature of a C shell is “aliases”.
 The complete path-name for the C shell is /bin/csh. By default, it uses the
prompt hostname# for the root user and hostname% for the non-root users.

4. The Korn Shell (ksh) :

 The Korn shell was developed at AT&T Bell Labs by David Korn, to improve the Bourne
shell. It is denoted as ksh. The Korn shell is essentially a superset of the Bourne shell.
 It provides users with new functionalities. It allows in-built support for arithmetic
operations while offereing interactive features which are similar to the C shell.
 The Korn shell runs scripts made for the Bourne shell, while offering string, array and
function manipulation similar to the C programming language. It also supports scripts
which were written for the C shell. Further, it is faster than most different types of shells in
Linux, including the C shell.
 The complete path-name for the Korn shell is /bin/ksh. By default, it uses the prompt # for
the root user and $ for the non-root users.

5. The Z Shell (zsh) :

Operating System | Ms. Snehal Shinde 16


Unit I

 The Z Shell or zsh is a sh shell extension with tons of improvements for customization. If
you want a modern shell that has all the features a much more, the zsh shell is what you’re
looking for.
 Some noteworthy features of the z shell include:
 Generate filenames based on given conditions
 Plugins and theming support
 Index of built-in functions
 Command completion

What is a terminal?

A program which is responsible for providing an interface to a user so that he/she can access the
shell. It basically allows users to enter commands and see the output of those commands in a text-
based interface. Large scripts that are written to automate and perform complex tasks are executed
in the terminal.

To access the terminal, simply search in search box “terminal” and double-click it.

Shell Scripting:

 Usually, shells are interactive, which means they accept commands as input from users and
execute them. However, sometimes we want to execute a bunch of commands routinely, so
we have to type in all commands each time in the terminal.
 As a shell can also take commands as input from file, we can write these commands in a file
and can execute them in shell to avoid this repetitive work. These files are called “Shell
Scripts” or “Shell Programs”. Shell scripts are similar to the batch file in MS-DOS. Each shell
script is saved with `.sh` file extension e.g., myscript.sh.
 A shell script has syntax just like any other programming language. If you have any prior
experience with any programming language like Python, C/C++ etc. It would be very easy to
get started with it.

A shell script comprises the following elements –

1. Shell Keywords – if, else, break etc.


2. Shell commands – cd, ls, echo, pwd, touch etc.
3. Functions
4. Control flow – if..then..else, case and shell loops etc.

There are many reasons to write shell scripts:

 To avoid repetitive work and automation


 System admins use shell scripting for routine backups.
 System monitoring
 Adding new functionality to the shell etc.

Some Advantages of shell scripts :

 The command and syntax are exactly the same as those directly entered in the command
line, so programmers do not need to switch to entirely different syntax.
 Writing shell scripts are much quicker.
Operating System | Ms. Snehal Shinde 17
Unit I

 Quick start
 Interactive debugging etc.

Some Disadvantages of shell scripts :

 Prone to costly errors, a single mistake can change the command which might be harmful.
 Slow execution speed
 Design flaws within the language syntax or implementation
 Not well suited for large and complex task
 Provide minimal data structure unlike other scripting languages. etc.

Basic Shell Commands


A shell is a special user program that provides an interface to the user to use operating system
services. Shell accepts human-readable commands from the user and converts them into something
which the kernel can understand. It is a command language interpreter that executes commands
read from input devices such as keyboards or from files. The shell gets started when the user logs in
or starts the terminal.

1) Displaying the file contents on the terminal:

cat: It is generally used to concatenate the files. It gives the output on the standard output.

more: It is a filter for paging through text one screenful at a time.

less: It is used to viewing the files instead of opening the file. Similar to more command but it allows
backward as well as forward movement.

head : Used to print the first N lines of a file. It accepts N as input and the default value of N is 10.

tail : Used to print the last N-1 lines of a file. It accepts N as input and the default value of N is 10.

2) File and Directory Manipulation Commands:

mkdir : Used to create a directory if not already exist. It accepts the directory name as an input
parameter.

cp : This command will copy the files and directories from the source path to the destination path. It
can copy a file/directory with the new name to the destination path. It accepts the source
file/directory and destination file/directory.

mv : Used to move the files or directories. This command’s working is almost similar
to cp command but it deletes a copy of the file or directory from the source path.

rm : Used to remove files or directories.

touch : Used to create or update a file.

3) Extract, sort, and filter data Commands:

grep : This command is used to search for the specified text in a file. grep with Regular Expressions:
Used to search for text using specific regular expressions in file.

Operating System | Ms. Snehal Shinde 18


Unit I

sort : This command is used to sort the contents of files.

wc : Used to count the number of characters, words in a file.

cut : Used to cut a specified part of a file.

4) Basic Terminal Navigation Commands:

ls : To get the list of all the files or folders.

ls -l: Optional flags are added to ls to modify default behavior, listing contents in extended form -l is
used for “long” output

ls -a: Lists of all files including the hidden files, add -a flag

cd: Used to change the directory.

du: Show disk usage.

pwd: Show the present working directory.

man: Used to show the manual of any command present in Linux.

rmdir: It is used to delete a directory if it is empty.

ln file1 file2: Creates a physical link.

ln -s file1 file2: Creates a symbolic link.

locate: It is used to locate a file in Linux System

echo: This command helps us move some data, usually text into a file.

df: It is used to see the available disk space in each of the partitions in your system.

tar: Used to work with tarballs (or files compressed in a tarball archive)

5) File Permissions Commands:

The chmod and chown commands are used to control access to files in UNIX and Linux systems.

chown : Used to change the owner of the file.

chgrp : Used to change the group owner of the file.

chmod : Used to modify the access/permission of a user.

Bash Scripting
Bash is a command-line interpreter or Unix Shell and it is widely used in GNU/Linux Operating
System. It is written by Brian Jhan Fox. It is used as a default login shell for most Linux
distributions. Scripting is used to automate the execution of the tasks so that humans do not need to
perform them individually. Bash scripting is a great way to automate different types of tasks in a
system. Developers can avoid doing repetitive tasks using bash scripting.

Operating System | Ms. Snehal Shinde 19


Unit I

Bash scripting supports variables, conditional statements, and loops just like programming
languages.

Applications of Bash Scripts:

 Manipulating files

 Executing routine tasks like Backup operation

 Automation

Advantages of Bash Scripts:

 It is simple.

 It helps to avoid doing repetitive tasks

 Easy to use

 Frequently performed tasks can be automated

 A sequence of commands can be run as a single command.

Disadvantages of Bash Scripts:

 Any mistake while writing can be costly.

 A new process launched for almost every shell command executed

 Slow execution speed

 Compatibility problems between different platforms

To write a Bash Script we will follow the steps :

 First, we will create a file with the .sh extension.


 Next, we will write down the bash scripts within it
 After that, we will provide execution permission to it.

To create and write a file with the .sh extension we can use gedit text editor. The command for it
will be –

gedit scriptname.sh

The first line of our script file will be –

#!/bin/bash

This will tell, the system to use Bash for execution. Then we can write our own scripts.

Let’s write down just a simple script that will print some lines in the terminal. The code for it will be

#!/bin/bash

Operating System | Ms. Snehal Shinde 20


Unit I

echo "Hello, GeeksforGeeks"

Now we will save the file and provide the execution permission to it. To do so use the following
command –

chmod +x scriptname.sh

Next to execute the following script we will use the following command –

./scriptname.sh

File Names and Permissions :

While writing bash scripts we should save our file with the .sh extension, so that the Linux system
can execute it. When we first create a file with the .sh extension, it doesn’t have any execute
permission and without the execute permission the script will not work. So, we should provide
execute permission to it using the chmod command.

The filename of a bash script can be anything but by convention, it is recommended to use snake
case ( my_bash_script.sh ) or hyphens ( my-bash-script.sh ) for naming a script file.

Variables :

We can use variables in bash scripting.

Example Script:

Name="SATYAJIT GHOSH"

Age=20

echo "The name is $Name and Age is $Age"

Output of Variables:

The name is SATYAJIT GHOSH and Age is 20

So, we can declare a variable in a bash script using VariableName=Value and can access it
using $VariableName.

There are two types of variables present within Bash Scripting. Conventionally, If a variable, is
declared inside a function then it is generally a local variable and if it is declared outside then it is
a global variable. In the case of a bash script, this concept is a little bit different, here any variable
whether it is written inside a function or outside a function by default is a global variable. If we
want to make a local variable then we need to use the keyword “local”.

Note: It is best practice to always use a local variable inside a function to avoid any unnecessary
confusion.

Input and Output :

Input & output are fundamental concepts for shell scripting. A script can take one or more inputs
and can also produce zero or many outputs. It may even produce some errors.

Operating System | Ms. Snehal Shinde 21


Unit I

Functions :

In programming, A function is a block of code that performs some tasks and it can be called multiple
times for performing tasks.

Syntax of Functions:

#for defining

function_name(){

commands

.....

function_name # for calling

Besides this, we can also have functions with passing arguments and with return values.

Decision Making :

In programming, Decision Making is one of the important concepts. The programmer provides one
or more conditions for the execution of a block of code. If the conditions are satisfied then those
block of codes only gets executed.

Two types of decision-making statements are used within shell scripting. They are –

1. If-else statement:

If else statement is a conditional statement. It can be used to execute two different codes based on
whether the given condition is satisfied or not.

There are a couple of varieties present within the if-else statement. They are –

 if-fi
 if-else-fi
 if-elif-else-fi
 nested if-else

Syntax of If-else statement:

if [ expression ]; then

statements

fi

2. case-sac statement:

case-sac is basically working the same as switch statement in programming. Sometimes if we have
to check multiple conditions, then it may get complicated using if statements. At those moments we
can use a case-sac statement. The syntax will be –

Operating System | Ms. Snehal Shinde 22


Unit I

Syntax of case-sac statement:

case $var in

Pattern 1) Statement 1;

Pattern n) Statement n;

esac

String and Numeric Comparisons :

The string comparison means in the shell scripting we can take decisions by doing comparisons
within strings as well. Here is a descriptive table with all the operators –

Operator Description Opera Description Oper- Description


-tor ator

== Returns true if the strings -eq Equal -le Less Than or


are equal Equal

!= Returns true if the strings -ge Greater Than or -lt Less Than
are not equal Equal

-n Returns true if the string -gt Greater Than -ne Not Equal
to be tested is not null

-z Returns true if the string


to be tested is null

Operating System | Ms. Snehal Shinde 23

You might also like