Unix Shell Script
Unix Shell Script
Unix Shell Script
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
number"
case $choice in
1)
then
else
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
number factorial=1
do factorial=$(( factorial * i
))
done
;;
3)
number reverse=""
digit=$(( number % 10 ))
reverse="$reverse$digit"
number=$(( number / 10 ))
done
;;
4)
number original=$number
reverse=""
digit=$(( number % 10 ))
reverse="$reverse$digit"
number=$(( number / 10 ))
done
else
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)
number original=$number
sum=0
digit=$(( number % 10 ))
number=$(( number / 10 ))
done
number" else
fi
;;
6)
number is_prime=1
do if (( number % i == 0 ));
then
is_prime=0
break
fi
done
else
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)
negative" else
fi
;;
8)
))
first_digit=$number
first_digit=$(( first_digit / 10
))
done
;;
9)
number ))
;;
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
;;
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 9 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
mkdir $directory
;;
4)
directory cd $directory
;;
5)
cp $source $destination
;;
6)
new
mv $current $new
;;
7)
file rm $file
;;
8)
file vi $file
;;
9)
P a g e 10 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
P a g e 11 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
mv $source $destination
;;
10)
break
;;
*)
;;
esac
done
largest=$num1
smallest=$num1
then largest=$num2
fi
then largest=$num3
fi
smallest=$num2
P a g e 12 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
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 13 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
P a g e 14 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
else
fi
Ans =>
shell
#!/bin/bash
else
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
P a g e 15 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
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
4. Delete student based on ID
Ans =>
shell
#!/bin/bash
while true; do
detail"
P a g e 16 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
P a g e 17 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
case $choice in
1)
class
;;
2)
if (( $? == 0 )); then
class
else
fi
;;
3)
if (( $? != 0 )); then
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)
if (( $? == 0 )); then
successfully" else
fi
;;
5)
break
;;
*)
;;
esac
done
Ans =>
shell
#!/bin/bash
student.txt
P a g e 20 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
student.txt
P a g e 21 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
10. Write a shell script to input string using command line. Display total
numeric, uppercase, lowercase and special characters:
Ans =>
shell
#!/bin/bash
characters: $special"
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 22 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
P a g e 23 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
case $choice in
1)
course
;;
2)
cat student.dat
;;
3)
;;
4)
;;
5)
;;
6)
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)
;;
8)
if (( $? == 0 )); then
course
successfully" else
fi
;;
9)
if (( $? == 0 )); then
successfully" else
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
;;
esac
done
#!/bin/bash
while true; do
echo "5) Display students who have percentage between 50 and 60"
case $choice in
1)
marks1
" marks3
;;
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
;;
*)
;;
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 30 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
if (( $? == 0 )); then
in" 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
total=$(cat /etc/passwd | wc -
l) 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 31 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
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
fi
# Concatenate strings
concatenated="$string1 $string2"
length=${#string1}
# Occurrence of word in a
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
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[@]}
{numbers[j]} numbers[j]=${numbers[j+1]}
numbers[j+1]=$temp
fi
done
done
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
# Count of lines
# File in reverse
reverse=$(tac "$file")
word="example"
$words"
P a g e 35 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
P a g e 36 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
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 37 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
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 38 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
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 39 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
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
then
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 40 | 26
TY BCA-C UNIX SHELL SCRIPT Roll No :- 48
shell
#!/bin/bash
if (( $# != 2 )); then
<file2>" exit 1
fi
file1=$1
file2=$2
else
fi
P a g e 41 | 26