Linux Programming Lab
Linux Programming Lab
Linux Programming Lab
clear
wc -l user
OUTPUT
15 user
CURRENT DATE, USERNAME, AND CURRENT DIRECTORY
date
echo "Username:"
whoami
pwd
OUTPUT
Current Date:
Username:
pkn
Current directory:
/home/pkn
TO PRINT THE NUMBERS 5,4,3,2,1 USING WHILE LOOP
clear
read k
seq $k -1 1
OUTPUT
Input number:
1
TO CONVERT LOWERCASE TO UPPERCASE USING TR UTILITY
read fileName
if [ ! -f $fileName ]; then
exit 1
fi
OUTPUT
HELLO
TO COPY AND RENAME A FILE
clear
echo "Menu "
echo "1. Copy a File "
echo "2. Remove a file "
echo "3. Move a file"
echo "4. Quit"
echo "Enter ur Choice "
read Choice
case "$Choice" in
1) echo "Enter File name to copy "
read f1
echo "Enter FIle name "
read f2
if [ -f $f1 ]
then
cp $f1 $f2
else
echo "$f1 does not exist"
fi
;;
2) echo "Enter the File to be removed "
read r1
if [ -f $r1 ]
then
rm -i $r1
else
echo "$r1 file does not exist "
fi
;;
3)
echo "Enter File name to move \c"
read f1
echo "Enter destination \c "
read f2
if [ -f $f1 ]
then
if [ -d $f2 ]
then
mv $f1 $f2
fi
else
echo "$f1 does not exist"
fi
;;
4)
echo "Exit......."
exit;;
esac
OUTPUT
Menu
1. Copy a File
2. Remove a file
3. Move a file
4. Quit
Enter ur Choice
1
Enter File name to copy
hello
Enter FIle name
hello1
Enter ur Choice
2
Enter the File to be removed
hello1
rm: remove regular file `hello1'? yes
TO ADD 5 NUMBERS AND FIND THE AVERAGE.
clear
read n
n1=$n
s=0
while [ $n -gt 0 ]
do
read a
s=` expr $s + $a `
n=` expr $n - 1 `
done
OUTPUT
enter the Limit
do
echo "Menu"
echo "2.Exit"
read choice
case $choice in
read n
hex=`echo "ibase=10;obase=16;$n"|bc`
;;
2) exit
;;
esac
done
OUTPUT
Menu
1.Decimal to Hexadecimal
2.Exit
Enter Your Choice
1
Enter the decimal no.
10
The hexadecimal no. is A
Menu
1.Decimal to Hexadecimal
2.Exit
Enter Your Choice
2
TO FIND THE FACTORIAL OF A NUMBER.
clear
echo "Factorial"
read n
fact=1
i=1
for((i=1;i<=n;i++))
do
done
OUTPUT
Factorial
Enter number:
Factorial of 5 is 120
TO CHECK FOR PALINDROME
read n
sd=0
rev=""
on=$n
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
done
then
else
fi
OUTPUT
Number is palindrome
TO DISPLAY HELLO WORLD IN BOLD, BLINK EFFECT AND IN DIFFERENT
COLORS LIKE RED, GREEN ETC.
clear
OUTPUT
Hello World
Blink
Hello World
Hello World
Hello World
Hello World
TO DISPLAY A MULTIPLICATION TABLE.
clear
i=1
read a
read n
while [ $i -le $n ]
do
m=`expr $i \* $a`
echo "$i*$a=$m"
i=`expr $i + 1`
done
OUTPUT
multiplication table
read a
read b
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo 5.Modules
read choice
case $choice in
1)echo Addition : $(expr $a + $b);;
2)echo Suubtraction : $(expr $a - $b);;
3)echo Multiplication : $(expr $a \* $b);;
4)echo Division : $(expr $a / $b);;
5)echo Modules : $(expr $a % $b);;
*)echo This is not a choice
esac
OUTPUT
Enter the a value
45
Enter the b value
5
1.Addition
2.Subtraction
3.Multiplication
4.Division
5.Modules
Enter your choice
4
Division : 9
TO ADD TWO REAL NUMBERS
read num1
read num2
echo $sum
OUTPUT
sum: =11
TO DISPLAY THE PATTERN
do
do
echo -n "$i"
done
echo ""
done
OUTPUT
22
333
4444
55555
REVERSE THE NUMBER
clear
read n
s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s \* 10 + $r`
n=`expr $n / 10`
done
echo "$s"
OUTPUT
123
321
TO DISPLAY THE STUDENT MARK DETAILS.
clear
echo -----------------------------------
echo 'Student Mark List'
echo -----------------------------------
echo Enter the Student name
read name
echo Enter the Register number
read rno
echo Enter the Mark1
read m1
echo Enter the Mark2
read m2
echo Enter the Mark3
read m3
echo Enter the Mark4
read m4
echo Enter the Mark5
read m5
tot=$(expr $m1 + $m2 + $m3 + $m4 + $m5)
avg=$(expr $tot / 5)
echo -----------------------------------
echo 'Student Mark List'
echo -----------------------------------
echo "Student Name : $name"
echo "Register Number : $rno"
echo "Mark1 : $m1"
echo "Mark2 : $m2"
echo "Mark3 : $m3"
echo "Mark4 : $m4"
echo "Mark5 : $m5"
echo "Total : $tot"
echo "Average : $avg"
if [ $m1 -ge 35 ] && [ $m2 -ge 35 ] && [ $m3 -ge 35 ] && [ $m4 -ge 35 ] && [ $m5 -ge 35 ]
then
echo "Result : Pass"
if [ $avg -ge 90 ]
then
echo "Grade : S"
elif [ $avg -ge 80 ]
then
echo "Grade : A"
elif [ $avg -ge 70 ]
then
echo "Grade : B"
elif [ $avg -ge 60 ]
then
echo "Grade : C"
elif [ $avg -ge 50 ]
then
echo "Grade : D"
elif [ $avg -ge 35 ]
then
echo "Grade : E"
fi
else
echo "Result : Fail"
fi
echo -----------------------------------
OUTPUT
-----------------------------------
Student Mark List
-----------------------------------
Enter the Student name
xxx
Enter the Register number
09
Enter the Mark1
67
Enter the Mark2
76
Enter the Mark3
87
Enter the Mark4
78
Enter the Mark5
67
-----------------------------------
Student Mark List
-----------------------------------
Student Name : xxx
Register Number : 09
Mark1 : 67
Mark2 : 76
Mark3 : 87
Mark4 : 78
Mark5 : 67
Total : 375
Average : 75
Result : Pass
Grade :B
-----------------------------------
TO PREPARE ELECTRICITY BILL
clear
echo -----------------------------------------
echo 'Calculate Electricity Charge'
echo -----------------------------------------
echo Enter the Name
read name
echo Enter the Meter number
read mno
echo Enter the current month rea ding
read cmr
echo Enter the last month reading
read lmr
unit=$(expr $cmr - $lmr)
if [ $unit -eq 0 ]
then
charge=40
elif [ $unit -gt 0 ] && [ $unit -le 100 ]
then
charge=$(expr $unit \* 1)
elif [ $unit -gt 100 ] && [ $unit -le 300 ]
then
charge=$(expr 100 \* 1 + $unit - 100)
elif [ $unit -gt 300 ] && [ $unit -le 500 ]
then
charge=$(expr $unit \* 1 + 200 \* 2 + $unit - 300)
elif [ $unit -gt 500 ]
then
charge=$(expr 1400 + $unit - 400)
fi
echo -----------------------------------------
echo 'Electricity Billing'
echo -----------------------------------------
echo "Name : $name"
echo "Meter Number : $mno"
echo "Current Month reading : $cmr"
echo "Last Month reading : $lmr"
echo "Unit : $unit"
echo "Charge : $charge"
echo -----------------------------------------
OUTPUT
-----------------------------------------
Calculate Electricity Charge
-----------------------------------------
Enter the Name
xxx
Enter the Meter number
12345
Enter the current month rea ding
1000
Enter the last month reading
500
-----------------------------------------
Electricity Billing
-----------------------------------------
Name : xxx
Meter Number : 12345
Current Month reading : 1000
Last Month reading : 500
Unit : 500
Charge : 1100
-----------------------------------------
TO SORT THE NUMBERS IN ASCENDING ORDER
for i in ${arr[@]};do
((srtd[i+(2<<60)]++))
done
for i in ${!srtd[@]};do
for ((l=0;l<${srtd[i]};l++));do
echo $[i-(2<<60)]
done
done
OUTPUT
-12
-10
-10
-1
10
24
66
TO CREATE AND APPEND A FILE
(Eg)
vi linuxsam.txt
Welcome to linux
vi linuxsam1.txt
OUTPUT
cat linuxsam.txt>>linuxsam1.txt
welcome to linux
Create filename1:
vi linuxsam.txt
Welcome to linux
Create filename2:
vi linuxsam1.txt
OUTPUT
2 files to edit