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

os 39

The document contains a series of shell script labs (LAB-11 to LAB-43) that cover various scripting tasks such as listing directory contents, changing directories, managing user accounts, backing up files, and implementing CPU scheduling algorithms. Each lab includes a brief description, the corresponding shell script code, and expected output. The scripts demonstrate practical applications of shell scripting in system administration and process management.

Uploaded by

cognitrontech114
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)
7 views

os 39

The document contains a series of shell script labs (LAB-11 to LAB-43) that cover various scripting tasks such as listing directory contents, changing directories, managing user accounts, backing up files, and implementing CPU scheduling algorithms. Each lab includes a brief description, the corresponding shell script code, and expected output. The scripts demonstrate practical applications of shell scripting in system administration and process management.

Uploaded by

cognitrontech114
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/ 48

LAB -11

LAB – 11: Write a shell script to list contents of a directory.


CODE:
#!/bin/bash

echo "Enter the directory name:"

read dir

if [ -d "$dir" ]; then

echo "Contents of $dir:"

ls -l "$dir"

else

echo "Directory not found."

fi

OUTPUT:
LAB -12
LAB – 12: Write a shell script to change directory (cd) based
on user input.
CODE:
#!/bin/bash

echo "Enter the directory to navigate to:"

read dir

cd "$dir" || echo "Directory not found."

Pwd

OUTPUT:
LAB -13
LAB – 13: Write a shell script to navigate to the directory that
contains a specific file.
CODE:
#!/bin/bash

echo "Enter the filename to search:"

read filename

path=$(find /home/yash -type f -name "$filename" 2>/dev/null | head -n 1)

if [ -n "$path" ]; then

dir=$(dirname "$path")

cd "$dir" || exit

echo "Moved to: $dir"

else

echo "File not found."

fi

OUTPUT:
LAB -14
LAB – 14: Write a shell Script to display running processes
and their details.
CODE:
#!/bin/bash

echo "Displaying running processes:"

ps aux | less

OUTPUT:
LAB -15
LAB – 15: Write a shell Script to kill processes based on
name or ID.
CODE:
#!/bin/bash

echo "Enter process name or ID to kill:"

read proc

kill "$proc" 2>/dev/null || pkill "$proc"

OUTPUT:
LAB -16
LAB – 16: Write a shell Script to automatically Restart a
Process if it Crashes.
CODE:
#!/bin/bash

echo "Enter the process name to monitor:"

read process

while true; do

if ! pgrep "$process" > /dev/null; then

echo "$process has stopped. Restarting it…"

$process &

else

echo "$process is running."

fi

sleep 5

done

OUTPUT:
LAB -17
LAB – 17: Write a shell Script to create, modify, and delete
user accounts.
CODE:
#!/bin/bash
echo "Select an option:"
echo "1. Create user"
echo "2. Modify user"
echo "3. Delete user"
read option

case $option in
1)
echo "Enter username to create:"
read username
sudo useradd "$username"
echo "User $username created successfully."
;;

2)
echo "Enter username to modify:"
read username
echo "Enter the new username:"
read new_username
sudo usermod -l "$new_username" "$username"
echo "User $username renamed to $new_username."
;;

3)
echo "Enter username to delete:"
read username
sudo userdel "$username"
echo "User $username deleted successfully."
;;

*)
echo "Invalid option!"
;;
esac
OUTPUT:
LAB -18
LAB – 18: Write a shell Script to add or remove users from
groups.
CODE:
#!/bin/bash
echo "Select an option:"
echo "1. Add user to group"
echo "2. Remove user from group"
read option

case $option in
1)
echo "Enter the username to add:"
read username
echo "Enter the group name:"
read groupname
sudo usermod -a -G "$groupname" "$username"
echo "User $username added to group $groupname."
;;

2)
echo "Enter the username to remove:"
read username
echo "Enter the group name:"
read groupname
sudo gpasswd -d "$username" "$groupname"
echo "User $username removed from group $groupname."
;;

*)
echo "Invalid option!"
;;
esac
OUTPUT:
LAB -19
LAB – 19: Write a shell script to file Backup Script with
Custom Retention Policy.
CODE:
#!/bin/bash

backup_dir="/home/yash/backup"

mkdir -p "$backup_dir"

cp /home/yash/*.txt "$backup_dir"

# Delete backups older than 7 days

find "$backup_dir" -type f -mtime +7 -exec rm {} \;

echo "Backup done. Old backups cleaned."

OUTPUT:
LAB -20
LAB – 20: Write a shell script for database Backup and
Restore Script.
CODE:
#!/bin/bash

echo "1. Backup 2. Restore"


read choice
if [ "$choice" -eq 1 ]; then
mysqldump -u root -p your_db > ~/db_backup.sql
elif [ "$choice" -eq 2 ]; then
mysql -u root -p your_db < ~/db_backup.sql
fi

OUTPUT:
LAB -21
LAB – 21: Write a shell script for Network Configuration
Script with Error Handling Intercepting System Calls Using
Dynamic Tracing Tools.
CODE:
#!/bin/bash

echo "Enter IP address:"

read ip

if ip addr add "$ip"/24 dev eth0 2>/dev/null; then

echo "IP $ip assigned to eth0"

else

echo "Error assigning IP"

fi

OUTPUT:
LAB -22
LAB – 22: Write a shell Script to intercept system calls using
strace and log process ID, system call name, arguments, and
return values.

CODE:
#!/bin/bash

echo "Enter process ID to trace:"

read pid

strace -p "$pid" -o trace_log.txt

echo "System calls for PID $pid logged to trace_log.txt"

OUTPUT:
LAB -23
LAB – 23: Write a shell Script to intercept library calls using
ltrace and capture similar information.
CODE:
#!/bin/bash

echo "Enter command to trace:"

read cmd

ltrace $cmd > libtrace_log.txt

echo "Library calls traced to libtrace_log.txt"

OUTPUT:
LAB -24
LAB – 24: Write a shell script to monitor process forks using
“ps”.
CODE:
#!/bin/bash
ps -eo ppid,pid,cmd --sort=ppid

OUTPUT:
LAB -25
LAB – 25: Write a shell script to collect packet counts using
tools like tcpdump or tshark”.
CODE:
#!/bin/bash
sudo tcpdump -c 10 -i any > packets.log

echo "10 packets captured to packets.log"


OUTPUT:
LAB -26
LAB – 26: Write a shell script to measure bandwidth usage
using iftop or nload.
CODE:
#!/bin/bash
sudo iftop -i eth0

OUTPUT:
LAB -27
LAB – 27: Write a shell script to analyze latency using ping
or traceroute.
CODE:
#!/bin/bash
echo "Enter host to ping:"
read host
ping -c 4 "$host"

OUTPUT:
LAB -28
LAB – 28: Write a shell script to check connection status
using netstat or ss.
CODE:
#!/bin/bash
netstat -tulnp

OUTPUT:
LAB -29
LAB – 29: Write a shell script to visualize network data using
gnuplot or matplotlib for graphs and charts.
CODE:
#!/bin/bash
echo "Generating dummy network data..."
echo -e "Time\tPackets" > net_data.txt
for i in {1..10}; do
echo -e "$i\t$((RANDOM % 100))" >> net_data.txt
done

echo "Plotting with gnuplot..."


gnuplot -persist <<-EOFMarker
set title "Network Packet Count"
set xlabel "Time"
set ylabel "Packets"
plot "net_data.txt" using 1:2 with lines title "Packets"
EOFMarker

OUTPUT:
LAB -30
LAB – 30: Print Current Date and Time: Write a shell script
to Display the current date and time using date command.
CODE:
#!/bin/bash
date

OUTPUT:
LAB -31
LAB – 31: Generate Random Password: Write a shell script
to Use openssl rand to generate a random password.
CODE:
#!/bin/bash
openssl rand -base64 12

OUTPUT:
LAB -32
LAB – 32: Write a shell script to show system information
like kernel version, CPU info, etc., using uname, lscpu, etc.
CODE:
#!/bin/bash
uname –a
Iscpu

OUTPUT:
LAB -33
LAB – 33: Display System Uptime: Write a shell script to
show system uptime using uptime command.
CODE:
#!/bin/bash
Uptime

OUTPUT:
LAB -34
LAB – 34: View Disk Usage: Write a shell script to Display
disk space usage of files and directories using du and df
commands.
CODE:
#!/bin/bash
df -h
du –sh*

OUTPUT:
LAB -35
LAB – 35: Check System Load: Write a shell script to
monitor system load averages using w or top commands.
CODE:
#!/bin/bash
Top -b n1 | head -n 10

OUTPUT:
LAB -36
LAB – 36: Display Calendar: Write a shell script to show the
calendar for a specific month using cal.
CODE:
#!/bin/bash
cal

OUTPUT:
LAB -37
LAB – 37: Search Text in Files: Write a shell script to Use
grep to search for specific text within files.
CODE:
#!/bin/bash
echo "Enter text to search:"
read text
grep -rnw '.' -e "$text"

OUTPUT:
LAB -38
LAB – 38: Count Lines in a File: Write a shell script to Use
wc -l to count the number of lines in a file.
CODE:
#!/bin/bash
echo "Enter filename:"
read file
wc -l script.sh

OUTPUT:
LAB -39
LAB – 39: Check System Users: Write a shell script to
Display currently logged-in users using who or w commands.
CODE:
#!/bin/bash
who

OUTPUT:
LAB -40
LAB – 40: Implement FCFS CPU Scheduling algorithm.
CODE:
#include <stdio.h>
void main()
{
int p;
printf("Enter the number of processes: ");
scanf("%d", &p);
int p1, al, bl;
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%d %d %d", &p1, &al, &b1);
int p2, a2, b2;
printf("Enter the Process Name, Arrival Time & Burst Time:");
scanf("%d %d %d", &p2, &a2, 8b2);
printf("Process Name\tArrival Time\tBurst Time\n");
printf("%d\t %d\t %d\n", p1, a1, b1);
printf(" %d\t %d\t %d\n", p2, a2, b2);
printf("PName
Arrtime Bursttime Start WT TAT Finish\n");
int start1 = al;
int finishl1 = start1 + b1;
int tati finish1 a1;
int wt1 = tat1 - b1;
int start2 = finish1;
int finish2 = start2 + b2;
int tat2 finish2 a2;
int wt2 tat2 - b2;
printf("%d\t %d\t\t %d\t %d\t %d\t %d\t %d\n", p1, a1, b1, start1, wt1, tat1,
finish1);
printf("%d\t %d\t\t %d\t %d\t %d\t %d\t %d\n", p2, a2, b2, start2, wt2, tat2,
finish2);
float aw (wt1+wt2)/2.0;
float at (tat1 + tat2)/2.0;
printf("Average Waiting time:%f\n", aw);
printf("Average Turn Around Time:%f", at);
}

OUTPUT:
LAB -41
LAB – 41: Implement the given CPU Scheduling algorithm a)
SJF b) Priority Based.
CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 50
void main(){
int
bTime[max],aTime[max],n,i,j,temp,sTime[max],fTime[max],wTime[max],taTi
me[max];
int totwTime=0, tottaTime=0;
float awTime,ataTime;
char pName[max][max],t[max];
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter process name, arrival time & execution time:");
scanf("%s%d%d",pName[i],&aTime[i],&bTime[i]);
}
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(bTime[i]<bTime[j]){
temp=aTime[i];
aTime[i]=aTime[j];
aTime[j]=temp;
temp=bTime[i];
bTime[i]=bTime[j];
bTime[j]=temp;
strcpy(t,pName[i]);
strcpy(pName[i],pName[j]);
strcpy(pName[j],t);
}
}
}
for(i=0;i<n;i++){
if(i==0){
sTime[i]=aTime[i];
}
else{
sTime[i]=fTime[i-1];
}
wTime[i]=sTime[i]-aTime[i];
fTime[i]=sTime[i]+bTime[i];
taTime[i]=fTime[i]-aTime[i];
totwTime+=wTime[i];
tottaTime+=taTime[i];
}
awTime=(float)totwTime/n;
ataTime=(float)tottaTime/n;
printf("Pname\tarrivaltime\texecutiontime\twaitingtime\ttatime\n");
for(i=0;i<n;i++){
printf("%s\t%5d\t\t%5d\t\t%5d\t\t%5d\n",pName[i],aTime[i],bTime[i],w
Time[i],taTime[i]);
}
printf("Average waiting time is:%f\n",awTime);
printf("Average turnaroundtime is:%f\n",ataTime);
}

OUTPUT:
LAB -42
LAB – 42: Implement Multi-level Queue CPU Scheduling
algorithm.
CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 50
void main(){
int
bTime[max],aTime[max],n,i,j,temp,sTime[max],fTime[max],wTime[max],taTi
me[max],pr[max];
int towTime=0,totaTime=0;
float awTime,ataTime;
char pName[max][max],t[max];
printf("Enter the number of process:");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("Enter process name,arrivaltime,execution time &
priority:");
scanf("%s%d%d%d",pName[i],&aTime[i],&bTime[i],&pr[i]); }
for(i=0;i<n;i++){
if(i == 0){
sTime[i]=aTime[i];
wTime[i]=sTime[i]-aTime[i];
fTime[i]=sTime[i]+bTime[i];
taTime[i]=fTime[i]-aTime[i];
} else{
sTime[i]=fTime[i-1];
wTime[i]=sTime[i]-aTime[i];
fTime[i]=sTime[i]+bTime[i];
taTime[i]=fTime[i]-aTime[i];
}
towTime+=wTime[i];
totaTime+=taTime[i]; }
awTime=(float)towTime/n;
ataTime=(float)totaTime/n;
printf("Pname\tarrivaltime\texecutiontime\tpriority\twaitingtime\ttatime\n
");
for(i=0;i<n;i++){
printf("%s\t%5d\t\t%5d\t\t%5d\t\t%5d\t\t%5d\n",pName[i],aTime[i],bTi
me[i],pr[i],wTime[i],taTime[i]);
}
printf("Average waiting time is:%f\n",awTime);
printf("Average turnaroundtime is:%f\n",ataTime);
}

OUTPUT:
LAB -43
LAB – 43: Implement PRIORITY CPU Scheduling
Algorithm (For both Pre-emptive and non-pre-emptive).
CODE:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#define max 50
void main(){
int i, n,
sum=0,count=0,y,quant,wt=0,tat=0,aTime[max],bTime[max],temp[max],wTime
[max],rem_bTime[max],taTime[max];
float avg_wt,avg_tat;
printf("Enter Total Number of Processes: ");
scanf("%d",&n);
y=n;
for(i=0;i<n;i++){
printf("Enter Details of Process[%d]: Arrival Time:\t",i+1);
scanf("%d",&aTime[i]);
printf("Burst Time:\t");
scanf("%d",&bTime[i]);
temp[i]=bTime[i];
}
printf("Enter Time Quantum:\t");
scanf("%d",&quant);
printf("Process ID\t\tBurst Time\t Turnaround Time\t Waiting Time\n");
for(sum=0,i=0;y!=0;){
if(temp[i]<=quant && temp[i]>0){
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--;
printf("Process[%d]\t\t%d\t\t %d\t\t\t
%d\n",i+1,bTime[i],sum-aTime[i],sum-aTime[i]-bTime[i]);
wt=wt+sum-aTime[i]-bTime[i];
tat=tat+sum-aTime[i];
count=0;
}
if(i==n-1){
i=0;
}
else if(aTime[i+1]<=sum){
i++;
}
else{
i=0;
}
}
avg_wt=(float)wt/n;
avg_tat=(float)tat/n;
printf("Average Waiting Time:\t%f\n",avg_wt);
printf("Avg Turnaround Time:\t%f\n",avg_tat);
}

OUTPUT:
LAB -44
LAB – 44: Implement Round-Robin CPU Scheduling
Algorithm.
CODE:
#include<stdio.h>
#define max 50
void main(){
int p[max],bTime[max],su[max],wTime[max],taTime[max],i,k,n,temp;
float avg_wt,avg_tat;
printf("Enter the number of processes:");
scanf("%d",&n);
for(i=0;i<n;i++){
p[i]=i;
printf("Enter the Burst Time of Process %d:",i);
scanf("%d",&bTime[i]);
printf("System/User Process (0/1) ?");
scanf("%d",&su[i]);
}
for(i=0;i<n;i++){
for(k=i+1;k<n;k++){
if(su[i]>su[k]){
temp=p[i];
p[i]=p[k];
p[k]=temp;
temp=bTime[i];
bTime[i]=bTime[k];
bTime[k]=temp;
temp=su[i];
su[i]=su[k];
su[k]=temp;
}
}
}
avg_wt=wTime[0]=0;
avg_tat=taTime[0]=bTime[0];
for(i=1;i<n;i++){
wTime[i]=wTime[i-1]+bTime[i-1];
taTime[i]=taTime[i-1]+bTime[i];
avg_wt=avg_wt+wTime[i];
avg_tat=avg_tat+taTime[i];
}
printf("PROCESS\t\t SYSTEM/USER PROCESS \tBURST
TIME\tWAITING TIME\tTURNAROUND TIME");
for(i=0;i<n;i++){
printf("\n%d \t\t %d \t\t %d \t\t %d \t\t %d
",p[i],su[i],bTime[i],wTime[i],taTime[i]);
}
printf("\nAverage Waiting Time is --- %f",avg_wt/n);
printf("\nAverage Turnaround Time is --- %f", avg_tat/n);
}
OUTPUT:
LAB -45
LAB – 45: Implement Multilevel Queue CPU Scheduling
Algorithm.
CODE:
#include <stdio.h>

struct Process {
int id, arrival, burst, priority, queue;
int remaining, waiting, turnaround;
};

void sortByArrival(struct Process p[], int n) {


for(int i = 0; i < n-1; i++)
for(int j = i+1; j < n; j++)
if(p[i].arrival > p[j].arrival) {
struct Process temp = p[i];
p[i] = p[j];
p[j] = temp;
}
}

int main() {
int n;
printf("Enter total number of processes: ");
scanf("%d", &n);
struct Process p[n];
for(int i = 0; i < n; i++) {
printf("\nProcess %d\n", i+1);
p[i].id = i+1;
printf("Arrival Time: ");
scanf("%d", &p[i].arrival);
printf("Burst Time: ");
scanf("%d", &p[i].burst);
printf("Priority (lower = higher priority): ");
scanf("%d", &p[i].priority);
printf("Queue Number (0=System, 1=User): ");
scanf("%d", &p[i].queue);
p[i].remaining = p[i].burst;
}

sortByArrival(p, n);

int time = 0, completed = 0;


printf("\nGantt Chart:\n");

while(completed < n) {
int idx = -1;
int highestPriority = 9999;

for(int i = 0; i < n; i++) {


if(p[i].arrival <= time && p[i].remaining > 0) {
if(p[i].queue == 0 && p[i].priority < highestPriority) {
highestPriority = p[i].priority;
idx = i;
}
}
}

// if no system queue process found, check user queue


if(idx == -1) {
for(int i = 0; i < n; i++) {
if(p[i].arrival <= time && p[i].remaining > 0) {
if(p[i].queue == 1 && p[i].priority < highestPriority) {
highestPriority = p[i].priority;
idx = i;
}
}
}
}

if(idx != -1) {
printf("| P%d ", p[idx].id);
p[idx].remaining--;
time++;

if(p[idx].remaining == 0) {
completed++;
p[idx].turnaround = time - p[idx].arrival;
p[idx].waiting = p[idx].turnaround - p[idx].burst;
}
} else {
printf("| Idle ");
time++;
}
}
printf("|\n");

printf("\nProcess\tAT\tBT\tPri\tQ\tWT\tTAT\n");
for(int i = 0; i < n; i++) {
printf("P%d\t%d\t%d\t%d\t%d\t%d\t%d\n",
p[i].id, p[i].arrival, p[i].burst, p[i].priority, p[i].queue, p[i].waiting,
p[i].turnaround);
}

return 0;
}

OUTPUT:

You might also like