34. Write a shell script to print Fibonacci series.
$vi prg34
clear
echo enter the last element of series
read n
echo
a=0
b=1
echo $a
echo $b
i=1
while test $i –lt $n
do
c=‘expr $a + $b’
if test $c –gt $n
then
exit
fi
echo $c
a=$b
b=$c
i=‘expr $i + 1’
done
$sh prg34
enter value of series
5
0
1
1
2
3
5
35. Write a shell script to translate the contents of a file into UPPER CASE, where file name is entered through command line.
$vi prg35
clear
if test $# -eq 0
then
echo “No argument”
exit
fi
while test $# -gt 0
do
if test –s $1
then
if test –f $1
then
cat $1 | tr a-z A-Z >$1.up
cat $1.up
fi
else
echo $1 is not a file
fi
shift
done
echo Translation successful
$sh prg35 file.txt
WELCOME
HELLO
Translation successful
In file.txt, welcome and hello are written in small letters. After running this program, welcome and hello are converted in capital letters and saved in 1.up file
36. Write a shell script to perform following tasks-
(
)
Display the present working directory.
(
)
Clear the screen.
(
)
Display the current date.
(
)
Make a directory with its sub-directory d1.
(
)
Change the directory to the directory having sub directory d1.
(
)
Create two files (say file1 & file2) within this.
(
)
Provide appropriate security options to these files.
(
)
List the contents of directory.
$vi prg36
(a) Pwd
(b) clear
(c) date
(d) mkdir d
cd d
mkdir d1
(e) cd d1
(f) touch file1 file2
(g) chmod 644 file1 file2
(h) Is
37. The marks obtained by a student in five different subjects are input through the keyboard. The student gets a division as per the following rules. (Using else’s clause).
if percentage greater than or equal to 60 get First division
if percentage greater than or equal to 50 or less than 60 get Second division
if percentage greater than or equal to 40 or less than 50 get Third division
if percentage less than 40 Fail
$vi prg37
clear
echo enter marks of five subjects (out of 100 each)
read m1
read m2
read m3
read m4
read m5
per=‘echo \( $m1 + $m2 + $m3 + $m4 + $m5 \) /5 | bc’
echo
echo Percentage is $per
if [ $per –ge 60 ]
then
echo First division
else
if [ $per –ge 50 –a -$per –lt 60 ]
then
echo Second division
else
if [ $per –ge 40 –a $per –lt 50 ]
then
echo Third division
else
echo Fail
fi
fi
fi
Sample Run
$sh prg37
enter marks of five subjects
44
67
80
90
67
Percentage is 69
First division
$sh prg37
enter marks of five subjects
56
54
53
51
60
Percentage is 54
Second division
$sh prg37
enter marks of five subjects
46
54
41
42
46
Percentage is 45
Third division
$sh prg37
enter marks of five subjects
34
42
31
32
23
Percentage is 32
Fail
38. The marks obtained by a student in two different subjects are input through the keyboard. The student gets a division as per the following rules. (Using elif clause).
if percentage greater than or equal to 60 get First division
if percentage greater than or equal to 50 or less than 60 get Second division
if percentage greater than or equal to 40 or less than 50 get Third division
if percentage less than 40 Fail
$vi prg38
clear
echo enter marks of five subjects
read m1
read m2
read m3
read m4
read m5
per=‘echo \( $m1 + $m2 + $m3 + $m4 + $m5 \) /5 | bc’
echo
echo Percentage is $per
if [ $per –ge 60 ]
then
echo First division
elif [ $per –ge 50 –a -$per –lt 60 ]
then
echo Second division
elif [ $per –ge 40 –a $per –lt 50 ]
then
echo Third division
else
echo Fail
fi
$sh prg38
enter marks of five subjects
44
67
80
90
67
Percentage is 69
First division
$sh prg38
enter marks of five subjects
56
54
53
51
60
Percentage is 54
Second division
$sh prg38
enter marks of five subjects
46
54
41
42
46
Percentage is 45
Third division
$sh prg38