[go: up one dir, main page]

0% found this document useful (0 votes)
10 views44 pages

Shell Scripting Practical 0079docx

The document outlines a series of practical shell scripting exercises, each detailing steps to create and execute scripts for various tasks such as calculating area and perimeter of a rectangle, counting words and lines in a file, calculating Fibonacci sequences, and more. Each practical includes code snippets, execution commands, and example outputs. The exercises aim to enhance understanding of shell scripting and file manipulation in a Linux environment.

Uploaded by

nisthasingh2609
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views44 pages

Shell Scripting Practical 0079docx

The document outlines a series of practical shell scripting exercises, each detailing steps to create and execute scripts for various tasks such as calculating area and perimeter of a rectangle, counting words and lines in a file, calculating Fibonacci sequences, and more. Each practical includes code snippets, execution commands, and example outputs. The exercises aim to enhance understanding of shell scripting and file manipulation in a Linux environment.

Uploaded by

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

PROGRAM LIST

S.No. Program Name Submission Grade Signature


Date
Practical 1: Write a shell script to determine the Area and Perimeter of a
Rectangle.
STEP 1: Create a file named - nano calculate_rectangle

STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.

STEP 3: To execute the file type - chmod +x calculate_rectangle

STEP 4: For output type - ./calculate_rectangle Script:


#!/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
#!/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:

NoorainWakil@linux:~$ Enter the length of the rectangle:


NoorainWakil@linux:~$ 6
NoorainWakil@linux:~$ Enter the width of the rectangle:
NoorainWakil@linux:~$ 2
NoorainWakil@linux:~$ The area of the rectangle is: 12
NoorainWakil@linux:~$ The perimeter of the rectangle is: 16
Practical 2: Write a shell script to count the words, characters, and lines in the
file.
STEP 1: Create a file named - nano count

STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.

STEP 3: To execute the file type - chmod +x count

STEP 4: For output type - ./count filename.txt

Script:
#!/bin/bash

if [ $# -eq 0 ]; then echo

"Usage: $0 <filename>"

exit 1 fi if [ ! -f "$1" ]; then echo "Error: File '$1' not

found!"

exit 1 fi

words=$(wc -w < "$1") characters=$(wc -m < "$1") lines=$


(wc -l < "$1") echo "File: $1" echo "Words:
$words" echo "Characters:
$characters" echo "Lines: $lines"

#!/bin/bash

if [ $# -eq 0 ]; then echo


"Usage: $0 <filename>" exit
1
fi
if [ ! -f "$1" ]; then echo
"Error: File '$1' not found!" exit
1
fi

words=$(wc -w < "$1") characters=$(wc


-m < "$1") lines=$(wc -l < "$1")

echo "File: $1" echo "Words:


$words" echo "Characters:
$characters" echo "Lines:
$lines"

Output:

NoorainWakil@linux:~$ File: filename.txt


NoorainWakil@linux:~$ Words: 24
NoorainWakil@linux:~$ Characters: 145
NoorainWakil@linux:~$ Lines: 5

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.

STEP 3: To execute the file type - chmod +x array.sh


STEP 4: For output type - ./array.sh

Script:
#!/bin/bash sum=0 count= echo "enter

numbers for array(using space): " read -a

numbers for num in "${numbers[@]}"; do

sum=$((sum + num)) count=$((count +1))

done if [ $count -ne 0 ]; then average=$(echo

"scale=2; $sum / $count" | bc) else average=0

fi

echo "Sum: $sum" echo "Average:


$average"

#!/bin/bash

sum=0
count=0

echo "enter numbers for array(using space): "


read -a numbers

for num in "${numbers[@]}"; do


sum=$((sum + num)) count=$
((count +1))
done

if [ $count -ne 0 ]; then


average=$(echo "scale=2; $sum / $count"
| bc) else average=0 fi

echo "Sum: $sum" echo


"Average: $average"

Output:

NoorainWakil@linux:~$ enter numbers for array(using space):


NoorainWakil@linux:~$ 10 20 30 40 50
NoorainWakil@linux:~$ Sum: 150 NoorainWakil@linux:~$ Average: 30.00

Practical 4: Write a shell script to calculate the Fibonacci sequence.

STEP 1: Create a file named - nano fib.sh


STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
STEP 3: To execute the file type - chmod +x fib.sh STEP
4: For output type - ./fib.sh Script:
#!/bin/bash fibonacci() { n=$1 a=0
b=1 echo "Fibonacci sequence up to $n
terms:" for (( i=0; i<n; i++ )) do
echo -n "$a " fn=$((a + b)) a=$b
b=$fn done echo ""
}
echo -n "Enter the number of terms: " read num
if [[ $num =~ ^[0-9]+$ && $num -gt 0 ]]; then
fibonacci $num else echo "Please enter a
valid positive integer."
Fi

#!/bin/bash

fibonacci() { n=$1 a=0 b=1 echo


"Fibonacci sequence up to $n terms:" for
(( i=0; i<n; i++ )) do
echo -n "$a " fn=$
((a + b)) a=$b
b=$fn
done echo ""
}

echo -n "Enter the number of terms: " read num

if [[ $num =~ ^[0-9]+$ && $num -gt 0 ]]; then fibonacci


$num else
echo "Please enter a valid positive integer." fi

Output:

NoorainWakil@linux:~$ Enter the number of terms: 7 NoorainWakil@linux:~$


Fibonacci sequence up to 7 terms:
NoorainWakil@linux:~$ 0 1 1 2 3 5 8

Practical 5: Write a shell script that finds prime numbers inside a user-specified
range.

STEP 1: Create a file named - nano find_prime.sh


STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
STEP 3: To execute the file type - chmod +x find_prime.sh STEP
4: For output type - ./find_prime.sh Script:
#!/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
}
echo "Enter the range (start and end):" read start
end
echo "Prime numbers between $start and $end are:"
for ((num=$start; num<=$end; num++)); do
is_prime $num
if [ $? -eq 0 ]; then
echo $num fi
done

#!/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
}

echo "Enter the range (start and end):" read start


end

echo "Prime numbers between $start and $end are:"


for ((num=$start; num<=$end; num++)); do is_prime
$num
if [ $? -eq 0 ]; then
echo $num
fi
done

Output:

NoorainWakil@linux:~$ Enter the range (start and end):


NoorainWakil@linux:~$ 10 50
NoorainWakil@linux:~$ Prime numbers between 10 and 50 are:
NoorainWakil@linux:~$ 11
NoorainWakil@linux:~$ 13
NoorainWakil@linux:~$ 17
NoorainWakil@linux:~$ 19
NoorainWakil@linux:~$ 23
NoorainWakil@linux:~$ 29
NoorainWakil@linux:~$ 31
NoorainWakil@linux:~$ 37
NoorainWakil@linux:~$ 41
NoorainWakil@linux:~$ 43
NoorainWakil@linux:~$ 47

Practical 6: Write a shell script to determine whether a given string is


palindrome.

STEP 1: Create a file named - nano palindrome.sh


STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
STEP 3: To execute the file type - chmod +x palindrome.sh STEP
4: For output type - ./palindrome.sh Script:
#!/bin/bash echo "Enter a string:" read str

reversed_str=$(echo "$str" | rev) if [ "$str" ==

"$reversed_str" ]; then echo "$str is a palindrome."

else echo "$str is not a palindrome."

Fi

#!/bin/bash

echo "Enter a string:" read str

reversed_str=$(echo "$str" | rev)

if [ "$str" == "$reversed_str" ]; then


echo "$str is a palindrome." else
echo "$str is not a palindrome." fi

Output:

NoorainWakil@linux:~$ Enter a string:


NoorainWakil@linux:~$ madam
NoorainWakil@linux:~$ madam is a palindrome.

Practical 7: Write shell script that allows users to create, delete, and list files in
a directory.

STEP 1: Create a file named - nano file_operations.sh


STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
STEP 3: To execute the file type - chmod +x file_operations.sh STEP
4: For output type - ./file_operations.sh Script:
#!/bin/bash echo "Choose
an operation:" echo "1.
Create a file" echo "2. Delete
a file" echo "3. List files" read
choice case $choice in
1) echo "Enter the name of the file to
create:" read filename touch
"$filename" echo
"File '$filename' created."
;;
2)
echo "Enter the name of the file to delete:"
read filename
if [ -f "$filename" ]; then rm
"$filename" echo "File
'$filename' deleted." else
echo "File '$filename' not found."
fi ;;
3)
echo "Files in the current 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
.

List files" read


choice
case $choice in
1)
echo "Enter
the name of the
file to create:"
read filename
touch "$filename"
echo
"File '$filename'
created."
;;
2)
echo "Enter
the name of the
file to delete:"
read filename

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:

NoorainWakil@linux:~$ Choose an operation:


NoorainWakil@linux:~$ 3
NoorainWakil@linux:~$ Files in the current directory:
NoorainWakil@linux:~$ file_operations.sh test.txt

Practical 8: Write a shell script that Count Lines in Each File in a Directory.

STEP 1: Create a file named - nano count_lines.sh


STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
STEP 3: To execute the file type - chmod +x count_lines.sh STEP
4: For output type - ./count_lines.sh Script:

#!/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
#!/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:

NoorainWakil@linux:~$ Files in the current directory with line count:


NoorainWakil@linux:~$ file_operations.sh: 20 lines
NoorainWakil@linux:~$ test.txt: 5 lines
Practical 9: Write a shell script that find and Replace Text in Files.
STEP 1: Create a file named - nano find_replace.sh
STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
STEP 3: To execute the file type - chmod +x find_replace.sh STEP
4: For output type - ./find_replace.sh Script:
#!/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_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:

NoorainWakil@linux:~$ Enter the file name:


NoorainWakil@linux:~$ test.txt
NoorainWakil@linux:~$ Enter the text to find:
NoorainWakil@linux:~$ oldtext
NoorainWakil@linux:~$ Enter the text to replace it with:
NoorainWakil@linux:~$ newtext
NoorainWakil@linux:~$ Replaced 'oldtext' with 'newtext' in test.txt.
Practical 10: Write a shell script that find Files Modified in the Last N Days.

STEP 1: Create a file named - nano modified_files.sh


STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
STEP 3: To execute the file type - chmod +x modified_files.sh
STEP 4: For output type - ./modified_files.sh Script:

#!/bin/bas echo "Enter the number of


days:" read days echo "Files modified
in the last $days:" find . -type f -mtime
-$days
#!/bin/bash

echo "Enter the number of days:" read days

echo "Files modified in the last $days days:"


find . -type f -mtime -$days

Output:

NoorainWakil@linux:~$ Enter the number of days:


NoorainWakil@linux:~$ 7
NoorainWakil@linux:~$ Files modified in the last 7 days:
NoorainWakil@linux:~$ ./test.txt
Practical 11: Write a shell script to list contents of a directory.

STEP 1: Create a file named - nano list_dir.sh


STEP 2: Write the code and then, Press CTRL + X, then Y, and hit Enter to save and exit.
STEP 3: Execute - chmod +x list_dir.sh
STEP 4: Run with - ./list_dir.sh
Script:
#!/bin/bash echo "Enter the

directory path:" read dir if [ -d

"$dir" ]; then echo "Contents of

$dir:" ls -l "$dir" else

echo

"Directory does not exist."

Fi
#!/bin/bash

echo "Enter the directory path:" read dir

if [ -d "$dir" ]; then echo


"Contents of $dir:"
ls -l "$dir" else
echo "Directory does not exist." fi

Output:

NoorainWakil@linux:~$ Enter the directory path:


NoorainWakil@linux:~$ /home/NoorainWakil/Documents
NoorainWakil@linux:~$ Contents of /home/NoorainWakil/Documents:
NoorainWakil@linux:~$ total 4
-rw-r--r-- 1 NoorainWakil NoorainWakil 100 Apr 16 file.txt

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:

#!/bin/bash echo "Enter the path to change directory:" read

path if [ -d "$path" ]; then cd "$path" echo "Now in

directory: $(pwd)" else echo "Directory not found."

fi
#!/bin/bash

echo "Enter the path to change directory:" read path

if [ -d "$path" ]; then
cd "$path"
echo "Now in directory: $(pwd)"
else echo "Directory not found." fi

Output:

NoorainWakil@linux:~$ Enter the path to change directory:


NoorainWakil@linux:~$ /etc
NoorainWakil@linux:~$ Now in directory: /etc
Practical 13: Write a shell script to navigate to the directory that contains a
specific file.

STEP 1: Create - nano goto_file_dir.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x goto_file_dir.sh STEP
4: Run - ./goto_file_dir.sh Script:

found=$(find / -type f -name "$file" 2>/dev/null | head -n 1)

if [ -n "$found" ]; then dir=$(dirname "$found")

cd "$dir" echo "Changed directory

to: $(pwd)" else echo "File not

found."

fi

#!/bin/bash echo "Enter


filename to search:" read file
found=$(find / -type f -name "$file" 2>/dev/null | head -n 1)
if [ -n "$found" ]; then dir=$(dirname "$found") cd
"$dir" echo "Changed directory to: $(pwd)" else
echo "File not found." fi

Output:
#!/bin/bash echo "Enter filename to search:" read file

NoorainWakil@linux:~$ Enter filename to search:


NoorainWakil@linux:~$ myconfig.txt
NoorainWakil@linux:~$ Changed directory to: /home/NoorainWakil/Documents/config
Practical 14: Write a shell Script to display running processes and their details.

STEP 1: Create - nano process_list.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x process_list.sh STEP
4: Run - ./process_list.sh

Script:

#!/bin/bash

echo "Running processes:"

ps aux

#!/bin/bash

echo "Running processes:" ps aux

Output:

NoorainWakil@linux:~$ Running processes:


NoorainWakil@linux:~$ USER PID %CPU %MEM VSZ RSS TTY STAT START TIME
COMMAND
NoorainWakil@linux:~$ root 1 0.0 0.1 22520 1144 ? Ss 10:00 0:01 /sbin/init
...
Practical 15: Write a shell Script to kill processes based on name or ID.

STEP 1: Create - nano kill_proc.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x kill_proc.sh
STEP 4: Run - ./kill_proc.sh Script:
#!/bin/bash echo "Enter process name or PID to

kill:" read input if [[ "$input" =~ ^[0-9]+$ ]]; then

kill "$input" echo "Killed process with PID

$input" else pkill "$input" echo "Killed

processes named $input"

fi
#!/bin/bash

echo "Enter process name or PID to kill:" read input

if [[ "$input" =~ ^[0-9]+$ ]]; then kill


"$input"
echo "Killed process with PID $input"
else pkill "$input" echo "Killed processes
named $input" fi

Output:

NoorainWakil@linux:~$ Enter process name or PID to kill:


NoorainWakil@linux:~$ firefox
NoorainWakil@linux:~$ Killed processes named firefox
Practical 16: Write a shell Script to automatically Restart a Process if it Crashes

STEP 1: Create - nano auto_restart.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x auto_restart.sh
STEP 4: Run - ./auto_restart.sh Script:
#!/bin/bash process="your_app" while true; do if ! pgrep

"$process" > /dev/null; then echo "$process not running,

starting it..."

./"$process" &

fi

sleep 5

done
#!/bin/bash

process="your_app"

while true; do if ! pgrep "$process" > /dev/null;


then echo "$process not
running, starting it..."
./"$process" & fi
sleep 5 done

Output:

NoorainWakil@linux:~$ Enter the number of days:


NoorainWakil@linux:~$ 7
NoorainWakil@linux:~$ Files modified in the last 7 days:
NoorainWakil@linux:~$ ./test.txt
Practical 17: Write a shell Script to create, modify, and delete user accounts.

STEP 1: Create - nano manage_users.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x manage_users.sh
STEP 4: Run - sudo ./manage_users.sh
Script:
#!/bin/bash echo "Choose: add | modify | delete"

read action echo

"Enter username:" read user 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

#!/bin/bash

echo "Choose: add | modify | delete" read action


echo "Enter username:" read user

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:

NoorainWakil@linux:~$ Choose: add | modify |


delete NoorainWakil@linux:~$ add
NoorainWakil@linux:~$ Enter username:
NoorainWakil@linux:~$ testuser
NoorainWakil@linux:~$ User testuser added.

Practical 18: Write a shell Script to add or remove users from groups.

STEP 1: Create - nano group_manage.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x group_manage.sh STEP
4: Run - sudo ./group_manage.sh Script:
#!/bin/bash echo "Choose: add | remove"

read action echo "Enter username:" read user

echo "Enter group:" read group 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

#!/bin/bash

echo "Choose: add | remove" read action


echo "Enter username:" read
user echo "Enter
group:" read group

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

STEP 1: Create - nano backup.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x backup.sh STEP
4: Run - ./backup.sh Script:
#!/bin/bash backup_dir="./backups" mkdir -p

"$backup_dir" filename="backup_$(date
+%Y%m%d%H%M%S).tar.gz" tar -czf

"$backup_dir/$filename" ./important_data

# Retention policy: keep last 3 backups cd

"$backup_dir"

ls -tp | grep -v '/$' | tail -n +4 | xargs -I {} rm -- {} echo "Backup


done: $filename"

#!/bin/bash backup_dir="./backups" mkdir -p


"$backup_dir" filename="backup_$(date
+%Y%m%d%H%M%S).tar.gz" tar -czf
"$backup_dir/$filename" ./important_data
# Retention policy: keep last 3 backups cd
"$backup_dir"
ls -tp | grep -v '/$' | tail -n +4 | xargs -I {} rm -- { echo "Backup done: $filename"

Output:

NoorainWakil@linux:~$ Backup done: backup_20250416103000.tar.gz

Practical 20: Write a shell script for database Backup and Restore Script.

STEP 1: Create - nano db_backup.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x db_backup.sh
STEP 4: Run - ./db_backup.sh Script:
#!/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
#!/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:

NoorainWakil@linux:~$ Choose: backup | restore


NoorainWakil@linux:~$ backup NoorainWakil@linux:~$ Enter password:
NoorainWakil@linux:~$ Backup completed.

Practical 21: Write a shell script for Network Configuration Script with Error
Handling

STEP 1: Create - nano env_var.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x env_var.sh STEP
4: Run - ./env_var.sh
Script:

#!/bin/bash export MY_VAR="HelloWorld" echo

"Environment variable set: MY_VAR=$MY_VAR" echo

"Viewing variable: $MY_VAR"

#!/bin/bash

export MY_VAR="HelloWorld" echo "Environment variable set: MY_VAR=$MY_VAR" echo


"Viewing variable: $MY_VAR"

Output:

NoorainWakil@linux:~$ Environment variable set: MY_VAR=HelloWorld


NoorainWakil@linux:~$ Viewing variable: HelloWorld

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

STEP 1: Create - nano positional.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x positional.sh STEP
4: Run - ./positional.sh arg1 arg2 arg3
Script:

#!/bin/bash echo

"Script name: $0" echo

"First arg: $1" echo

"Second arg: $2" echo

"Third arg: $3"

#!/bin/bash

echo "Script name: $0" echo "First arg: $1" echo "Second arg: $2" echo "Third arg: $3"

Output:

NoorainWakil@linux:~$ Script name: ./positional.sh


NoorainWakil@linux:~$ First arg: arg1
NoorainWakil@linux:~$ Second arg: arg2
NoorainWakil@linux:~$ Third arg: arg3

Practical 23: Write a shell Script to intercept library calls using ltrace and
capture similar information.

STEP 1: Create - nano options.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x options.sh STEP
4: Run - ./options.sh -h Script:
#!/bin/bash case
$1 in

-h|--help) echo "Usage:

./options.sh [option]"

;;

-v|--version)

echo "Version 1.0"

;;

*)

echo "Invalid option"

;;

esac

#!/bin/bash case
$1 in
-h|--help)
echo "Usage: ./options.sh [option]"
;;
-v|--version)
echo "Version 1.0"
;;
*)
echo "Invalid option"
;; esac

Output:

NoorainWakil@linux:~$ Usage: ./options.sh [option]

Practical 24: Write a shell script to monitor process forks using “ps”

STEP 1: Create - nano auto_update.sh STEP 2:


Save and exit.
STEP 3: Execute - chmod +x auto_update.sh
STEP 4: Run - sudo ./auto_update.sh Script:
#!/bin/bash sudo apt update && sudo apt upgrade -

y echo "System updated

successfully."

#!/bin/bash

sudo apt update && sudo apt upgrade -y echo "System updated successfully."

Output:

NoorainWakil@linux:~$ System updated successfully.

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

echo "Errors extracted to error_logs.txt"

#!/bin/bash

logfile="/var/log/syslog" grep -i "error" "$logfile" > error_logs.txt echo "Errors extracted to


error_logs.txt"

Output:

NoorainWakil@linux:~$ Errors extracted to error_logs.txt

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

$5 }' | sed 's/%//g') if [ "$usage" -gt "$threshold" ]; then

echo "Disk usage is above $threshold% - Current: $usage%"

else echo "Disk usage is under control: $usage%"

fi

#!/bin/bash

threshold=80

usage=$(df / | grep / | awk '{ print $5 }' | sed 's/%//g')

if [ "$usage" -gt "$threshold" ]; then


echo "Disk usage is above $threshold% - Current: $usage%" else
echo "Disk usage is under control: $usage%" fi

Output:

NoorainWakil@linux:~$ Disk usage is under control: 42%

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

+%Y%m%d).tar.gz" tar -czf "$archive" $logdir/*.log

echo

"Logs compressed to $archive"

#!/bin/bash

logdir="/var/log" archive="logs_$(date
+%Y%m%d).tar.gz"

tar -czf "$archive" $logdir/*.log echo


"Logs compressed to $archive"

Output:

NoorainWakil@linux:~$ Logs compressed to logs_20250416.tar.gz

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

echo "CPU and Memory Usage:" top -b -n1 | head -n 10

Output:

NoorainWakil@linux:~$ CPU and Memory Usage:


NoorainWakil@linux:~$ top - 10:30:01 up 1 day, 2:10, 1 user, load average: 0.10, 0.20,
0.30
...

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

apt update sudo apt install curl -y echo

"Installation complete."

#!/bin/bash

echo "Installing curl..." sudo apt update sudo apt install curl -y echo "Installation complete."

Output:

NoorainWakil@linux:~$ Installing curl...


NoorainWakil@linux:~$ Installation complete.

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

city curl "wttr.in/$city"

#!/bin/bash

echo "Enter city name:"


read city

curl "wttr.in/$city"

Output:

NoorainWakil@linux:~$ Enter city name:


NoorainWakil@linux:~$ Delhi
NoorainWakil@linux:~$ Weather info displays from wttr.in

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

of length $length..." password=$(openssl rand base64 48 | cut

-c1-$length) echo "Generated Password:

$password"

#!/bin/bash

length=12

echo "Generating a random password of length $length..." password=$(openssl rand -


base64 48 | cut -c1-$length)

echo "Generated Password: $password"

Output:

NoorainWakil@linux:~$ Generating a random password of length 12...


NoorainWakil@linux:~$ Generated Password: hG7tKsA93ZxL

You might also like