Professional Documents
Culture Documents
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
case $choice in
1)
else
fi
;;
2)
P a g e 1 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
factorial=1
factorial=$(( factorial * i ))
done
;;
3)
reverse=""
digit=$(( number % 10 ))
reverse="$reverse$digit"
number=$(( number / 10 ))
done
4)
original=$number
reverse=""
digit=$(( number % 10 ))
reverse="$reverse$digit"
number=$(( number / 10 ))
done
else
fi
P a g e 2 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
;;
5)
original=$number
sum=0
digit=$(( number % 10 ))
number=$(( number / 10 ))
done
else
fi
;;
6)
is_prime=1
is_prime=0
break
fi
done
else
fi
;;
P a g e 3 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
7)
else
fi
;;
8)
last_digit=$(( number % 10 ))
first_digit=$number
first_digit=$(( first_digit / 10 ))
done
;;
9)
;;
10)
break
;;
*)
P a g e 4 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
;;
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
case $choice in
1)
pwd
;;
2)
ls
;;
3)
P a g e 5 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
mkdir $directory
;;
4)
cd $directory
;;
5)
cp $source $destination
6)
mv $current $new
;;
7)
rm $file
;;
8)
vi $file
;;
9)
P a g e 6 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
mv $source $destination
;;
10)
break
;;
*)
;;
esac
done
largest=$num1
smallest=$num1
largest=$num2
fi
largest=$num3
fi
smallest=$num2
P a g e 7 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
fi
smallest=$num3
fi
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)
else
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
P a g e 8 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
else
fi
#!/bin/bash
else
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
P a g e 9 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
awk -F, '$4 < 40 && $5 >= 40 && $6 >= 40 { print }' student.dat
awk -F, '{ total=$3+$4+$5; percent=total/3; if (percent > max) { max=percent; name=$2 } } END { print
name, max }' student.dat
awk -F, '{ total=$3+$4+$5; percent=total/3; if (percent >= 50 && percent <= 60) { print } }' 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
Ans =>
shell
#!/bin/bash
while true; do
P a g e 10 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
case $choice in
1)
;;
2)
if (( $? == 0 )); then
else
fi
;;
3)
if (( $? != 0 )); then
fi
;;
P a g e 11 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
4)
if (( $? == 0 )); then
else
fi
;;
5)
break
;;
*)
;;
esac
done
Ans =>
shell
#!/bin/bash
P a g e 12 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
10. Write a shell script to input string using command line. Display total numeric,
uppercase, lowercase and special characters:
Ans =>
shell
#!/bin/bash
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 "5) Display all fields of student.dat having field separator |(pipe) instead of colon"
P a g e 13 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
case $choice in
1)
;;
2)
cat student.dat
;;
3)
;;
4)
;;
5)
;;
6)
;;
P a g e 14 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
7)
;;
8)
if (( $? == 0 )); then
else
echo "Record not found"
fi
;;
9)
if (( $? == 0 )); then
else
;;
10)
break
;;
*)
P a g e 15 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
;;
esac
done
#!/bin/bash
while true; do
echo "5) Display students who have percentage between 50 and 60"
case $choice in
1)
;;
2)
cat student.dat
P a g e 16 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
;;
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
;;
*)
;;
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
P a g e 17 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
if (( $? == 0 )); then
else
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
logged_in=$(who | wc -l)
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
else
file=$1
fi
line_count=0
echo "$line"
P a g e 18 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
line_count=$(( line_count + 1 ))
break
fi
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"
else
echo "Strings are not equal"
fi
# Concatenate strings
concatenated="$string1 $string2"
length=${#string1}
word="test"
P a g e 19 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
# Reverse a string
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[@]}
temp=${numbers[j]}
numbers[j]=${numbers[j+1]}
numbers[j+1]=$temp
fi
done
done
echo "${numbers[i]}"
done
P a g e 20 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
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
# Count of words
# Count of lines
# File in reverse
reverse=$(tac "$file")
word="example"
P a g e 21 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
echo "$reverse"
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
echo "$i"
if (( i % 2 != 0 )); then
sum=$(( sum + i ))
fi
done
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
P a g e 22 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
case $choice in
1)
cat /etc/passwd
;;
2)
who
;;
3)
pwd
;;
4)
break
;;
*)
;;
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
P a g e 23 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
echo "Files:"
echo "Directories:"
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
else
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
P a g e 24 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
echo "${line:0:10}"
line="${line:10}"
done
echo "$line"
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
exit 1
fi
filename=$1
N=$2
echo "$word"
fi
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 25 | 26
TY-BCA-C UNIX SHELL SCRIPT Roll No :- 29
shell
#!/bin/bash
if (( $# != 2 )); then
exit 1
fi
file1=$1
file2=$2
else
fi
P a g e 26 | 26