1 | Write a shell script to generate marksheet of a student. Take 3 subjects, calculate and display total marks, percentage and Class obtained by the student |
Ans.
echo "Enter the three subject marks for the student"
read s1 s2 s3
sum1=`expr $s1 + $s2 + $s3`
echo "Sum of 3 subjects are: " $sum1
per=`expr $sum1 / 3`
echo "Percentage: " $per
if [ $per -ge 75 ]
then
echo "You get Distinction"
elif [ $per -ge 60 ]
then
echo "You get First class"
elif [ $per -ge 33 ]
then
echo "You get Second class"
else
echo "You get Fail"
fi
2 | Write a shell script to display multiplication table of given number. |
|
Ans.
echo "Enter the Number to display multiplication table"
read a1
temp=1
while [ $temp -le 10 ]
do
multiply=`expr $temp \* $a1`
echo "$a1 * $temp = $multiply"
temp=`expr $temp + 1`
done
3 | Write a shell script to find factorial of given number n. |
|
Ans.
fact=1
echo "Enter number to find factorial : "
read n
a=$n
#if enter value less than 0
if [ $n -le 0 ]
then
echo "invalid number"
exit
fi
#factorial logic
while [ $n -ge 1 ]
do
fact=`expr $fact \* $n`
n=`expr $n - 1`
done
echo "Factorial for $a is $fact"
4 | Write a shell script which will accept a number b and display first n prime numbers as output. |
|
Ans.
clear
echo "Enter the number upto which you want prime numbers: "
read n
for ((i=1;i<=n;i++))
do
flag=0
for ((j=2;j<i;j++))
do
if [ `expr $i % $j` -eq 0 ]
then
flag=1
fi
done
if [ $flag -eq 0 ]
then
echo $i
fi
done
5 | Write a shell script which will generate first n Fibonacci numbers like: 1, 1, 2, 3, 5, 13, |
|
Ans.
echo "How many number of terms to be generated ?"
read n
x=0
y=1
i=2
echo "Fibonacci Series up to $n terms :"
echo "$x"
echo "$y"
while [ $i -lt $n ]
do
i=`expr $i + 1 `
z=`expr $x + $y `
echo "$z"
x=$y
y=$z
done
Ans.
echo "==============Select Anyone Option============="
echo "1)Display Calener of current month"
echo "2)Display Today's Date and Time"
echo "3)Display Username who are currently logged in"
echo "4)Display your name on given x,y position"
echo "5)Display your terminal Number"
echo "6)Exit"
echo "Enter your choice:"
read ch
case $ch in
1)cal;;
2)date;;
3)who;;
4)row=$(tput lines)
col=$(tput cols)
echo "Terminal Window has Rows=$row Cols=$col"
echo "Enter desired X,Y position"
echo "X position="
read x
echo "Y position="
read y
echo "Enter the name"
read name
tput cup $x $y
echo "$name";;
5)tty;;
6)echo "Exit";;
*)echo "Enter valid choice";;
esac
7 | Write a shell script to read n numbers as command arguments and sort them in descending order. |
|
Ans.
#Setting up commmand line inputs with positional parameters
set $1 $2 $3 $4 $5
#Array Declaration and Initialization
declare -a nos=($1 $2 $3 $4 $5)
#printing the number before sorting
#Total Array Size is determined by ${#arrayname[@]}
echo "Numbers in an array are:"
for (( i = 0; i < ${#nos[@]}; i++ ))
do
#Printing values which is stored on jth index in an array
#Syntax: ${arrayname[index_number]}
echo -e "${nos[$i]} \c"
done
# Now do the Sorting of numbers
for (( i = 0; i < ${#nos[@]} ; i++ ))
do
for (( j = $i; j < ${#nos[@]}; j++ ))
do
if [ ${nos[$i]} -lt ${nos[$j]} ];
then
t=${nos[$i]}
nos[$i]=${nos[$j]}
nos[$j]=$t
fi
done
done
# Printing the sorted number in descending order
echo -e "\nSorted Numbers "
for (( i=0; i < ${#nos[@]}; i++ ))
do
echo -e "${nos[$i]} \c"
done
8 | Write a shell scriptto display all executable files, directories and zero sized files from current directory. |
|
Ans.
echo "Display List of all the Executable Files available in your Present Working Directoy"
find -executable -type f
echo "Display List of all the Directories available in your Present Working Directory"
ls -d */
echo "Display all Zero sized files available in your Present Working Directory"
ls -s | grep " 0 "
9 | Write a shell script to check entered string is palindrome or not. |
|
Ans.
clear
echo "Enter a string to be entered:"
read str
echo
len=`echo $str | wc -c`
len=`expr $len - 1`
i=1
j=`expr $len / 2`
while test $i -le $j
do
k=`echo $str | cut -c $i`
l=`echo $str | cut -c $len`
if test $k != $l
then
echo "String is not palindrome"
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is palindrome"
10 | Write a shell script to validate the entered date. (eg. Date format is : dd-mm-yyyy). |
|
Ans.
echo "Enter Valid Date"
read dt
echo "You have entered $dt"
date -d $dt
#CORRECT FORMAT
#MM/DD/YYYY OR DD-MONTH_NAME-YYYY
#'$?' checks the previous condition. 0 means valid, 1 means invalid
if [ $? -eq 0 ]
then
echo "Entered Date is valid"
else
echo "Entered Data is Invalid"
fi
11 | Write an awk program using function, which convert each word in a given text into capital. |
Ans.
#Introduction to awk Programing Utlity
echo "Enter the string"
a=$(awk 'BEGIN{
$getline str;
print toupper(str);
}')
echo $a
12 | Write a program forprocess creation using C. (Use of gcc compiler). |
Ans.
#include<stdio.h>
main(){
int a;
a=fork();
if(a == 0)
{
printf("\n This is a child process !!");
}
else
{
printf("\n This is a Parent Process");
}
}
No comments:
Post a Comment