Showing posts with label SHELL SCRIPTING. Show all posts
Showing posts with label SHELL SCRIPTING. Show all posts

Wednesday, October 30, 2013

TO STUDY COMMAND LINE ARGUMENTS

echo "TO STUDY COMMAND LINE ARGUMENTS"

        case $2 in
                +)      echo "Addition Operation"
                        x=$1
                        y=$3
                        echo "$x+$y=`expr $x + $y`" ;;

                -)      echo "Subtraction Operation"
                        x=$1
                        y=$3
                        echo "$x-$y=`expr $x - $y`" ;;

                \*)     echo "Multiplication Operation"
                        x=$1
                        y=$3
                        echo "$x*$y=`expr $x \* $y`" ;;

                /)      echo "Division Operation"
                        x=$1
                        y=$3
                        echo "$x/$y=`expr $x / $y`" ;;

        esac
#OUTPUT:
#chmod +x cmd.sh
#./cmd.sh 2 + 3
#Addition Operation
#2+3=5

PROGRAM TO DEMONSTRATE BACKGROUND PROCESSES

#PROGRAM TO DEMONSTRATE BACKGROUND PROCESSES

echo "background processes"
while :
do
ti=`date`
tput cup 0 65
echo -n $ti

sleep 1
clear
done

Monday, October 28, 2013

WAP to generate Fibonacci series

#WAP to generate Fibonacci series 

echo “Enter the range to be displayed” 

read n 
a=0
b=1
echo fibonacci series 
echo $a 
echo $b i=2 
while test $i -It $n 
do c = ‘expr $a + $b’ 
echo $c 
i=`expr$i+ 1’ 
a = $b 
b = $c 
done 

WAP to counts the number of lines and words present in a given file.

#WAP to counts the number of lines and words present in a given file.  

echo Enter a file name:
read n
echo Number of Lines:wc –l $n
echo Number of Words:wc –w $n
echo Number of Characters:wc –c $n

Largest of three numbers with the total and the average.

#WAP to find Largest of three numbers with the total and the average.

echo "Enter the 1st Number: "
read n1
echo "Enter the 2nd Number: "
read n2
echo "Enter the 3rd Number: "
read n3

total=`expr $n1 + $n2 + $n3`
avg=`expr $total / 3`
if [ $n1 -gt $n2 -a $n1 -gt $n3 ]
then
echo "$n1 is the Largest of three"
elif [ $n2 -gt $n1 -a $n2 -gt $n3 ]
then
echo "$n2 is the Largest of three"
elif [ $n3 -gt $n1 -a $n3 -gt $n2 ]
then
echo "$n3 is the Largest of three"
else
echo "All the three numbers are Equal"
fi
echo "Total: $total"
echo "Average: $avg"

To find a factorial of a given no.

 #To find a factorial of a given no.
fact=1
ie=1
echo -e "Enter a number:\c"
read a
while [ $ie -le $a ]
do
        fact=`expr $fact \* $ie`
        ie=`expr $ie + 1`
done
echo -e "Multilpication of $a number is $fact."
#OUTPUT
#Enter a number:5
#Multilpication of 10 number is 120.

Shell script to find the sum of the first n numbers.

#Write a shell script to find the sum of the first n numbers.


echo "Enter a number: "
read n
i=1
sum=0
while [ $i -le $n ]
do
sum=`expr $sum + $i`
i=`expr $i + 1`
done
echo "The sum of first $n numbers is: $sum"
#Enter a number:
#5
#The sum of first 5 numbers is: 15

PROGRAM TO FIND PRIME NOS IN SHELL SCRIPT

#PROGRAM TO FIND PRIME NOS IN SHELL SCRIPT
echo "Enter a number: "
read num
i=2
f=0
while [ $i -le `expr $num / 2` ]
do
if [ `expr $num % $i` -eq 0 ]
then
f=1
fi
i=`expr $i + 1`
done
if [ $f -eq 1 ]
then
echo "The number is composite"
else
echo "The number is Prime"
fi


#OUTPUT
#Enter a number:
#5
#The number is Prime

PROGRAM TO DISPLAY TABLE OF NUMBER


#PROGRAM TO DISPLAY TABLE OF NUMBER-WHILE LOOP

echo "enter no for which table is to be printed";
read x
i=1
while [ $i -lt 13 ]
do
echo " $x x $i = ` expr $x \* $i `"
i=`expr $i /* 1`;
done
---------------------------------------------------------------------------------------

# SAME PROGRAM USING FOR LOOP
echo enter number for table
read n
for (( i=1;i<=12;i++ ))
do
echo " $n x $i= `expr $n \* $i` ";
done
echo THANK U CODED BY TG

#OUTPUT:
#enter number for table
#10
#  10 x 1= 10
 #10 x 2= 20
 #10 x 3= 30
 #10 x 4= 40
 #10 x 5= 50
 #10 x 6= 60
 #10 x 7= 70
 #10 x 8= 80
 #10 x 9= 90
 #10 x 10= 100
 #10 x 11= 110
 #10 x 12= 120
#THANK U CODED BY TG#

PROGRAM TO DISPLAY SCREEN HANDLING

#PROGRAM TO DISPLAY SCREEN HANDLING

echo "enter text";
read text
for (( i=10 ;i>0;i++ ))
do
echo "enter ur choice"

echo -e "1>Bold \n 2>underline \n 3>strikethrough \n 4>text color \n 5>baground";
read choice

case $choice in
1)
echo "bold"
echo -e "\033[01m  $text"
echo -e "\033[00m  ";;
2)
echo "underline"
echo -e "\033[04m $text"
echo -e "\033[00m  ";;
3)
echo "strikethruh"
echo -e "\033[09m  $text"
echo -e "\033[00m  ";;
4)
echo "text color 35"
echo -e "\033[35m  $text"
echo -e "\033[00m  ";;
5)
echo "background color 44"
echo -e "\033[044m  $text"
echo -e "\033[00m ";;

esac
done

DISPLAY THE SUM AND REVERSE OF A NUMBER USING WHILE LOOP.

#DISPLAY THE SUM AND REVERSE OF A NUMBER USING WHILE LOOP.

echo "Enter the number: "
read x
rev=0
sum=0
d=0

while [ $x -gt 0 ]
do
 d=`expr $x % 10`
 x=`expr $x / 10`
 sum=`expr $sum + $d`
 rev=`expr $rev \* 10`
 rev=`expr $rev + $d`
done

echo "sum= " $sum
echo "reverse= " $rev

#OUTPUT:
#bash-3.00$ ./EXP7_1.sh
#Enter the number:
#123
#sum=  6
#reverse=  321

TO DEMONSTRATE NESTED IF ELSE USING SHELL SCRIPTING.

#TO DEMONSTRATE NESTED IF ELSE USING SHELL SCRIPTING.
echo "Enter percentage: "
read a

if [ $a -ge 75 ]
then
 echo "First Class Distinction"
elif [ $a -ge 60 ]
then
 echo "First Class"
elif [ $a -ge 40 ]
then
 echo "Second Class"
else
 echo "Fail"
fi


#OUTPUT:
#Enter percentage:
#75
#First Class Distinction

TO FIND IF NOS ARE EQUAL OR NOT USING SHELL SCRIPT

#TOPIC :TO FIND IF NOS ARE EQUAL OR NOT IN SHELL SCRIPT
echo "enter a:"
read a
echo "enter b:"
read b
if [ $a -eq $b ]
then
echo " numbers are not equal"
else
echo "nos are equal"
fi

TO PERFORM ARITHMETIC OPERATIONS USING SHELL SCRIPTING

#TO PERFORM ARITHMETIC OPERATIONS USING SHELL SCRIPTING IN LINUX

echo "Enter the number 1: "
read x
echo "Enter the number 2: "
read y

exit=0
while [ $exit -eq 0 ]
do

echo -e "\n1.Addition \n2.Subtraction \n3.Multiplication \n4.Division \n5.Mod \nEnter your choice:  "
read ch
case $ch in
 1)add=`expr $x + $y`
   echo "Addition= " $add;;
 2)sub=`expr $x - $y`
   echo "Subtraction= " $sub;;
 3)mul=`expr $x \* $y`
   echo "Multiplication= " $mul;;
 4)div=`expr $x / $y`
   echo "Division= " $div;;
 5)mod=`expr $x % $y`
   echo "Mod= " $mod;;
   esac

echo -e "\nDo you want to continue(0:'YES'/1:'NO'):  "
read exit

done