TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
THIRD ASSIGNMENT
1. Write a script to accept any number/by using command line argument and make menu
driven program for the following operations 1) Find Number is odd or Even 2) Find the
factorial of Number 3) Reverse the number 4) Check the number is palindrome or not 5)
Find the Number is Armstrong or not 6) Find the number is prime or not 7) Find the
number is positive, negative or zero 8) Sum of first and last digit 9) Display square and
cube 10) Exit
Ans =>
while true; do
echo "1) Find Number is odd or Even"
echo "2) Find the factorial of
Number" echo "3) Reverse the
number"
echo "4) Check the number is palindrome or not"
echo "5) Find the Number is Armstrong or not"
echo "6) Find the number is prime or not"
echo "7) Find the number is positive, negative or zero"
echo "8) Sum of first and last digit"
echo "9) Display square and
cube" echo "10) Exit"
read -p "Enter your choice: " choice
case $choice in
1)
read -p "Enter the number: "
number if (( number % 2 == 0 ));
then
echo "The number is even"
else
echo "The number is odd"
fi
P a g e 1 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
;;
2)
P a g e 2 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
read -p "Enter the number: "
number factorial=1
for (( i=1; i<=number; i++ ));
do factorial=$(( factorial * i
))
done
echo "The factorial of $number is $factorial"
;;
3)
read -p "Enter the number: "
number reverse=""
while (( number > 0 )); do
digit=$(( number % 10 ))
reverse="$reverse$digit"
number=$(( number / 10 ))
done
echo "The reverse of $1 is $reverse"
;;
4)
read -p "Enter the number: "
number original=$number
reverse=""
while (( number > 0 )); do
digit=$(( number % 10 ))
reverse="$reverse$digit"
number=$(( number / 10 ))
done
if (( original == reverse )); then
echo "The number is a palindrome"
else
echo "The number is not a palindrome"
P a g e 3 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
fi
P a g e 4 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
;;
5)
read -p "Enter the number: "
number original=$number
sum=0
while (( number > 0 )); do
digit=$(( number % 10 ))
sum=$(( sum + digit * digit * digit ))
number=$(( number / 10 ))
done
if (( original == sum )); then
echo "The number is an Armstrong
number" else
echo "The number is not an Armstrong number"
fi
;;
6)
read -p "Enter the number: "
number is_prime=1
for (( i=2; i<=number/2; i++ ));
do if (( number % i == 0 ));
then
is_prime=0
break
fi
done
if (( is_prime == 1 )); then
echo "The number is prime"
else
echo "The number is not prime"
fi
P a g e 5 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
;;
P a g e 6 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
7)
read -p "Enter the number: "
number if (( number > 0 )); then
echo "The number is positive"
elif (( number < 0 )); then
echo "The number is
negative" else
echo "The number is zero"
fi
;;
8)
read -p "Enter the number: "
number last_digit=$(( number % 10
))
first_digit=$number
while (( first_digit >= 10 )); do
first_digit=$(( first_digit / 10
))
done
sum=$(( first_digit + last_digit ))
echo "The sum of the first and last digit is $sum"
;;
9)
read -p "Enter the number: "
number square=$(( number *
number ))
cube=$(( number * number * number ))
echo "The square of $number is $square"
echo "The cube of $number is $cube"
;;
10)
P a g e 7 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
break
;;
*)
P a g e 8 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
echo "Invalid choice"
;;
esac
done
2 .Write a script to make following file and directory management operations menu
based: 1. Display current directory 2. List directory 3. Make directory 4. Change directory
5. Copy a file 6. Rename a file 7. Delete a file 8. Edit a file 9.Move file 10.exit
Ans =>
while true; do
echo "1) Display current
directory" echo "2) List directory"
echo "3) Make directory"
echo "4) Change directory"
echo "5) Copy a file"
echo "6) Rename a file"
echo "7) Delete a file"
echo "8) Edit a file"
echo "9) Move a file"
echo "10) Exit"
read -p "Enter your choice: " choice
case $choice in
1)
pwd
;;
2)
ls
;;
3)
read -p "Enter the directory name: " directory
P a g e 9 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
mkdir $directory
echo "Directory created successfully"
;;
4)
read -p "Enter the directory name: "
directory cd $directory
echo "Directory changed successfully"
;;
5)
read -p "Enter the source file name: " source
read -p "Enter the destination file name: " destination
cp $source $destination
echo "File copied successfully"
;;
6)
read -p "Enter the current file name: "
current read -p "Enter the new file name: "
new
mv $current $new
echo "File renamed successfully"
;;
7)
read -p "Enter the file name: "
file rm $file
echo "File deleted successfully"
;;
8)
read -p "Enter the file name: "
file vi $file
;;
9)
P a g e 10 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
read -p "Enter the source file name: " source
P a g e 11 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
read -p "Enter the destination directory: " destination
mv $source $destination
echo "File moved successfully"
;;
10)
break
;;
*)
echo "Invalid choice"
;;
esac
done
3.Find largest and smallest of three numbers.
Ans =>
read -p "Enter the first number: " num1
read -p "Enter the second number: " num2
read -p "Enter the third number: " num3
largest=$num1
smallest=$num1
if (( num2 > largest ));
then largest=$num2
fi
if (( num3 > largest ));
then largest=$num3
fi
if (( num2 < smallest )); then
smallest=$num2
P a g e 12 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
fi
if (( num3 < smallest )); then
smallest=$num3
fi
echo "The largest number is $largest"
echo "The smallest number is $smallest"
4 .Code for Shell script which whenever gets executed displays the message
“Good Morning/Good afternoon /Good Evening “depending on the time it get
executed"
Ans =>
shell
#!/bin/bash
hour=$(date +%H)
if (( hour >= 0 && hour < 12 )); then
echo "Good morning"
elif (( hour >= 12 && hour < 18 )); then
echo "Good afternoon"
else
echo "Good evening"
fi
5 .Write a shell script to validate the name of a person accepted through the keyboard
so that it does not exceed 10 characters of length.
Ans =>
shell
#!/bin/bash
read -p "Enter your name: " name
if (( ${#name} > 10 )); then
P a g e 13 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
echo "Name should not exceed 10 characters"
P a g e 14 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
else
echo "Name is valid"
fi
6. Write a shell script to test the file is executable or not.
Ans =>
shell
#!/bin/bash
read -p "Enter a filename: " filename
if [[ -x $filename ]]; then
echo "$filename is executable"
else
echo "$filename is not executable"
fi
7. Create a student file with roll number, name, marks1, marks2, and marks3. Insert
five records and perform the following tasks:
1. Display students who fail in marks2 only
2. Display student who has the maximum percentage
3. Display students who have a percentage between 50 and 60
4. Display all students in ascending order
Ans =>
shell
#!/bin/bash
echo "Roll Number,Name,Marks1,Marks2,Marks3" >
student.dat echo "1,Alice,80,30,70" >> student.dat
echo "2,Bob,70,60,80" >> student.dat
echo "3,Charlie,50,40,60" >> student.dat
echo "4,David,90,80,70" >> student.dat
echo "5,Eve,60,30,40" >> student.dat
P a g e 15 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
# Display students who fail in marks2 only
awk -F, '$4 < 40 && $5 >= 40 && $6 >= 40 { print }' student.dat
# Display student who has the maximum percentage
awk -F, '{ total=$3+$4+$5; percent=total/3; if (percent > max) { max=percent; name=$2 } } END { print
name, max }' student.dat
# Display students who have a percentage between 50 and 60
awk -F, '{ total=$3+$4+$5; percent=total/3; if (percent >= 50 && percent <= 60) { print } }' student.dat
# Display all students in ascending
order sort -t, -k2 student.dat
8. Write a shell script to create a file having student(id, name, class, percent) and
perform the following tasks:
1. Insert student details
2. Update student detail
3. Search student detail based on ID
4. Delete student based on ID
Ans =>
shell
#!/bin/bash
echo "ID,Name,Class,Percent" > student.dat
while true; do
echo "1) Insert student details"
echo "2) Update student
detail"
echo "3) Search student detail based on
ID" echo "4) Delete student based on ID"
echo "5) Exit"
P a g e 16 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
read -p "Enter your choice: " choice
P a g e 17 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
case $choice in
1)
read -p "Enter the ID: " id
read -p "Enter the name: "
name read -p "Enter the class: "
class
read -p "Enter the percent: " percent
echo "$id,$name,$class,$percent" >>
student.dat echo "Record added successfully"
;;
2)
read -p "Enter the ID to update: "
id grep -q "^$id," student.dat
if (( $? == 0 )); then
sed -i "/^$id,/d" student.dat
read -p "Enter the new name: "
name read -p "Enter the new class: "
class
read -p "Enter the new percent: " percent
echo "$id,$name,$class,$percent" >>
student.dat echo "Record updated successfully"
else
echo "Record not found"
fi
;;
3)
read -p "Enter the ID to search: " id
grep "^$id," student.dat
if (( $? != 0 )); then
echo "Record not found"
P a g e 18 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
fi
;;
P a g e 19 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
4)
read -p "Enter the ID to delete: "
id grep -q "^$id," student.dat
if (( $? == 0 )); then
sed -i "/^$id,/d" student.dat
echo "Record deleted
successfully" else
echo "Record not found"
fi
;;
5)
break
;;
*)
echo "Invalid choice"
;;
esac
done
9. Write a script that reads student.txt(Rno,Name,M1,M2,M3,Class) file and displays
the mark sheet in the following format [display data sorted by semester]:
SRNO|Rollno|Name|Marks1|Marks2|Marks3|Total|Percent|Grade
Ans =>
shell
#!/bin/bash
echo "Rno,Name,M1,M2,M3,Class" >
student.txt echo "1,Alice,80,30,70,1" >>
student.txt
echo "2,Bob,70,60,80,1" >> student.txt
echo "3,Charlie,50,40,60,2" >>
student.txt echo "4,David,90,80,70,2" >>
P a g e 20 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
student.txt echo "5,Eve,60,30,40,3" >>
student.txt
P a g e 21 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
awk -F, '{ total=$3+$4+$5; percent=total/3; grade=percent>=60?"A":percent>=50?"B":"C"; print $1
"|" $2 "|" $3 "|" $4 "|" $5 "|" total "|" percent "|" grade }' student.txt | sort -t\| -k6rn
10. Write a shell script to input string using command line. Display total
numeric, uppercase, lowercase and special characters:
Ans =>
shell
#!/bin/bash
read -p "Enter a string: " string
numeric=$(echo $string | grep -o [0-9] | wc -l)
uppercase=$(echo $string | grep -o [A-Z] | wc -l)
lowercase=$(echo $string | grep -o [a-z] | wc -l)
special=$(echo $string | grep -o [^a-zA-Z0-9] | wc -l)
echo "Total numeric characters: $numeric"
echo "Total uppercase characters:
$uppercase" echo "Total lowercase
characters: $lowercase" echo "Total special
characters: $special"
11. Consider student.dat is as follows Rollno: student_name: course: joining_date [in
the form dd-mm-yyyy]. Use this file and write a menu-driven script as follows:
Ans =>
shell
#!/bin/bash
while true; do
echo "1) Add record"
echo "2) Display all records"
echo "3) Sort student information as per their joining date"
echo "4) Sort student information on their name"
echo "5) Display all fields of student.dat having field separator |(pipe) instead of colon"
P a g e 22 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
echo "6) Display name of student and their joining date"
P a g e 23 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
echo "7) Display student information course
wise" echo "8) Modify record"
echo "9) Delete record"
echo "10) Exit"
read -p "Enter your choice: " choice
case $choice in
1)
read -p "Enter the roll number: " rollno
read -p "Enter the student name: "
name read -p "Enter the course: "
course
read -p "Enter the joining date (dd-mm-yyyy): "
date echo "$rollno:$name:$course:$date" >>
student.dat echo "Record added successfully"
;;
2)
cat student.dat
;;
3)
sort -t: -k4 student.dat
;;
4)
sort -t: -k2 student.dat
;;
5)
cat student.dat | tr ':' '|'
;;
6)
cut -d: -f2,4 student.dat
P a g e 24 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
;;
P a g e 25 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
7)
cut -d: -f3 student.dat | sort | uniq -c
;;
8)
read -p "Enter the roll number to modify: " rollno
grep -q "^$rollno:" student.dat
if (( $? == 0 )); then
sed -i "/^$rollno:/d" student.dat
read -p "Enter the new student name: "
name read -p "Enter the new course: "
course
read -p "Enter the new joining date (dd-mm-yyyy): " date
echo "$rollno:$name:$course:$date" >> student.dat
echo "Record modified
successfully" else
echo "Record not found"
fi
;;
9)
read -p "Enter the roll number to delete: "
rollno grep -q "^$rollno:" student.dat
if (( $? == 0 )); then
sed -i "/^$rollno:/d" student.dat
echo "Record deleted
successfully" else
echo "Record not found"
fi
;;
10)
break
;;
P a g e 26 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
*)
P a g e 27 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
echo "Invalid choice"
;;
esac
done
12. Create a student file(rno,name,marks1,marks2,marks3). Perform the following task:
Ans =>
shell
#!/bin/bash
while true; do
echo "1) Insert record"
echo "2) Display all records"
echo "3) Display students who fail in marks2 only"
echo "4) Display student who has maximum percentage"
echo "5) Display students who have percentage between 50 and 60"
echo "6) Display all pass students in ascending order"
echo "7) Exit"
read -p "Enter your choice: " choice
case $choice in
1)
read -p "Enter the roll number: " rollno
read -p "Enter the student name: "
name read -p "Enter the marks1: "
marks1
read -p "Enter the marks2: "
marks2 read -p "Enter the marks3:
" marks3
echo "$rollno:$name:$marks1:$marks2:$marks3" >> student.dat
echo "Record added successfully"
;;
P a g e 28 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
2)
cat student.dat
P a g e 29 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
;;
3)
awk -F: '$4 < 40 && $5 >= 40 && $6 >= 40 { print }' student.dat
;;
4)
awk -F: '{ total=$3+$4+$5; percent=total/3; if (percent > max) { max=percent; name=$2 } } END
{ print name, max }' student.dat
;;
5)
awk -F: '{ total=$3+$4+$5; percent=total/3; if (percent >= 50 && percent <= 60) { print } }'
student.dat
;;
6)
awk -F: '{ total=$3+$4+$5; percent=total/3; if (percent >= 40 && $4 >= 40 && $5 >= 40) { print
} }' student.dat | sort -t: -k3
;;
7)
break
;;
*)
echo "Invalid choice"
;;
esac
done
13. Write a shell script which accepts a username and checks whether the entered user
is currently logged in or not:
Ans =>
shell
#!/bin/bash
read -p "Enter a username: " username
who | grep -q "^$username "
P a g e 30 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
if (( $? == 0 )); then
echo "$username is currently logged
in" else
echo "$username is not currently logged in"
fi
14. Code for Write a shell script that finds the total number of users and finds out
how many of them are currently logged in Unix:
Ans =>
shell
#!/bin/bash
total=$(cat /etc/passwd | wc -
l) logged_in=$(who | wc -l)
echo "Total number of users: $total"
echo "Number of users currently logged in: $logged_in"
15. Write a shell script which takes input of a filename and prints the first ten lines of
that file. The filename is to be passed as a command-line argument. If the argument is
not
passed, then any C program from the current directory is to be selected (don’t
use head command)
Ans =>
shell
#!/bin/bash
if (( $# == 0 )); then
file=$(ls *.c | head -n1)
else
file=$1
fi
line_count=0
while IFS= read -r line; do
echo "$line"
P a g e 31 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
line_count=$(( line_count + 1
)) if (( line_count == 10 )); then
break
fi
done < "$file"
16. Code for Shell script to perform operations like compare string, concatenate,
find length, occurrence of word in a string and reverse a string:
Ans =>
shell
#!/bin/bash
# Compare
strings
string1="Hello"
string2="World"
if [[ $string1 == $string2 ]]; then
echo "Strings are equal"
else
echo "Strings are not equal"
fi
# Concatenate strings
concatenated="$string1 $string2"
echo "Concatenated string: $concatenated"
# Find length of a string
length=${#string1}
echo "Length of string1: $length"
# Occurrence of word in a
string string="This is a test
P a g e 32 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
string" word="test"
P a g e 33 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
occurrences=$(grep -o -w "$word" <<< "$string" | wc -l)
echo "Occurrences of '$word' in the string: $occurrences"
# Reverse a string reverse=$
(rev <<< "$string")
echo "Reversed string: $reverse"
17. Code for Write a shell script to sort the given numbers in descending order
using Bubble sort:
Ans =>
shell
#!/bin/bash
numbers=(5 2 8 1 9 3)
n=${#numbers[@]}
for (( i=0; i<n-1; i++ )); do
for (( j=0; j<n-i-1; j++ )); do
if (( numbers[j] < numbers[j+1] )); then temp=$
{numbers[j]} numbers[j]=${numbers[j+1]}
numbers[j+1]=$temp
fi
done
done
echo "Sorted numbers in descending
order:" for (( i=0; i<n; i++ )); do
echo "${numbers[i]}"
done
P a g e 34 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
18. Write a script which reads a text file and output the following: Count of
characters, words, and lines. File in reverse. Frequency of a particular word in the file.
Lower case letter in place of upper case letter:
Ans =>
shell
#!/bin/bash
file="sample.txt"
# Count of characters
characters=$(wc -m < "$file")
# Count of words words=$
(wc -w < "$file")
# Count of lines
lines=$(wc -l < "$file")
# File in reverse
reverse=$(tac "$file")
# Frequency of a particular word
word="example"
frequency=$(grep -o -w "$word" "$file" | wc -l)
# Lower case letter in place of upper case letter
lowercase=$(tr '[:upper:]' '[:lower:]' < "$file")
echo "Count of characters:
$characters" echo "Count of words:
$words"
P a g e 35 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
echo "Count of lines: $lines"
P a g e 36 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
echo "File in reverse:"
echo "$reverse"
echo "Frequency of '$word': $frequency"
echo "Lower case letter in place of upper case letter:"
echo "$lowercase"
19. Write a shell script to print numbers 1 to 20 in reverse and calculate the sum of
odd numbers:
Ans =>
shell
#!/bin/bash
sum=0
for (( i=20; i>=1; i-- )); do
echo "$i"
if (( i % 2 != 0 )); then
sum=$(( sum + i ))
fi
done
echo "Sum of odd numbers: $sum"
20. Write the menu-driven form which has the following options: Contents of
/etc/passwd, List of users who have currently logged in, Present working directory, Exit
(Quit):
Ans =>
shell
#!/bin/bash
while true; do
echo "1) Contents of /etc/passwd"
echo "2) List of users who have currently logged in"
P a g e 37 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
echo "3) Present working
directory" echo "4) Exit (Quit)"
read -p "Enter your choice: " choice
case $choice in
1)
cat /etc/passwd
;;
2)
who
;;
3)
pwd
;;
4)
break
;;
*)
echo "Invalid choice"
;;
esac
done
21. Write a shell script to find out how many files and directories are there in the
current directory and also list file and directory names separately:
Ans =>
shell
#!/bin/bash
file_count=$(find . -maxdepth 1 -type f | wc -l)
directory_count=$(find . -maxdepth 1 -type d | wc -l)
P a g e 38 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
echo "Number of files: $file_count"
echo "Number of directories: $directory_count"
echo "Files:"
find . -maxdepth 1 -type f -exec basename {} \;
echo "Directories:"
find . -maxdepth 1 -type d -exec basename {} \;
22. Code for Write a shell script that receives any number of filenames as arguments.
The shell script should check whether such files already exist:
Ans =>
shell
#!/bin/bash
for file in "$@"; do
if [[ -e $file ]]; then
echo "$file exists"
else
echo "$file does not exist"
fi
done
23. Write a shell script to input a filename and display the content of the file in a manner
that each line has only 10 characters. If a line in a file exceeds 10 characters, the
remaining characters should be printed on the next line:
Ans =>
shell
#!/bin/bash
read -p "Enter a filename: " filename
P a g e 39 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
while IFS= read -r line; do
while (( ${#line} > 10 )); do
echo "${line:0:10}"
line="${line:10}"
done
echo "$line"
done < "$filename"
24. Write a shell script to display all words that have a length greater than N
characters from file N, and the filename must be passed from the command line
(validation required):
Ans =>
shell
#!/bin/bash
if (( $# != 2 )); then
echo "Usage: $0 <filename> <N>"
exit 1
fi
filename=$1
N=$2
while IFS= read -r word; do
if (( ${#word} > N ));
then
echo "$word"
fi
done < "$filename"
25. A shell script that accepts two filenames as arguments, checks if the permissions for
these files are identical, and if the permissions are identical, output common
permissions; otherwise, output each filename followed by its permissions:
Ans =>
P a g e 40 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
shell
#!/bin/bash
if (( $# != 2 )); then
echo "Usage: $0 <file1>
<file2>" exit 1
fi
file1=$1
file2=$2
permissions1=$(stat -c "%a" "$file1")
permissions2=$(stat -c "%a" "$file2")
if [[ $permissions1 == $permissions2 ]]; then
echo "Common permissions: $permissions1"
else
echo "$file1 permissions: $permissions1"
echo "$file2 permissions: $permissions2"
fi
P a g e 41 | 26