Shell Scripting Practical 0079docx
Shell Scripting Practical 0079docx
STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
is: $perimeter"
calculate_rectangle
#!/bin/bash
calculate_rectangle(){
echo "Enter the length of the rectangle:"
read length echo "Enter the width of the
rectangle:" read width area=$((length *
width)) perimeter=$(((2 * length) + (2 *
width))) echo "The area of the rectangle
is: $area"
echo "The perimeter of the rectangle is: $perimeter"
}
calculate_rectangle
Output:
STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
Script:
#!/bin/bash
"Usage: $0 <filename>"
found!"
exit 1 fi
#!/bin/bash
Output:
Practical 3: Write a shell script that calculates the sum and average of an array
of numbers
STEP 1: Create a file named - nano array.sh
STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
Script:
#!/bin/bash sum=0 count= echo "enter
fi
#!/bin/bash
sum=0
count=0
Output:
#!/bin/bash
Output:
Practical 5: Write a shell script that finds prime numbers inside a user-specified
range.
#!/bin/bash
is_prime() {
num=$1
if [ $num -lt 2 ]; then
return 1
fi
for ((i=2; i<=num/2; i++)); do
if [ $((num % i)) -eq 0 ]; then
return 1
fi
done
return 0
}
Output:
Fi
#!/bin/bash
Output:
Practical 7: Write shell script that allows users to create, delete, and list files in
a directory.
ls
;;
*)
echo "Invalid option."
;;
esac
#!/bin/bash
e
c
h
o
"
C
h
o
o
s
e
a
n
o
p
e
r
a
t
i
o
n
:
"
e
c
h
o
"
1
.
C
r
e
a
t
e
a
f
i
l
e
"
e
c
h
o
"
2
.
D
e
l
e
t
e
f
i
l
e
"
e
c
h
o
"
3
.
if [ -f
"$file
nam
e" ];
then
rm
"$file
nam
e"
echo
"File
'$file
nam
e'
delet
ed."
else
echo
"File
'$filename'
not found."
fi ;;
3)
echo
"Files in the
current
directory:"
ls ;;
*)
echo "Invalid
option."
;;
esac
Output:
Practical 8: Write a shell script that Count Lines in Each File in a Directory.
fi
done
#!/bin/bash
echo "Files in the current directory with line count:" for file in *; do if [ -f "$file" ]; then
line_count=$(wc -l < "$file") echo "$file: $line_count lines" fi done
Output:
! -f
exit 1 fi
echo "Enter the text to find:" read find_tex echo "Enter the text
to replace it with:" read replace_text sed -i
"s/$find_text/$replace_text/g" "$filename" echo "Replaced
'$find_text' with '$replace_text' in $filename."
#!/bin/bash
echo "Enter the file name:" read filename
if [ ! -f "$filename" ]; then
echo "File not found!" exit
1 fi
echo "Enter the text to find:" read find_text echo "Enter the
text to replace it with:" read replace_text sed -i
"s/$find_text/$replace_text/g" "$filename" echo "Replaced
'$find_text' with '$replace_text' in $filename."
Output:
Output:
echo
Fi
#!/bin/bash
Output:
Practical 12: Write a shell script to change directory (cd) based on user input.
STEP 1: Create - nano change_dir.sh STEP 2:
Save and exit.
STEP 3: Execute - chmod +x change_dir.sh
STEP 4: Run - ./change_dir.sh
Script:
fi
#!/bin/bash
if [ -d "$path" ]; then
cd "$path"
echo "Now in directory: $(pwd)"
else echo "Directory not found." fi
Output:
found."
fi
Output:
#!/bin/bash echo "Enter filename to search:" read file
Script:
#!/bin/bash
ps aux
#!/bin/bash
Output:
fi
#!/bin/bash
Output:
starting it..."
./"$process" &
fi
sleep 5
done
#!/bin/bash
process="your_app"
Output:
;;
"User
$user modified."
;;
;;
*)
;;
esac
#!/bin/bash
case $action in
add)
sudo useradd "$user"
echo "User $user added."
;;
modify) echo "Enter new shell (e.g.,
/bin/bash):" read shell sudo usermod
-s "$shell" "$user" echo "User
$user modified."
;;
delete) sudo userdel "$user"
echo "User $user deleted."
;;
*)
echo "Invalid action."
;;
esac
Output:
Practical 18: Write a shell Script to add or remove users from groups.
$group."
;;
;;
*)
;;
Esac
#!/bin/bash
case $action in
add)
sudo usermod -aG "$group" "$user"
echo "User $user added to group $group."
;;
remove) sudo gpasswd -d
"$user" "$group" echo "User $user removed
from group $group."
;;
*)
echo "Invalid action."
;;
esac
Output:
NoorainWakil@linux:~$ Choose: add | remove
NoorainWakil@linux:~$ add NoorainWakil@linux:~$ Enter username:
NoorainWakil@linux:~$ testuser NoorainWakil@linux:~$ Enter group:
NoorainWakil@linux:~$ sudo
NoorainWakil@linux:~$ User testuser added to group sudo.
Practical 19: Write a shell script to file Backup Script with Custom Retention
Policy
"$backup_dir" filename="backup_$(date
+%Y%m%d%H%M%S).tar.gz" tar -czf
"$backup_dir/$filename" ./important_data
"$backup_dir"
Output:
Practical 20: Write a shell script for database Backup and Restore Script.
fi
#!/bin/bash
echo "Choose: backup | restore" read action
if [ "$action" == "backup" ]; then
mysqldump -u root -p your_database > db_backup.sql
echo "Backup completed." elif [ "$action" == "restore" ]; then
mysql -u root -p your_database < db_backup.sql
echo "Restore completed." else echo "Invalid
action." fi
Output:
Practical 21: Write a shell script for Network Configuration Script with Error
Handling
#!/bin/bash
Output:
Practical 22: Write a shell Script to intercept system calls using strace and log
process ID, system call name, arguments, and return values.
#!/bin/bash echo
#!/bin/bash
echo "Script name: $0" echo "First arg: $1" echo "Second arg: $2" echo "Third arg: $3"
Output:
Practical 23: Write a shell Script to intercept library calls using ltrace and
capture similar information.
./options.sh [option]"
;;
-v|--version)
;;
*)
;;
esac
#!/bin/bash case
$1 in
-h|--help)
echo "Usage: ./options.sh [option]"
;;
-v|--version)
echo "Version 1.0"
;;
*)
echo "Invalid option"
;; esac
Output:
Practical 24: Write a shell script to monitor process forks using “ps”
successfully."
#!/bin/bash
sudo apt update && sudo apt upgrade -y echo "System updated successfully."
Output:
Practical 25: Write a shell script to collect packet counts using tools like
tcpdump or tshark.
STEP 1: Create - nano extract_errors.sh STEP 2:
Save and exit.
STEP 3: Execute - chmod +x extract_errors.sh STEP
4: Run - ./extract_errors.sh
Script:
#!/bin/bash logfile="/var/log/syslog"
grep -i "error" "$logfile" > error_logs.txt
#!/bin/bash
Output:
Practical 26: Write a shell script to measure bandwidth usage using iftop or
nload.
STEP 1: Create - nano disk_alert.sh STEP 2:
Save and exit.
STEP 3: Execute - chmod +x disk_alert.sh STEP
4: Run - ./disk_alert.sh
Script:
#!/bin/bash threshold=80 usage=$(df / | grep / | awk '{ print
fi
#!/bin/bash
threshold=80
Output:
Practical 27: Write a shell script to analyze latency using ping or traceroute.
STEP 1: Create - nano compress_logs.sh STEP 2:
Save and exit.
STEP 3: Execute - chmod +x compress_logs.sh
STEP 4: Run - ./compress_logs.sh Script:
#!/bin/bash logdir="/var/log" archive="logs_$(date
echo
#!/bin/bash
logdir="/var/log" archive="logs_$(date
+%Y%m%d).tar.gz"
Output:
Practical 28: Write a shell script to check connection status using netstat or ss.
STEP 1: Create - nano monitor_resources.sh STEP 2:
Save and exit.
STEP 3: Execute - chmod +x monitor_resources.sh STEP
4: Run - ./monitor_resources.sh
Script:
#!/bin/bash echo "CPU and
Memory Usage:" top -b -n1 |
head -n 10
#!/bin/bash
Output:
Practical 29: Write a shell script to visualize network data using gnuplot or
matplotlib for graphs and charts.
STEP 1: Create - nano install_software.sh STEP 2:
Save and exit.
STEP 3: Execute - chmod +x install_software.sh STEP
4: Run - sudo ./install_software.sh
Script:
#!/bin/bash echo "Installing curl..." sudo
"Installation complete."
#!/bin/bash
echo "Installing curl..." sudo apt update sudo apt install curl -y echo "Installation complete."
Output:
Practical 30: Print Current Date and Time: Write a shell script to Display the
current date and time using date command.
STEP 1: Create - nano weather.sh STEP 2:
Save and exit.
STEP 3: Execute - chmod +x weather.sh STEP
4: Run - ./weather.sh
Script:
#!/bin/bash echo
"Enter city name:" read
#!/bin/bash
curl "wttr.in/$city"
Output:
Practical 31: Generate Random Password: Write a shell script to Use openssl
rand to generate a random password.
STEP 1: Create - nano random_password.sh STEP 2:
Save and exit.
STEP 3: Execute - chmod +x random_password.sh STEP
4: Run - ./random_password.sh
Script:
#!/bin/bash length=12 echo "Generating a random password
$password"
#!/bin/bash
length=12
Output: