Osmanualdocx 2025 01 13 13 14 47
Osmanualdocx 2025 01 13 13 14 47
1. Create a Windows Installation Media: - If you order Windows 10 for a new PC build, they may
send you an installation media. If you don't have a Windows installation media, you can make one. You'll
need a computer with internet access and an 8 GB USB flash drive (16 GB recommended). You also need to
know if the computer you want to install Windows 10 on has 32-bit or 64-bit architecture. You will also need
to purchase Windows to get a product key. This can be done before or after the installation.
5. Select the USB drive: - This is the drive that has the Windows Installation Media. This will boot the
PC from the Windows Installation Media. Once the computer boots from the USB drive press any key to start
the Windows installation process.
If you are inside the BIOS settings. Locate the option that says "Boot", "Boot order", "Priorities" or
something similar. Change the boot order so that your PC boots from the USB drive first. Then
select the option to save and exit. This will reboot your PC from the USB drive.
6. Select your language, time and currency, and keyboard input and click next: - Use the drop-
down menus to first select your language. Then select your country or region, and your keyboard input
method. Then click next.
7. Click Install Now: - It's the button in the centre of the screen.
8. Enter your Windows product key and click next: - you‟ve already purchased Windows; you
can find your license key in your confirmation email. If you have not purchased Windows, click I don't
have a product key. You'll need to purchase Windows and activate it later on.
9. Select which edition of Windows you want to install and click next: - If you've already
purchased Windows, check which version of Windows you purchased and make sure you click the right
version. If you have not yet purchased Windows, check which version you want to install. Different versions
of Windows have a different price.
If you are not sure which version of Windows to install, Windows Home Edition is the most common
version of Windows.
10. Click the checkbox next to "I accept the license terms" and click Next: - You can read
the license terms in the window in the centre. Click next when you are ready to continue.
11. Click custom: install windows only or upgrade: install windows and keep files, setting,
and applications: - If your computer already has a Windows installation on it, click Upgrade: Install Windows
and keep files, settings, and applications. If your computer does not have a Windows installation or you need
to reinstall Windows, click Custom: Install Windows only.
12. Select a drive or partition you want to install Windows on and click Delete: - If you have
multiple hard drives or a hard drive with multiple partitions, select the partition you want to install Windows
on and click Delete. Be aware that this will erase all data on the drive. Make sure you have backed up all
data you want to keep before continuing. This will leave the drive with unallocated space.
13. Select a drive with unallocated space and click Next: - This will start installing Windows on
the drive you selected. The amount of time it takes will depend on the hardware of your computer.
14. Remove the USB flash drive and restart your computer: - Once Windows is finished installing,
your computer will restart automatically. Remove the USB flash drive so that it does not try to boot from
the Flash drive again. The first time Windows boots up, you will need to go through the Setup process.
EXPERIMENT: - 2
Installation of Linux OS.
Installation of Linux: - Linux is the foundation of thousands of open source operating systems designed to
replace Windows and Mac OS. It is free to download and install on any computer. Because it is open source,
there are a variety of different versions, or distributions, available developed by different groups. Follow this
guide for basic instructions on how to install any version of Linux, as well as specific instructions for some of
the most popular ones.
1. Download the Linux distribution of your choice: - If you're new to Linux, consider trying a
lightweight and easy to use distribution, such as Ubuntu or Linux Mint. Linux distributions (known as
"distress") are typically available for free to download in ISO format. You can find the ISO for the
distribution of your choice at the distribution‟s website. This format needs to be burned to a CD or USB stick
before you can use it to install Linux. This will create a Live CD or Live USB.
A Live CD or Live USB is a disk that you can boot into, and often contains a preview version of the
operating system that can be run directly from the CD or USB stick.
Install an image burning program, or use your system‟s built-in burning tool if you are using
Windows 7, 8, or Mac OS X. Pen Drive Linux and UNetBootin are two popular tools for burning ISO
files to USB sticks.
2. Boot into the Live CD or Live USB: -Most computers are set to boot into the hard drive first, which
means you will need to change some settings to boot from your newly-burned CD or USB. Start by rebooting
the computer.
Once the computer reboots, press the key used to enter the boot menu. The key for your system will
be displayed on the same screen as the manufacturer‟s logo. Typical keys include F12, F2, or Del.
For Windows 8 users, hold the Shift key and click restart. This will load the Advanced Startup
Options, where you can boot from CD.
For Windows 10 users, go to advanced boot in settings and click "Restart Now."
If your computer doesn't give you direct access to the boot menu from the manufacturer's splash
screen, it's most likely hidden in the BIOS menu. You can access the BIOS menu in the same way that
you would get to the boot menu. At the manufacturer splash screen, the key should be listed in one of
the bottom corners.
Once you're in the boot menu, select your live CD or USB. Once you‟ve changed the settings,
save and exit the BIOS setup or boot menu. Your computer will continue with the boot process.
3. Try out the Linux distribution before installing: - Most Live CDs and USBs can launch a "live
environment", giving you the ability to test it out before making the switch. You won‟t be able to create
files, but you can navigate around the interface and decide if it‟s right for you.
4. Start the installation process: - If you‟re trying out the distort, you can launch the installation from the
application on the desktop. If you decided not to try out the distribution, you can start the installation from the
boot menu.
You will be asked to configure some basic options, such as language, keyboard layout, and time zone.
5. Create a username and password: - You will need to create login information to install Linux.
A password will be required to log into your account and perform administrative tasks.
`
6. Set up the partition: - Linux needs to be installed on a separate partition from any other operating
systems on your computer if you intend dual booting Linux with another OS. A partition is a portion of the
hard drive that is formatted specifically for that operating system. You can skip this step if you don't plan on
dual booting.
Distros such as Ubuntu will set a recommended partition automatically. You can then adjust this
manually yourself. Most Linux installations require at least 20 GB, so be sure to set aside enough
room for both the Linux operating system and any other programs you may install and files you may
create.
If the installation process does not give you automatic partitions, make sure that the partition you
create is formatted as Ext4. If the copy of Linux you are installing is the only operating system on
the computer, you will most likely have to manually set your partition size.
7. Boot into Linux: - Once the installation is finished, your computer will reboot. You will see a new
screen when your computer boots up called “GNU GRUB”. This is a boot loader that handles Linux
installations. Pick your new Linux distro from the list. This screen may not show up if you only have one
operating system on your computer. If this screen isn't being presented to you automatically, then you can
get it back by hitting shift right after the manufacturer splash screen.
If you install multiple distros on your computer, they will all be listed here.
8. Check your hardware: - Most hardware should work out of the box with your Linux distro, though
you may need to download some additional drivers to get everything working.
Some hardware requires proprietary drivers to work correctly in Linux. This is most common with
graphics cards. There is typically an open source driver that will work, but to get the most out of
your
graphics cards you will need to download the proprietary drivers from the manufacturer.
n Ubuntu, you can download proprietary drivers through the System Settings menu. Select the
Additional Drivers option, and then select the graphics driver from the list. Other distros have
specific methods for obtaining extra drivers.
You can find other drivers from this list as well, such as Wi-Fi drivers.
9. Start using Linux: - Once your installation is complete and you‟ve verified that your hardware is
working, you‟re ready to start using Linux. Most distros come with several popular programs installed,
and you can download many more from their respective file repositories.
EXPERIMENT: - 3
Dual boot installation of Operating systems.
Dual boot installation: - In this article we will see how to install a Linux based OS alongside a Windows
OS already installed on a System.
Before we begin here are some of the Advantages of a Linux OS over a Windows OS and why you should
consider using a Linux System. (Purely based of my experience)
Linux Systems are generally light on system resources and hence run faster even on old
hardware setups.
Linux has native support for the gcc compiler and python interpreter.
Linux also has native support for Git which makes it easier for people to synchronize their work
with remote repositories on GitHub.
Linux is Open Source and hence tinkering around with its source code can give us a better
understanding of the Structure of an OS.
Prerequisites, before we begin the process: -
It is recommended that you have 100 – 150 GB of free Hard Disk space, although anything above 40
GB would do the job. (This step is important as it is the amount of space you will be dedicating
to your Linux OS, and once Linux is installed you will not be able to access this space through
Windows)
A USB flash drive has a minimum space of 8 GB.
(Optional, Only for newer machines) Making sure whether Windows is installed in UEFI mode or
Legacy BIOS Mode.
Making Sure whether Windows is installed in legacy BIOS mode or UEFI mode
1. STEP: -1
Unallocating the Required amount of Hard Disk Space: - This step is required to free up some
space from your NTFS formatted Hard Disk. This unallocated space will contain the Linux System Partition
and hence it is necessary for you to decide carefully how much space you will be devoting to Linux.
To do this goes to the Windows Disk Management Service. You can go to „Run‟ enter „diskmgmt.msc‟ and
run.
Windows Disk Management Service
2. STEP: -2
Creating a Bootable Linux USB Drive: -For the Purpose of this article i will be installing „Ubuntu‟ as
the Linux OS of choice as it is the most popular Linux OS and has a large support base.
We first need to download the Disc Image File (.iso file) of the Ubuntu OS from the Official Ubuntu
website (www.ubuntu.com).
The Latest LTS (Long Term Support) Version is preferred over the non – LTS version, as they provide
update support for 5 year rather than 9 months for the non – LTS versions.
We also need software called Rufus for the creation of a Bootable USB Drive. A Bootable Drive is one
from which the machine can load the OS without installing it.
Then we need to back up all the Contents of the USB flash Drive and Start Rufus. In Rufus we make the
following settings a shown below.
Setting up Rufus
3. STEP: - 3
Disabling Secure boot and Fast boot (Only for Newer Machines): - This step is not required for
older machines say, older than 2012. In this step we need to enter the BIOS/UEFI menu of our Computer.
In case you are running Windows 8.1/10, before entering the BIOS/UEFI Menu you need to disable „Fast
Start-up‟.
For this go to the „Power Options‟. On the left side options go to „Choose what the power buttons do‟ then
click of „Change Settings that are currently unavailable‟ and Unstick „Fast Start-up‟ below and save these
changes.
Power Options
Turning Off Fast Start-up
Now this step of the Process is manufacturer specific. I suggest you to Google a bit to get the exact key
combination you need to press during the machine startup that will get you into the BIOS/UEFI Menu. In
most Cases you need to press either one of the f1, f2 or f12 keys after turning on the machine but before
windows starts booting up.
Disabling Secureboot
Disabling Fastboot
4. STEP: -4
Booting Ubuntu Linux From USB Drive: - In the next step we boot Linux from the USB Flash Drive
we just created. To do this we enter into the „Boot Options‟ during the machine starts up but before windows
starts loading. The „Boot Options Menu‟ is shown below:
Boot Options Menu
First Screen after USB Boot
First Boot up of Ubuntu Linux:
Ubuntu Desktop
5. STEP: - 5
Installing Ubuntu Linux: - Once Ubuntu has booted off the USB Flash Drive you can try out the
different applications that the linux distribution has to offer and once you verify all the basic functionality
of the OS that you will be using work on your machine, we can proceed with the installation.
Run the „Install Ubuntu‟ icon shown on the Ubuntu Desktop by Double Clicking it and Follow the steps as
shown below.
Selecting Language
Choosing Keyboard Layout
choosing Type of Installation: Normal or Minimal
Confirmation of Modifications (Make sure both the partitions you created out of the free space are listed)
After all this is done you simply need to proceed as follows till the installation starts:
First Come First Serve is just like FIFO (First in First out) Queue data structure, where the data
element which is added to the queue first, is the one who leaves the queue first.
It's easy to understand and implement programmatically, using a Queue data structure, where a
new process enters through the tail of the queue, and the scheduler selects process from the head of
the queue.
A perfect real life example of FCFS scheduling is buying tickets at ticket counter.
Calculating Average Waiting Time: - For every scheduling algorithm, Average waiting time is a crucial
parameter to judge its performance.
AWT or Average waiting time is the average of the waiting times of the processes in the queue, waiting for
the scheduler to pick them for execution.
Lower the Average Waiting Time, better the scheduling algorithm.
consider the processes P1, P2, P3, P4 given in the below table, arrives for execution in the same order,
with Arrival Time 0, and given Burst Time, let's find the average waiting time using the FCFS scheduling
algorithm.
The average waiting time will be 18.75 ms
For the above given processes, first P1 will be provided with the CPU resources,
Similarly, waiting time for process P3 will be execution time of P1 + execution time for P2, which
will be (21 + 3) ms = 24 ms.
For process P4 it will be the sum of execution times of P1, P2 and P3.
The Gantt chart above perfectly represents the waiting time for each process.
Below we have a few shortcomings or problems with the FCFS scheduling algorithm: -
1. It is Non Pre-emptive algorithm, which means the process priority doesn't matter.
If a process with very least priority is being executed, more like daily routine backup process, which
takes more time, and all of a sudden some other high priority process arrives, like interrupt to avoid
system crash, the high priority process will have to wait, and hence in this case, the system will crash,
just because of improper process scheduling.
3. Resources utilization in parallel is not possible, which leads to Convoy Effect, and hence poor resource
(CPU, I/O etc.) utilization.
Convoy Effect is a situation where many processes, who need to use a resource for short time are blocked by
one process holding that resource for a long time.
This essentially leads to poort utilization of resources and hence poor performance.
Program for FCFS Scheduling: - Here we have a simple C++ program for processes with arrival
time as 0.
If you are not familiar with C++ language, we would recommend you to first Learn C++ language.
In the program, we will be calculating the Average waiting time and Average turnaround time for a
given array of Burst times for the list of processes.
#include<iostream>
// main function
int main()
{
// process ids
int processes[] = { 1, 2, 3, 4};
int n = sizeof processes / sizeof processes[0];
findAverageTime(processes, n, burst_time);
return 0;
}
Output: -
Here we have simple formulae for calculating various times for given processes: -
Completion Time: - Time taken for the execution to complete, starting from arrival time.
Turn Around Time: - Time taken to complete after arrival. In simple words, it is the difference between
the Completion time and the Arrival time.
Waiting Time: - Total time the process has to wait before it's execution begins. It is the difference between
the Turn Around time and the Burst time of the process.
For the program above, we have considered the arrival time to be 0 for all the processes, try to implement a
program with variable arrival times.
EXPERIMENT: - 5
Implementation of SJF Scheduling algorithm
Shortest Job First (SJF): - Shortest Job First is an algorithm in which the process having the smallest
execution time is chosen for the next execution. This scheduling method can be preemptive or non-
preemptive. It significantly reduces the average waiting time for other processes awaiting execution.
Preemptive Scheduling: - Preemptive scheduling is used when a process switches from running state to
ready state or from waiting state to ready state. The resources (mainly CPU cycles) are allocated to the process
for the limited amount of time and then is taken away, and the process is again placed back in the ready queue
if that process still has CPU burst time remaining. That process stays in ready queue till it gets next chance to
execute.
Algorithm: -
1. Sort all the process according to the arrival time.
2. Then select that process which has minimum arrival time and minimum Burst time.
3. After completion of process make a pool of process which after till the completion of previous
process and select that process among the pool which is having minimum Burst time.
Program for Shortest Job First Scheduling: -
#include<iostream>
int mat[10][6];
*a = *b;
*b = temp;
swap(mat[j][k], mat[j+1][k]);
temp = mat[i-1][3];
int low = mat[i][2];
for(int j=i; j<num; j++)
low = mat[j][2];
val = j;
swap(mat[val][k], mat[i][k]);
}
int main()
cin>>num;
cout<<"...Process "<<i+1<<"...\n";
cin>>mat[i][0];
cin>>mat[i][1];
cin>>mat[i][2];
cout<<"Before Arrange...\n";
cout<<"Process ID\tArrival Time\tBurst Time\n";
for(int i=0; i<num; i++)
cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\n";
arrangeArrival(num, mat);
completionTime(num, mat);
cout<<"Final Result...\n";
cout<<mat[i][0]<<"\t\t"<<mat[i][1]<<"\t\t"<<mat[i][2]<<"\t\t"<<mat[i][4]<<"\t\t"<<mat[i][5]<<"\n";
}
}
Output: -
Description of Program: - In the above program, we calculate the average waiting and average turnaround
times of the jobs. We first ask the user to enter the number of processes and store it in n. We then accept the
burst times from the user. It is stored in the bt array.
After this, the burst times are sorted in the next section so the shortest one can be executed first. Here
selection sort is used to sort the array of burst time bt.
Waiting time of the first element is zero, the remaining waiting time is calculated by using two for loop that
runs from 1 to in that controls the outer loop and the inner loop is controlled by another for loop that runs
from j=0 to j<i. Inside the loop, the waiting time is calculated by adding the burst time to the waiting time.
for(i=1;i<n;i++)
{
wt[i]=0;
for(j=0;j<i;j++)
wt[i]+=bt[j];
total+=wt[i];
}
Total is the addition of all the waiting time together. The average waiting time is calculated:
Basic Terminology:-
Completion Time: - It defines the time when processes complete their execution.
Turnaround Time: - It defines the time difference between the completion time (CT) and the
arrival time (AT).
Turn Around Time (TAT) = Completion Time (CT) - Arrival Time (AT)
Waiting Time: - It defines the total time between requesting action and acquiring the
resource. Waiting Time (WT) = Turn Around Time (TAT) - Burst Time (BT)
Response Time: - It is the time that defines at which time the system response to a process.
Algorithm: -
Step 1: - Organize all processes according to their arrival time in the ready queue. The queue structure of the
ready queue is based on the FIFO structure to execute all CPU processes.
Step 2: - Now, we push the first process from the ready queue to execute its task for a fixed time, allocated
by each process that arrives in the queue.
Step 3: - If the process cannot complete their task within defined time interval or slots because it is stopped
by another process that pushes from the ready queue to execute their task due to arrival time of the next
process is reached. Therefore, CPU saved the previous state of the process, which helps to resume from the
point where it is interrupted. (If the burst time of the process is left, push the process end of the ready queue).
Step 4: - Similarly, the scheduler selects another process from the ready queue to execute its tasks. When
a process finishes its task within time slots, the process will not go for further execution because the
process's burst time is finished.
Step 5: - Similarly, we repeat all the steps to execute the process until the work has finished.
Program for Round Robin Scheduling: -
#include<stdio.h>
#include<conio.h>
void main()
{
// initlialize the variable name
int i, NOP, sum=0,count=0, y, quant, wt=0, tat=0, at[10], bt[10], temp[10];
float avg_wt, avg_tat;
printf(" Total number of process in the system: ");
scanf("%d", &NOP);
y = NOP; // Assign the number of process to variable y
// Use for loop to enter the details of the process like Arrival time and the Burst Time
for(i=0; i<NOP; i++)
{
printf("\n Enter the Arrival and Burst time of the Process[%d]\n", i+1);
printf(" Arrival time is: \t"); // Accept arrival time
scanf("%d", &at[i]);
printf(" \nBurst time is: \t"); // Accept the Burst time
scanf("%d", &bt[i]);
temp[i] = bt[i]; // store the burst time in temp array
}
// Accept the Time qunat
printf("Enter the Time Quantum for the process: \t");
scanf("%d", &quant);
// Display the process No, burst time, Turn Around Time and the waiting
time printf("\n Process No \t\t Burst Time \t\t TAT \t\t Waiting Time ");
for(sum=0, i = 0; y!=0; )
{
if(temp[i] <= quant && temp[i] > 0) // define the conditions
{
sum = sum + temp[i];
temp[i] = 0;
count=1;
}
else if(temp[i] > 0)
{
temp[i] = temp[i] - quant;
sum = sum + quant;
}
if(temp[i]==0 && count==1)
{
y--; //decrement the process no.
printf("\nProcess No[%d] \t\t %d\t\t\t\t %d\t\t\t %d", i+1, bt[i], sum-at[i], sum-at[i]-bt[i]);
wt = wt+sum-at[i]-bt[i];
tat = tat+sum-at[i];
count =0;
}
if(i==NOP-1)
{
i=0;
}
else if(at[i+1]<=sum)
{
i++;
}
else
{
i=0;
}
}
// represents the average waiting time and Turn Around
time avg_wt = wt * 1.0/NOP;
avg_tat = tat * 1.0/NOP;
printf("\n Average Turn Around Time: \t%f", avg_wt);
printf("\n Average Waiting Time: \t%f", avg_tat);
getch();
}
Output: -
Description of Program: - In the above code, we ask the user to enter the number of processes and arrival
time and burst time for each process. We then calculate the waiting time and the turnaround time using the
round-robin algorithm.
The main part here is calculating the turnaround time and the waiting time. Turnaround time is calculated by
adding the total time taken and subtracting the arrival time.
The waiting time is calculated by subtracting the arrival time and burst time from the total and adding it to the
waiting time. This is how the round-robin scheduling takes place.
Example: - Consider the following set of processes that arrive at time 0 with the length of cpu burst time
given in milliseconds calculate the average waiting time using round robin scheduling with time quantum 4m.
Sec.
0 4 7 10 14 18 22 26 30
Command Mode: - When VI starts up, it is in Command Mode. This mode is where vi interprets any
characters we type as commands and thus does not display them in the window. This mode allows us to move
through a file, and to delete, copy, or paste a piece of text.
To enter into Command Mode from any other mode, it requires pressing the [Esc] key. If we press [Esc] when
we are already in Command Mode, then vi will beep or flash the screen.
Insert mode: - This mode enables you to insert text into the file. Everything that‟s typed in this mode is
interpreted as input and finally, it is put in the file. The vi always starts in command mode. To enter text, you
must be in insert mode. To come in insert mode you simply type i. To get out of insert mode, press the Esc
key, which will put you back into command mode.
Last Line Mode (Escape Mode): - Line Mode is invoked by typing a colon [:], while vi is in Command
Mode. The cursor will jump to the last line of the screen and VI will wait for a command. This mode enables
you to perform tasks such as saving files, executing commands.
Basic Vim commands: - The most simple commands allow you to open and close documents as well as
saving them. As with most other text editors, there are protections in place to help you avoid exiting the editor
without having saved what you're working on.
: help [keyword] - Performs a search of help documentation for whatever keyword you enter
: e [file] - Opens a file, where [file] is the name of the file you want opened
: w - Saves the file you are working on
: w [filename] - Allows you to save your file with the name you've defined
: wq - Save your file and close Vim
: q! - Quit without first saving the file you were working on
Vim commands for movement: - When using movement commands, you can put a number in front of
them to make Vim complete a command multiple times. For example, 5h will move your cursor five spaces to
the left, and 90j will put your cursor at the beginning of the 90th line down from where your cursor currently
is.
h - Moves the cursor to the left
l - Moves the cursor to the right
j - Moves the cursor down one line
k - Moves the cursor up one line
H - Puts the cursor at the top of the screen
M - Puts the cursor in the middle of the screen
L - Puts the cursor at the bottom of the screen
w - Puts the cursor at the start of the next
word
b - Puts the cursor at the start of the previous word
e - Puts the cursor at the end of a word
0 - Places the cursor at the beginning of a line
$ - Places the cursor at the end of a line
) - Takes you to the start of the next sentence
(- Takes you to the start of the previous sentence
} - Takes you to the start of the next paragraph or block of text
{- Takes you to the start of the previous paragraph or block of text
Ctrl + f - Takes you one page forward
Ctrl + b - Takes you one page back
gg - Places the cursor at the start of the file
G - Places the cursor at the end of the file
# - Where # is the number of a line, this command takes you to the line specified
Vim commands for editing: - Those who use Vim tend to use the term "yank" where most people would
use the terms copy and paste. Therefore, the command for copying a word is yw, which stands for yank word,
and the command for pasting whatever has been copied is p, meaning put. If you look up additional
commands in the future, it can be confusing if you don't know what yank and put mean when using Vim.
You also have two options for how to select text. You can either use commands like dd, which deletes a single
line, and yy, which copies a single line or you can highlight text and then copy it to the unnamed register. The
paste commands work the same whether you've highlighted text or used a command to automatically copy it
As with movement commands, putting a number in front of the command can increase the number of times a
task is completed. For instance, putting a number in front of yy will increase the number of lines copied,
so 5yy will copy five lines.
yy - Copies a line
yw - Copies a word
y$ - Copies from where your cursor is to the end of a line
v - Highlight one character at a time using arrow buttons or the h, k, j, l buttons
V - Highlights one line, and movement keys can allow you to highlight additional lines
p - Paste whatever has been copied to the unnamed register
d - Deletes highlighted text
dd - Deletes a line of text
dw - Deletes a word
D - Deletes everything from where your cursor is to the end of the line
d0 - Deletes everything from where your cursor is to the beginning of the line
dgg - Deletes everything from where your cursor is to the beginning of the file
dG - Deletes everything from where your cursor is to the end of the file
x - Deletes a single character
u - Undo the last operation; u# allows you to undo multiple actions
Ctrl + r - Redo the last undo
EXPERIMENT: - 8
Shell Commands
Shell: - A shell is a computer program which exposes an operating system's services to a human user or
other program. In general, operating system shells use either a command-line interface (CLI) or graphical user
interface (GUI), depending on a computer's role and particular operation. It is named a shell because it is the
outermost layer around the operating system. Command-line shells require the user to be familiar with
commands and their calling syntax, and to understand concepts about the shell-specific scripting language (for
example, bash).
A Shell provides you with an interface to the UNIX system. It gathers input from you and executes programs
based on that input. When a program finishes executing, it displays that program's output.
Shell is an environment in which we can run our commands, programs, and shell scripts. There are different
flavors of a shell, just as there are different flavors of operating systems. Each flavor of shell has its own set of
recognized commands and functions.
Shell Prompt: - The prompt, $, which is called the command prompt, is issued by the shell. While the
prompt is displayed, you can type a command.
Shell reads your input after you press Enter. It determines the command you want executed by looking at the
first word of your input. A word is an unbroken set of characters. Spaces and tabs separate words.
Following is a simple example of the date command, which displays the current date and time − $date Thu
Jun 25 08:30:19 MST 2009
Linux Shell Commands: - The shell is the Linux command line interpreter. It provides an interface
between the user and the kernel and executes programs called commands. For example, if a user enters ls then
the shell executes the ls command. The shell can also execute other programs such as applications, scripts, and
user programs (e.g., written in c or the shell programming language).
In Linux, commands are ways or instructions through which you can instruct your system to do some action.
Commands are executed in the command line.
Syntax: -
Command [option] [argument] : -There are some commands which don't have any option or don't accept
any argument such as 'clear' and 'pwd'.
Clear: - The 'clear' command clears out all the previous commands and outputs from terminal display.
Pwd: - The 'pwd' command stands for 'print working directory'. It doesn't accept any option or argument and
displays the detail of current working directory.
Types of Commands
External or built-in commands
Built-in: - commands are internal commands that are built-in the shell. Built-in commands are called from
the shell and executed directly within the shell itself. You can list all built-in commands with the help of 'help'
and 'compgen -b' command. Some example of built-in commands are 'pwd', 'help', 'type', 'set', 'unset', etc.
External: - commands are other than built-in commands. These commands are programs which have their
own binary and located in the file system. These are the commands that your system offer and are totally shell
independent. Mostly these commands reside in /bin, /sbin, /usr/sbin.
Linux 'type' commands tell us whether a command given to the shell is a built-in or external
command.
Syntax:
type <command>
Example:
type pwd
type cd
type man
type cat
type file
Look at above snapshot, commands like 'pwd' and 'cd' are built-in commands while commands 'man', 'cat', and
'file' are external commands.
Pwd stands for Print Working Directory. It prints the path of the working directory, starting from the root.
Pwd is shell built-in command (pwd) or an actual binary (/bin/pwd).
The cd (“change directory”) command is used to change the current working directory in Linux and other
Unix-like operating systems. It is one of the most basic and frequently used commands when working on
the Linux terminal.
Man command in Linux is used to display the user manual of any command that we can run on the terminal. It
provides a detailed view of the command which includes NAME, SYNOPSIS, DESCRIPTION, OPTIONS,
EXIT STATUS, RETURN VALUES, ERRORS, FILES, VERSIONS, EXAMPLES, AUTHORS and SEE
ALSO.
The cat (short for “concatenate“) command is one of the most frequently used command in Linux/Unix like
operating systems. cat command allows us to create single or multiple files, view contain of file, concatenate
files and redirect output in terminal or files.
A file type helps us in identifying the type of content that is saved in the file. Linux supports seven different
types of files. These file types are the Regular file, Directory file, Link file, Character special file, Block
special file, Socket file, and Named pipe file.
# echo $HOME
# echo $USER
We can tap into these environment variables from within your scripts by using the environment
variable‟s name preceded by a dollar sign.
Examples:-
echo “User info for userid: $USER”
echo UID: $UID
echo HOME: $HOME
Notice that the environment variables in the echo commands are replaced by their current values when the
script is run. Also notice that we were able to place the $USER system variable within the double quotation
marks in the first string, and the shell script was still able to figure out what we meant. There is a drawback to
using this method, however. Look at what happens in this example:
$ echo “The cost of the item is $15”
The cost of the item is 5
That is obviously not what was intended. Whenever the script sees a dollar sign within quotes, it assumes
you‟re referencing a variable. In this example the script attempted to display the variable $1 (which was
not defined), and then the number 5. To display an actual dollar sign, you must precede it with a backslash
character:
$ echo “The cost of the item is \$15”
The cost of the item is $15
User Defined Variables: - These variables are defined by users. A shell script allows us to set and use
our own variables within the script. Setting variables allows you to temporarily store data and use it
throughout the script, making the shell script more like a real computer program.
User variables can be any text string of up to 20 letters, digits, or an underscore character. User variables are
case sensitive, so the variable Var1 is different from the variable var1.
Values are assigned to user variables using an equal sign. No spaces can appear between the variable, the
equal sign, and the value (another trouble spot for novices). Here are a few examples of assigning values to
user variables:
var1=10
var2=-57
var3=testing
var4=“still more testing”
The shell script automatically determines the data type used for the variable value. Variables defined within
the shell script maintain their values throughout the life of the shell script but are deleted when the shell script
completes.
days=10
guest=”Katie”
echo “$guest checked in $days days ago”
$
Running the script produces the following output:
Katie checked in 10 days ago
$
Each time the variable is referenced, it produces the value currently assigned to it. It‟s important to
remember that when referencing a variable value you use the dollar sign, but when referencing the variable
to assign a value to it, you do not use the dollar sign.
The date and time are: Mon Jan 31 20:23:25 EDT 2011
EXPERIMENT: - 10
Shell Scripting- Input & Output
What is Redirection?
Redirection is a feature in Linux such that when executing a command, you can change the standard
input/output devices. The basic workflow of any Linux command is that it takes an input and give an output.
• The standard input (stdin) device is the keyboard.
• The standard output (stdout) device is the screen.
With redirection, the above standard input/output can be changed.
Redirections in Linux: -
The three types of redirection are:
1. Input redirection
2. Output redirection
3. Error redirection
Input Redirection: -
* The '<' symbol is used for input (STDIN) redirection.
* The example illustrate the use of input redirection:
cat <test1
* The cat command takes each line of the file „test1‟ as input and displays it on the screen.
* It is possible to specify that the standard input comes from a file.
Example: -
The mail program in Linux can help you send emails from the Terminal.
You can type the contents of the email using the standard device keyboard. But if you want to attach a File to
email you can use the input re-direction operator in the following format.
Mail -s "Subject" to-address < Filename
Output Redirection: -
The '>' symbol is used for output (STDOUT) redirection.
The example illustrate the use of output redirection:
cat test1 > out test
In Output Redirection: -
• The file to which the output is redirected is first created on the disk as an empty file and then the
output is sent to this file.
• If file already exist, its contents are deleted before the output is written to it.
• If you want to store permanent changes data into another file then you can use tr filter.
Syntax:-
tr „old_char‟ „new_char‟<filename> New file
Note: - Use the correct file name while redirecting command output to a file. If there is an existing file with
the same name, the redirected command will delete the contents of that file and then it may be overwritten."
If you do not want a file to be overwritten but want to add more content to an existing file, then you should
use '>>' operator.
The command appends the output to the existing file, out test:
Variables: -
In every programming language variables plays an important role , in Linux shell scripting we are using two
types of variables : System Defined Variables & User Defined Variables.
A variable in a shell script is a means of referencing a numeric or character value. And unlike formal
programming languages, a shell script doesn‟t require you to declare a type for your variables
Examples: -
echo “User info for userid: $USER”
echo UID: $UID
echo HOME: $HOME
Notice that the environment variables in the echo commands are replaced by their current values when the
script is run. Also notice that we were able to place the $USER system variable within the double quotation
marks in the first string, and the shell script was still able to figure out what we meant. There is a drawback to
using this method, however. Look at what happens in this example:
$ echo “The cost of the item is $15”
The cost of the item is 5
That is obviously not what was intended. Whenever the script sees a dollar sign within quotes, it assumes
you‟re referencing a variable. In this example the script attempted to display the variable $1 (which was
not defined), and then the number 5. To display an actual dollar sign, you must precede it with a backslash
character:
$ echo “The cost of the item is \$15”
The cost of the item is $15
The following points need to be considered when using the Arithmetic Operators: -
1. There must be spaces between the operators and the expressions. For example, 2+2 is not correct; it should
be written as 2 + 2.
2. You should use \ on the * symbol for multiplication.
5 Useful Ways to Do Arithmetic in Linux Terminal: -
1. Using Bash Shell: - The first and easiest way do basic math on the Linux CLI is a using double
parenthesis. Here are some examples where we use values stored in variables:
ADD=$((5 + 5))
echo $ADD
MUL=$((5 * 5))
echo $MUL
SUB=$((5 - 5))
echo $SUB
DIV=$((25 / 5))
echo $DIV
MOD=$((25 % 5))
echo $MOD
2. Using expr Command: - The expr command evaluates expressions and prints the value of provided
expression to standard output. We will look at different ways of using expr for doing simple math, making
comparison, incrementing the value of a variable and finding the length of a string.
The following are some examples of doing simple calculations using the expr
command. expr 3 + 7
expr 20 % 5
expr 4 \* 5
expr 10 – 5
expr 50 / 5
Next, we will cover how to make comparisons. When an expression evaluates to false, expr will print a value
of 0, otherwise it prints 1.
You can also use the expr command to increment the value of a variable. Take a look at the
following example (in the same way, you can also decrease the value of a variable).
NUM=$(( 25 + 25))
echo $NUM
NUM=$(expr $NUM + 25)
echo $NUM
echo '10+10' | bc
echo '10 % 20' | bc
echo '10 / 5' | bc
echo '(25 * 5) - 10' | bc.
The -l flag is used to the default scale (digits after the decimal point) to 20, for example:
echo '20/5‟ | bc
echo '12/6‟ | bc –l
4. Using factor Command: - The factor command is use to decompose an integer into prime factors. For
example:
factor 100
factor 125
factor 50
factor 1000
5. Using Awk Command: - Awk is one of the most prominent text-processing programs in GNU/Linux. It
supports the addition, subtraction, multiplication, division, and modulus arithmetic operators.
awk 'BEGIN {a = 5; b = 6; print "(a + b) = ", (a + b)}'
awk 'BEGIN { a = 5; b = 6; print "(a - b) = ", (a - b) }' awk 'BEGIN { a = 5; b = 6; print "(a * b) = ", (a * b)}'
awk 'BEGIN { a = 5; b = 6; print "(a / b) = ", (a / b) }' awk 'BEGIN { a = 5; b = 6; print "(a % b) = ", (a % b)}'
EXPERIMENT: - 13
Shell Scripting- if control statement programs
Linux - Shell Decision Making: - we will understand shell decision-making in Linux. While writing a
shell script, there may be a situation when you need to adopt one path out of the given two paths. So you need
to make use of conditional statements that allow your program to make correct decisions and perform the right
actions.
Linux Shell supports conditional statements which are used to perform different actions based on different
conditions. We will now understand decision-making statements here −
The if...else statements
If else statements are useful decision-making statements which can be used to select an option from a given
set of options.
Linux Shell supports following forms of if…else statement −
1. if...fi statement
2. if...else...fi statement
3. if...elif...else...fi statement
If statement: -
This block will process if specified condition is true.
Syntax: -
if [expression]
then
Statement(s) to be executed if expression is true
fi
fi statement is the fundamental control statement that allows Shell to make decisions and execute statements
conditionally.
The Shell expression is evaluated in the above syntax. If the resulting value is true, given statement(s) are
executed. If the expression is false then no statement would be executed. Most of the times, comparison
operators are used for making decisions.
It is recommended to be careful with the spaces between braces and expression. No space produces a syntax
error.
Example:-
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b“
fi if [ $a != $b ]
then
echo "a is not equal to b“
fi
The above script will generate the following result −
a is not equal to b.
If-else statement: - If specified condition is not true in if part then else part will be execute. The
if...else...fi statement is the next form of control statement that allows Shell to execute statements in a
controlled way and make the right choice.
Syntax:-
if [ expression ]
then Statement(s) to be executed if expression is true
else
Statement(s) to be executed if expression is not true
fi
The Shell expression is evaluated in the above syntax. If the resulting value is true, given statement(s) are
executed. If the expression is false, then no statement will be executed.
The Shell expression is evaluated in the above syntax. If the resulting value is true, given statement(s) are
executed. If the expression is false, then else statement will be executed.
Example: -
The above example can also be written using the if...else statement as follows −
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b"
else
echo "a is not equal to b"
fi
Upon execution, you will receive the following result −
a is not equal to b
If-elif-fi: - The if...elif...fi statement is the one level advance form of control statement that allows Shell to
make correct decision out of several conditions.
Syntax:-
if [ expression 1 ]
then
Statement(s) to be executed if expression 1 is true
elif [ expression 2 ]
then
Statement(s) to be executed if expression 2 is true
elif [ expression 3 ]
then Statement(s) to be executed if expression 3 is true else
Statement(s) to be executed if no expression is true fi
This code is just a series of if statements, where each if is part of the else clause of the previous statement.
Here statement(s) are executed based on the true condition, if none of the condition is true then else block is
executed.
Example:-
#!/bin/sh
a=10
b=20
if [ $a == $b ]
then
echo "a is equal to b“
elif [ $a -gt $b ]
then
echo "a is greater than b“
elif [ $a -lt $b ]
then
echo "a is less than b"
else
echo "None of the condition met"
fi
Upon execution, you will receive the following result −
a is less than b
EXPERIMENT: - 14
Shell Scripting- while control statement
Loops in scripting: - Loops are one of the fundamental concepts of programming languages. Loops are handy
when you want to run a series of commands a number of times until a particular condition is met.
In scripting languages such as Bash, loops are useful for automating repetitive tasks. There are three basic
loop constructs in Bash scripting, for loop , while loop, and until loop .
While Loop: - while loop is a control flow statement that allows code or commands to be executed
repeatedly based on a given condition. For example, run echo command 5 times or read text file line by line or
evaluate the options passed on the command line for a script.
For example following while loop will print welcome 5 times on screen:-
#!/bin/bash
x=1
while [ $x -le 3 ]
do
echo "Welcome $x times"
x=$(( $x + 1 ))
done
Output for the above program:-
x=1; while [ $x -le 5 ]; do echo "Welcome $x times" $(( x++ )); done
For Loop:-
Syntax:
for arg in list
do
command(s) ...
done
Where the value of the variable arg is set to the values provided in the list one at a time and the block of
statements executed. This is repeated until the list is exhausted.
Or
The for loop operates on lists of items. It repeats a set of commands for every item in a list.
Syntax:-
for var in word1 word2 ... wordN
do
Statement(s) to be executed for every word.
done
Here var is the name of a variable and word1 to wordN are sequences of characters separated by spaces
(words). Each time the for loop executes, the value of the variable var is set to the next word in the list of
words, word1 to wordN.
Example 1:-
Here is a simple example that uses the for loop to span through the given list of numbers −
Example 3:-
Using for loop to read an array
Any array variable contains a list of data that can be iterated very easily by using for-in loop. The following
example shows the use of for-in loop to read an array of string data. Here, each array value will fetch in the
variable, $language and print a message based on language in each iteration.
Output Of The program: -
Example: - 4
Using for loop to read a list of string values with spaces
When a list of a string is read by for-in loop and any string value contains space then the values split into
words based on space if the string value is not quoted with a single or double quotation. The following
example shows how a list of string values with space can be read by for-in loop. Here, one value of the list
contains space and it is printed with the space in the output.
Example: - 6
Using for loop with 3 parts and condition counter
Bash supports for loop with three parts like other standard programming languages. The first part contains
initialization, the second part contains the termination condition and the third part contains increment or
decrement operation. This loop is mainly used when the number of iteration is previously defined. In the
following example, for loop will iterate for 50 times and calculate the sum of 1 to 50. The result of the
summation will print as output.
Output of the Program: -
Example: - 7
Using Infinite for loop
When any loop is defined without any termination condition then the loop works as an infinite loop. The
following example shows the use of infinite for loop that does not contain any initialization, termination and
action parts. This type of for loop is defined by double semicolon (; ;). The following script will continue the
loop until the user types „quit‟ as input. The script will print any number from 1 to 20 that will take as input
otherwise it will print “The number is out of range”