linuxPrg
linuxPrg
Aim:
To write a LINUX program for calculating the age of a person.
Program:
clear
echo ‘Enter name”
read name
echo ‘Enter date of birth like yyyy-mm-dd”
read dob
d1=$(date -d “$TODAY” ‘+%s’)
d2=$(date -d “$dob” ‘+%s’)
age=$(( ( d1 – d2 )/(60*60*24*364) ))
echo “$name, you are $age years old.”
1
Output:
Enter name
Xyz
Enter date of birth like yyyy-mm-dd
2000-03-03
1604860200
952021800
Xyz, you are 20 years old.
Result:
Thus, the above program has been executed and the result is verified successfully.
2
CONVERSION FROM LOWERCASE TO UPPERCASE
Aim:
To write a LINUX program to convert the content of a file from lowercase to
uppercase.
Program:
3
Output:
Enter filename
Sname
Before changing
vimal
sankar
gopi
After changing
VIMAL
SANKAR
GOPI
Result:
Thus, the above program has been executed and the result is verified successfully.
4
CHECKING THE GIVEN FILE FOR DIRECTORY
Aim:
To write a LINUX program for checking the given file for Directory or not.
Program:
clear
echo “Enter file name “
read fn
if [ -d $fn ]
then
echo “$fn is a directory. “
else
echo “$fn is not a directory. “
fi
5
Output:
Result:
Thus, the above program has been executed and the result is verified successfully.
6
CHECKING FILE ATTRIBUTES
Aim:
To write a LINUX program for checking file attributes.
Program:
clear
echo “Enter file name “
read f1n
echo “1. File existence”
echo “2. Readable File”
echo “3. Writeable File”
echo “Enter your choice 1-3.”
read ch
case $ch in
1)
if [ -f $fn ]
then
echo “$fn - File exists.”
else
echo “$fn - File does not exist.”
fi
;;
2)
if [ -r $fn ]
then
echo “$fn is a readable file.”
7
else
echo “$fn is not a readable file.
fi
;;
3)
if [ -w $fn ]
then
echo “$fn is a writeable file.”
else
echo “$fn is not a writeable file.”
fi
;;
*)
echo “Choose 1-3”
echo “Thank you!”
esac
8
Output:
Result:
Thus, the above program has been executed and the result is verified successfully.
9
STRING FUNCTIONS
Aim:
Program:
clear
echo "String Function"
echo "-------------------"
str="Welcome"
len=${#str}
echo "Given String : $str"
echo "Length of String : $len"
echo “Reverse string:”
echo $str | rev
echo "Substring from starting position"
i=1
while [ $i -le $len ]
do
echo `expr substr $str 1 $i`
i=`expr $i + 1`
done
10
Output
String Function
-------------------
Given String : Welcome
Length of string : 7
Reverse String
emocleW
Substring from starting position
W
We
Wel
Welc
Welco
Welcom
Welcome
Result:
Thus, the above program has been executed and the result is verified
successfully.
11
SORTING NAMES
Aim:
Program:
clear
echo “Enter the file name containing names “
read fn
echo “Given names”
cat $fn
sort -g $fn > name_asce
sort -g -r $fn > name_desc
echo “Ascending order”
cat name_asce
echo “Descending order”
cat name_desc
12
Output
Enter the file name containing names
Sname
Given names
Bpb
Karthi
Xavier
hari
sankar
Ascending Order
Bpb
hari
Karthi
sankar
Xavier
Descending Order
Xavier
sankar
Karthi
hari
Bpb
Result:
Thus, the above program has been executed and the result is verified successfully.
13
SEARCH PATTERN USING GREP COMMAND
Aim:
To write a LINUX Shell script for searching the given word in the given file.
Program:
clear
echo “Search pattern using grep command”
echo “-----------------------------------------------------"
echo “1. Search a word (exact match) in a file”
echo “2. Ignore case for matching word”
echo “3. Display the count of matches”
echo “4. Display all lines that contain vowel”
echo “5. Display all lines that does not contain vowel”
echo “Enter choice 1-5”
read ch
echo “Enter file name”
read fn
case $ch in
1)
echo “Enter the word to be searched “
read word
if grep -x $word $filename
then
echo “$word presents in the $fn.”
else
echo “$word presents in the $fn.”
fi
;;
2)
echo “Enter the word to be searched “
read word
if grep -i $word $filename
14
then
echo “$word presents in the $fn.”
else
echo “$word presents in the $fn.”
fi
;;
3)
echo “Enter the word to be searched “
read word
if grep -c $word $filename
then
echo “$word presents in the $fn.”
else
echo “$word presents in the $fn.”
fi
;;
4)
grep “[aeiou]” $fn
;;
5)
grep “[^aeiou]” $fn
;;
*)
echo “Choose 1-5. Thank you”
esac
15
Output:
Search pattern using grep command
-------------------------------------------
Enter filename
sname
1. Search a word (exact match) in a file
2. Ignore case for matching word
3. Display the count of matches
4. Display all lines that contain vowel
5. Display all lines that does not contain vowel
Enter choice 1-5
3
Enter the word to be searched
Gopi
1
Gopi presents in the sname.
Result:
Thus, the above program has been executed and the result is verified successfully.
16
STUDENT MARK LIST
Aim:
Program:
clear
echo “Enter name of the student”
read sname
echo “Enter register no”
read rno
echo “Enter mark 1”
read m1
echo “Enter mark 2”
read m2
echo “Enter mark 3”
read m3
tot=`expr $m1 + $m2 + $m3`
avg=`expr $tot / 3`
if [ $m1 -lt 40 ] || [ $m2 -lt 40 ] || [ $m3 -lt 40 ]
then
result=”RA”
grade=”U”
else
result=’p”
if [ $avg -ge 90 ]
then
grade=”O”
elif [ $avg -ge 80 ]
then
grade=”A+”
17
elif [ $avg -ge 70 ]
then
grade=”A”
elif [ $avg -ge 60 ]
then
grade=”B+”
elif [ $avg -ge 50 ]
then
grade=”B”
elif [ $avg -ge 40 ]
then
grade=”C”
fi
fi
echo “_______________________________”
echo “ Mark List “
echo “_______________________________”
echo “Register No. : $rno”
echo “Name of the Student : $sname”
echo “Java Programming : $m1”
echo “Java Programming Lab : $m2”
echo “NME : $m3”
echo “_______________________________”
echo “Total : $tot \t Result : $result”
echo “_______________________________”
echo “Grade : $grade”
echo “_______________________________”
18
Output:
Enter name of the student
Vijay
Enter register no
101
Enter mark 1
45
Enter mark 2
78
Enter mark 3
89
_______________________________
Mark List
_______________________________
Register No. : 101
Name of the Student : Vijay
Java Programming : 45
Java Programming Lab : 78
NME : 89
_______________________________
Total : 212 Result : P
_______________________________
Grade : A
Result:
Thus, the above program has been executed and the result is verified successfully.
19
ELECTRICITY BILL CALCULATION
Aim:
Program:
clear
echo “Enter the previous month reading “
read pr
echo “Enter the current month reading “
read cr
unit=$(expr $cr - $pr)
if [ $unit -le 100 ]
then
amt=`expr $unit \* 1`
elif [ $unit -gt 100 ] && [ $unit -le 300 ]
then
x=`expr $unit - 100`
amt=`expr $x \* 2 + 100`
elif [ $unit -gt 300 ] && [ $unit -le 500 ]
then
x=`expr $unit - 300`
amt=`expr $x \* 3 + 500`
elif [ $unit -gt 500 ]
then
20
x=`expr $unit – 500`
amt=`expr $x \* 4 + 1100`
fi
echo “------------------------------------------ ”
echo “ Electricity Bill :”
echo “------------------------------------------ ”
echo "Power Consuming: $unit"
echo "Charge : $amt"
echo “------------------------------------------ ”
21
Output:
Result:
Thus, the above program has been executed and the result is verified successfully.
22