General Principles of Intellectual Property: Concepts of Intellectual Proper...
Solution manual of shell programming assignment 2
1. 1
Solution manual of Shell programming Assignment – 2
1. Write a Shell Script to find the reverse of a given number Using WC.
Shell Script Output
2. Write a Shell Script to generate Fibonacci Series.
Shell Script Output
3. Write a Shell Script to display Mathematical Table for a given number
Shell Script
2. 2
Output
4. Write a Shell Script to copy the content of a file to another file.
Shell Script
Output
3. 3
5. Write a Shell Script to count the number of Vowels, Number of Consonants and Number
of digits present in a given String.
Shell Script
Output
4. 4
6. Write a Shell Script to accept file name & convert its contents from lower to upper case.
Shell Script
Output
7. Write a shell script that will receive a name interactively from the user during execution
and prints a welcome message.(Say Hello <entered name>.Welcome to CSE Dept.,
Kalyani Govt. Engg. College !).
Shell Script
Output
8. Write a shell script that will receive a name interactively from the user during execution
and prints greetings according to the system time. (Example: say user enters “Anirban”
and if the system is less than 12.00 AM then the script will print : “Good Morning
Anirban” else if the system time is greater than 12.00 AM but less than 6.00 PM then
prints “Good After Noon Anirban” else if the system time is greater than 6.00 PM but
less than 8.00 PM then “Good Evening Anirban” else if the system time is greater than
8.00 PM but less than 12.00 PM then “Good Night Anirban”).
5. 5
Shell Script
Output
9. Write a shell script that will receive a text filename(which contents a paragraph)
interactively from the user during execution and prints the following according to the
number of words present in the file: SHORT FILE(if the number of words is less than
100), MEDIUM FILE(if the number of words is greater than 100 but less than 350) ,
LARGE FILE( words greater than 350 but less than 500) and VERY LARGE FILE(words
greater than 500).
Shell Script
6. 6
Output
10.Write a shell script that will receive a text filename as input and prints OFFICIAL FILE if
the first line of the file contains the string “KGEC” only else prints UNOFFICIAL FILE. If
the file is an OFFICIAL FILE then the program will prints number of words and characters
present in the first 10 lines, it also appends string “Accessed on: <current date-time
stamp>” at the end of the file.
Shell Script
Output
11.Write a shell script (check_exam.sh) that will use a file “exam_schedule.txt” to check
whether there is any exam today or not. If there is no exam today then it will print: NO
7. 7
EXAM TODAY. The file “exam_schedule.txt” must contain three fields as EXAM
DATE(dd/mm/yyyy), EXAM TIME and EXAM PAPER. For example:
03/02/2014 10.00AM OPTICAL_NETWORK
04/02/2014 10.00AM IMAGE_PROCESSING
……… ……….. ……………..
Shell Script
Output
12.Write a shell script that will take the basic salary (BS) as runtime input from the user and
calculates the DA (5% if BS is less than or equal to 10,000, above 10,000 it becomes
15%), HRA(5% if BS is less than or equal to 10,000, above 10,000 it becomes 7%) and
finally calculates the gross salary(gross salary=BS+DA+HRA).
8. 8
Shell Script
Output
13.Write a shell script to display the following menu:
i. Length of the string
ii. Reverse of the string
iii. Copy one string to another.
Shell Script
echo "Enter a string: "
read str
echo "i. Length of string"
echo "ii.Reverse of the string"
echo "iii. copy one string into another."
echo "Enter your choice: "
read c
case "$c" in
i)x=`echo $str | wc -c`
x=`expr $x - 1`
echo "Length is : $x";;
ii)x=`echo $str | wc -c`
x=`expr $x - 1`
while [ $x -gt 0 ]
do
c=`echo $str | cut -c $x`
s=$s$c
x=`expr $x - 1`
done
echo "The Reverse of string is : $s";;
iii)s=$str
echo "The copied string is : $s";;
esac
9. 9
Output
14.Write a shell script to check whether a given number is prime or not.
Shell Script
echo "Enter a number: "
read num
i=2
f=0
while [ $i -le `expr $num / 2` ]
do
if [ `expr $num % $i` -eq 0 ]
then
f=1
fi
i=`expr $i + 1`
done
if [ $f -eq 1 ]
then
echo "The number is composite"
else
echo "The number is Prime"
fi
Output
15.Write a shell script to check whether a given number is an Armstrong number or not.
Shell Script
echo "Enter a number: "
read c
x=$c
sum=0
r=0
n=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
n=`expr $r * $r * $r`
sum=`expr $sum + $n`
x=`expr $x / 10`
done
if [ $sum -eq $c ]
then
echo "It is an Armstrong Number."
10. 10
else
echo "It is not an Armstrong Number."
fi
Output
16.Write a shell script to find the sum of the first n numbers.
Shell Script
echo "Enter a number: "
read num
i=1
sum=0
while [ $i -le $num ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum of first $num numbers is: $sum"
Output
17.Write a shell script to find a sum of given no and to check out to see if it is even or odd.
Shell Script
echo "Enter a number: "
read n
sum=0
x=$n
r=0
while [ $x -gt 0 ]
do
r=`expr $x % 10`
sum=`expr $sum + $r`
x=`expr $x / 10`
done
if [ `expr $sum % 2` -eq 0 ]
then
echo "The sum of $n is $sum and it is even"
else
echo "The sum of $n is $sum and it is odd"
fi
11. 11
Output
18.Write a shell script to find a factorial of a given number.
Shell Script
fact=1
ie=1
echo -e "Enter a number:c"
read a
while [ $ie -le $a ]
do
fact=`expr $fact * $ie`
ie=`expr $ie + 1`
done
echo -e "Multilpication of $a number is $fact."
Output
19.Write a shell script that takes 2 numbers through K/B and finds the value of first number
raised to the power of second.
Shell Script
echo "Enter a number: "
read a
echo "Enter Power: "
read p
i=1
ans=1
while [ $i -le $p ]
do
ans=`expr $ans * $a`
i=`expr $i + 1`
done
echo "Answer of $a^$p is $ans"
Output
20.Write a shell script to read a string through keyboard and check whether it is
palindrome or not.
Shell Script
echo "Enter a string: "
12. 12
read s
len=`echo $s | wc -c`
while [ $len -gt 0 ]
do
st=`echo $s | cut -c $len`
str=$str$st
len=`expr $len - 1`
done
if [ $str = $s ]
then
echo "String Palindrome"
else
echo "string is not palindrome"
fi
Output