Q_1:
A sript to perform the following operation on two inputted numbers should be either an integer or a floating point or
both.Make proper validation.
(1)Addition.
(2)Subtaraction(3)Multipication(4) Division.
Ans:
echo
"Enter No1 : \c "
read a=0
echo
"Enter No2 : \c "
read
b=0
c=`echo
$a + $b | bc`
echo
"$a + $b = $c"
c=`echo
$a - $b | bc`
echo
"$a - $b = $c"
c=`echo
$a \* $b | bc`
echo
"$a * $b = $c"
c=`echo
$a / $b | bc`
echo
"$a / $b = $c"
Q_2:
A script that receive string and check both are same or different. Also
check the
length of both strings are greater than 0.
Ans:-
echo “enter first string:-“
read s1
echo “enter second string:-“
read s2
if
($s1 –eq $s2) ;then
echo “Both strings are same”
else
echo “both strings are
different”
fi
Q_3:
A script to enter 5 subjects marks from command line and display percentage
& appropriate class.
Ans:
clear
echo "enter marks :-"
echo
-n "501 :-"
read
p
echo
-n "502 :-"
read
a
echo
-n "503 :-"
read
u
echo
-n "504 :-"
read
i
echo
-n "505 :-"
read
o
total=`expr
$p + $a + $u + $i + $o + $pra`
pra=`expr
$total \* 100 / 700`
echo
"your total is :-$total"
echo
"your par is :-$pra"
Q_4:
Write an awk script that accepts values of two MxN matrics and display multiplication
of these matrics.
Ans:
awk
'NR==FNR {
for(i=1;i<=NF;i++)A[NR,i]=$i
next
}
{
for(i=1;i<=NF;i++){
t=0
for(j=1;j<=NF;j++)
t+=A[i,j]*$j
printf t FS
}
print ""
}'
Q_5:
Write a script that accept username from command line and check after every one- minute whether user has logged in or not. The script should
display message on user’s terminal whenever he logs in.
Ans:
if [
$# -eq 1 ]
then
who>user.lst
echo "$1 User is logeed at
"
grep -c $1 user.lst
else
echo "Pls enter User
Name"
fi
Q_6:
Write a script to simulate wc command of unix.
Ans:
echo
Enter File Name
read fn
l = 0
w =0
c = 0
exec
< $fn
while
read line
do
l = `
expr $l + 1 `
for i
in $line
do
w = `
expr $w + 1 `
done
l1 =
` expr “ $line “ : “ .* “ `
c = `
expr $c + $l1 `
done
echo Total Lines : $l
echo
Total Words : $w
echo
Total chars : $c
Q_7:
Write a script to read file contents and count number of
consonant,vowels,digits,special characters and white space characters.
Ans:
clear
file=$1
v=0
if [
$# -ne 1 ]
then
echo "$0 fileName"
exit 1
fi
if [
! -f $file ]
then
echo "$file not a file"
exit 2
fi
while
read -n 1 c
do
l=$(echo $c | tr [:upper:] [:lower:])
[[ "$l" == "a" ||
"$l" == "e" || "$l" == "i" ||
"$l" == "o" || "$l" == "u" ]]
&& (( v++ ))
done
< $file
echo
"Vowels : $v"
echo
"Characters : $(cat $file | wc -c)"
echo
"Blank lines : $(grep -c '^$'
$file)"
echo
"Lines : $(cat $file|wc -l )"
Q_8:
Write a script that receives any number of filenames as argument and then count
number of consonant,vowels,digits,special characters in each file.
Ans:
file=$1
v=0
if [
$# -ne 1 ]
then
echo "$0 fileName"
exit 1
fi
if [
! -f $file ]
then
echo "$file not a file"
exit 2
fi
while
read -n 1 c
do
l=$(echo $c | tr [:upper:] [:lower:])
[[ "$l" == "a" ||
"$l" == "e" || "$l" == "i" ||
"$l" == "o" || "$l" == "u" ]]
&& (( v++ ))
done
< $file
echo "Vowels : $v"
echo
"Characters : $(cat $file | wc -c)"
echo
"Blank lines : $(grep -c '^$'
$file)"
echo
"Lines : $(cat $file|wc -l )"
Q_9:
Write a script using awk utility to create two 3x3 matrics and multiply it.
Ans:
echo “enter values in matrix:-“
for I in 1 2 3
do
for j in 1 2 3
do
read a[$i$j]
done
done
for I in 1 2 3
do
for j in 1 2 3
done
do
echo –n
“${a[$i$j]}”
done
Q_10:
Write a script to take two numbers as argument and output their sum using
bc,nclude error checking to test whether two arguments were entered and they
are valid or not.
Ans:
read $1
read $2
bc $1 + $2
Q_11:
Write a shell script to simulate head command of unix.
Ans:
for
dir in `ls`
doif
[ -f $dir ]
then
line=`wc -l $dir | cut -d" "
-f1`
echo
$line
if [ $line -gt 10 ]
then
head -2 $dir
tail -3 $dir
else
echo "Less 10 line"
cat $dir
fi
fi
done
Q_12:
Write a script that accept amount,rate of interest and year from user.use bc to
calculate simple interest.appropriate data validation is expected.
Ans:
echo
"Enter the Principle Amount: "
read
p
echo
"Enter the rate of interest: "
read
r
echo
"Enter the number of years: "
read
n
i=`expr
$p \* $r \* $n`
i=`expr
$i / 100`
echo
"The Simple Interest is :Rs.$i"
Q_13:
Write an awk script that count frequency of one,two,three,four and more than
four characters word in a file.
Ans:
echo
"Enter File Name :\c"
read
File
cat
$File | wc -w | awk -F "|" ` ~ /./ ` | $File;
cat
$File | wc -w | awk -F "|" ` ~ /../ ` | $File;
cat
$File | wc -w | awk -F "|" ` ~ /.../ ` | $File;
cat
$File | wc -w | awk -F "|" ` ~ /..../ ` | $File;
Q_14:
Write an awk script to print the file myfile.txt.the output should be such that
there should be such that there should be such that there should be only 25
characters in each line.if a line in the exceeds 25 characters the remaining
characters the remaining characters should be printed in the next line.
Ans:
if
[ $# -eq 0 ]
then
echo "$0:Error command arguments missing!"
echo "Usage: $0 start_line uptoline filename"
echo "Where start_line is line number from which you would like to print
file"
echo "uptoline is line number upto which would like to print"
echo "For eg. $0 25 25 myfile"
echo "Here from myfile total 25 lines printed starting from line no. 25
to"
echo "line no 25."
exit 1
fi
if [ $# -eq 25 ]; then
if [ -e $3 ]; then
tail +$1 $3 | head -n$2
else
echo "$0: Error opening file $3"
exit 2
fi
else
echo "Missing
arguments!"
fi
Q_15
Write an awk script that calculates frequency of palindrome words in a text
file.
Ans:
echo
“Enter the String :\c“
read
S
r = `
echo $c | rev `
if [$s = $r]
then
echo | awk
-F “|” ` ~ /$s/ ‘|wc –l < $s
fi
Q_16:
Develop a shell script that creates 100 files with the bca001 up to bca100.
Ans:
Clear
Read
n
While [ $n -le
100 ]
Do
Cat > bca[$n]
N=`expr $n + 1`
done
Q_17:
The distance between two cities in input through the keyboard. Write a shell
script convert it in meters,feets,inches and cms.
Ans:
echo
Enter the distance in kilometers
read
dist
meters=‘expr
$dist \* 1000‘
centi=‘expr
$metert \* 100‘
inches=‘expr
$centi / 2.54 | bc‘
feet=‘expr
$inches / 12 | bc‘
echo
Distance in meters = $meters
echo
Distance in Centimeters = $centi
echo Distance
in inches = $inches
echo
Distance in feet = $feet
Q_18:
Write a shell script that output number o words that begins with capital
alphabet followed by filename for each file given as command line argument .
necessary validation is expected.
echo
“Enter File Name: \c”
read
File
ls [a-zA-Z]
*.* <$File ;
Q_19:
Write a shell script that output filename followed by generated permissions of
only those files having same permissions for both owner as well as group
Ans:
echo
“Enter File Name :\c”
read
File
echo
“Enter File1 Name :\c”
read
File1
chmod u+rwx ,g+rwx ,o-rwx <$File | cmp $File1
chmod u+rwx ,g+rwx ,o-rwx <$File1 |cmp $File
Q_20:
Write a shell script that merges two files alternatively in reverse(i.e. last
lines of two files,second last line of two line so on)
Ans:
echo
Enter first filename
read
first
echo
Enter second filename
read
second
cat
$first > third
cat
$second >> third
echo
After concatination of contents of entered two files
echo
----------------------------------------------------
cat
third | more
echo
----------------------------------------------------
Q_21:
Write a script that removes only empty files from current directory.
Ans:
echo
"Enter the File to be removed "
read r1
if [ -f $r1 ]
then
rm -i $r1
else
echo "$r1 file does not
exist "
fi
Q_22:
Write a user defined function using awk ultility to simulate left() of basic.
Ans:-
echo
“Enter File Name:\c”
read
File
awk -F “ |
“ ‘ {print $File } ’ < $File | left
()
Q_23:
Write a shell script that accept filenames and a number N from command line and
display last line-n-lines of each file.
Ans:
echo
“Enter File Name:\c”
read
File
tail
-1 $File
Q_24:
Write an awk script to display file contents in reverse(i.e. last line should
be display first-first line should be display last)
Ans:
echo
“Enter File Name:\c”
read
File
cat $File | awk `{print Strrev <$0> } | $p
Q_25:
Consider the stud.dat data file having fields stud_no,name,city and state. Each
file seprated by |. Write a menu driven script to perform following task.:
(1)Append
record to a data file.
(2)Delete
a record from a file.
(3)
Display record city wise.
Ans:1
echo “Enter File Name :\c”
read
File
cat >> $File
Ans:2
echo “Enter File Name :\c”
read
File
grep
“^[0-4]” < $File
Ans:3
echo “Enter File Name :\c”
read
File
Sed
–n `/ city / ` < $File
No comments:
Post a Comment