NO OF USER WHO LOGGED IN
clear
who > user
echo "the total no of user logged in"
wc -l user
OUTPUT
the total no of user logged in
15 user
CURRENT DATE, USERNAME, AND CURRENT DIRECTORY
echo "Current Date:"
date
echo "Username:"
whoami
echo "Current directory:"
pwd
OUTPUT
Current Date:
Sat Oct 21 10:20:01 IRKST 2034
Username:
pkn
Current directory:
/home/pkn
TO PRINT THE NUMBERS 5,4,3,2,1 USING WHILE LOOP
clear
echo "Input number: "
read k
seq $k -1 1
OUTPUT
Input number:
1
TO CONVERT LOWERCASE TO UPPERCASE USING TR UTILITY
echo -n "Enter File Name : "
read fileName
if [ ! -f $fileName ]; then
echo "Filename $fileName does not exists."
exit 1
fi
tr '[a-z]' '[A-Z]' < $fileName
OUTPUT
Enter File Name : hello
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
echo "enter the Limit"
read n
n1=$n
s=0
while [ $n -gt 0 ]
do
echo "enter the numbers"
read a
s=` expr $s + $a `
n=` expr $n - 1 `
done
avg=` expr $s / $n1 `
echo "the sum of a digit is:$s"
echo "the average of a digit is:$avg"
OUTPUT
enter the Limit
enter the numbers
78
enter the numbers
87
enter the numbers
90
enter the numbers
65
enter the numbers
78
the sum of a digit is:398
the average of a digit is:79
TO CONVERT A DECIMAL NUMBER TO HEXADECIMAL CONVERSION
while `test $ans='y'`
do
echo "Menu"
echo "1.Decimal to Hexadecimal"
echo "2.Exit"
echo "Enter Your Choice"
read choice
case $choice in
1) echo "Enter the decimal no."
read n
hex=`echo "ibase=10;obase=16;$n"|bc`
echo "The hexadecimal no. is $hex"
;;
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"
echo "Enter number:"
read n
fact=1
i=1
for((i=1;i<=n;i++))
do
fact=`expr $fact \* $i`
done
echo "Factorial of $n is $fact "
OUTPUT
Factorial
Enter number:
Factorial of 5 is 120
TO CHECK FOR PALINDROME
echo -n "Enter number : "
read n
sd=0
rev=""
on=$n
while [ $n -gt 0 ]
do
sd=$(( $n % 10 ))
n=$(( $n / 10 ))
rev=$( echo ${rev}${sd} )
done
if [ $on -eq $rev ];
then
echo "Number is palindrome"
else
echo "Number is NOT palindrome"
fi
OUTPUT
Enter number : 123
Number is NOT palindrome
Enter number : 121
Number is palindrome
TO DISPLAY HELLO WORLD IN BOLD, BLINK EFFECT AND IN DIFFERENT
COLORS LIKE RED, GREEN ETC.
clear
echo -e "\033[1m Hello World"
echo -e "\033[5m Blink"
echo -e "\033[0m Hello World"
echo -e "\033[31m Hello World"
echo -e "\033[32m Hello World"
echo -e "\033[33m Hello World"
echo -e "\033[34m Hello World"
echo -e "\033[35m Hello World"
echo -e "\033[36m Hello World"
echo -e -n "\033[0m "
echo -e "\033[41m Hello World"
echo -e "\033[42m Hello World"
echo -e "\033[43m Hello World"
echo -e "\033[44m Hello World"
echo -e "\033[45m Hello World"
echo -e "\033[46m Hello World"
echo -e "\033[0m Hello World"
OUTPUT
Hello World
Blink
Hello World
Hello World
Hello World
Hello World
TO DISPLAY A MULTIPLICATION TABLE.
clear
echo "multiplication table"
i=1
echo "Enter the number"
read a
echo "Enter the limit"
read n
while [ $i -le $n ]
do
m=`expr $i \* $a`
echo "$i*$a=$m"
i=`expr $i + 1`
done
OUTPUT
multiplication table
Enter the number
5
Enter the limit
5
1*5=5
2*5=10
3*5=15
4*5=20
5*5=25
TO PERFORM ARITHMETIC OPERATIONS USING CASE
clear
echo Enter the a value
read a
echo Enter the b value
read b
echo 1.Addition
echo 2.Subtraction
echo 3.Multiplication
echo 4.Division
echo 5.Modules
echo Enter your choice
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
echo "Enter two numbers"
read num1
read num2
sum =`expr $num1 + $num2`
echo $sum
OUTPUT
Enter two numbers
sum: =11
TO DISPLAY THE PATTERN
echo "Can you see the following:"
for (( i=1; i<=5; i++ ))
do
for (( j=1; j<=i; j++ ))
do
echo -n "$i"
done
echo ""
done
OUTPUT
Can you see the following:
22
333
4444
55555
REVERSE THE NUMBER
clear
echo "enter the number"
read n
s=0
while [ $n -gt 0 ]
do
r=`expr $n % 10`
s=`expr $s \* 10 + $r`
n=`expr $n / 10`
done
echo "reverse the number"
echo "$s"
OUTPUT
enter the number
123
reverse the number
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
read -a arr <<<'4 -1 2 66 -12 -10 10 2 24 -10'
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
Cat filename >> filename1
(Eg)
vi linuxsam.txt
Welcome to linux
vi linuxsam1.txt
welcome first computer science
OUTPUT
cat linuxsam.txt>>linuxsam1.txt
welcome first computer science
welcome to linux
TO COMPARE TWO FILES
Create filename1:
vi linuxsam.txt
Welcome to linux
Create filename2:
vi linuxsam1.txt
welcome first computer science
OUTPUT
vimdiff linuxsam.txt linuxsam1.txt
2 files to edit