Monday, 27 May 2013

Shell Script to print day

echo "enter the your choice for day (in number):-"
read ch
 case $ch in
 1) echo "Sunday"
  ;;
2) echo "monday"
  ;;
3) echo "tuesday"
  ;;
4) echo "wednesday"
  ;;
5) echo "thirsday"
  ;;
6) echo "friday"
  ;;
7) echo "saturday"
  ;;
*)
echo "wrong choice"
;;
 esac

Shell Script to find prime number

echo "enter the number"
read a
i=2
flag=1
a1=`expr $a - 1`
while test $i -le $a1
do
rem=`expr $a % $i`
if test $rem -eq 0
then
flag=0
break
else
i=`expr $i + 1`
fi
done
if test $flag -eq 0
then
echo "$a is Composite"
else
echo "$a is Prime"
fi

Shell Script to find armstrong number

echo -n "Enter number : "
read n
num=$n
sd=0
nnum=0
while [ $n -gt 0 ]
  do
    sd=$(( $n % 10 ))
    n=$(( $n / 10 ))
    pow=$(($sd * $sd * $sd))
    nnum=$(( $nnum +$pow ))
done
if test $num -eq $nnum
then
    echo "$num is a Armstrong Number"
else
    echo "$num is not an Armstrong Number"
fi

Shell Script to find time of day

time=$(date +%T)
hh=`echo $time | cut -b 1-2`
mm=`echo $time | cut -b 4-5`
if [ $hh -ge 5 ] && [ $hh -le 12 ]
then
    echo "Good Morning"
else
    echo "Good Afternoon"
fi

Shell Script to find number of files and directories

f=0
d=0
for i in `ls -l|tr -s " "|cut -c 1`
do
if [ $i = "-" ]
then
f=`expr $f + 1`
elif [ $i = "d" ]
then
d=`expr $d + 1`
fi
done
echo no of ordinary files are $f
echo no of directories are $d
               

Shell Script to show ps command

j=4
while [ $j -gt 0 ]
do
ps
sleep 10
j=`expr $j - 1`
done

Shell Script to display calender,login details,directory

echo "1.Calender"
echo "2.login details"
echo "3.Current Working Directory"
echo -n "Enter your choice:- "
read ch
case $ch in
1)
cal
;;
2)
who am i
;;
3)
pwd
;;
*)
echo "Invalid Option"
;;
esac

Shell Script to extract character from string

echo -n "Enter the String:- "
read n1
echo $n1 | rev
LEN=`echo $n1 | wc -c`
LEN=`expr $LEN - 1`
echo $LEN
echo "Enter starting:- "
read st
echo "Enter how many character to be extracted:- "
read chrn
end=$(($st + $chrn -1))
nwstr=`echo $n1 | cut -b $st-$end`
echo $nwstr

Shell Script to concatenate two files

echo -n "Enter File 1:- "
read f1
echo -n "Enter File 2:- "
read f2
echo -n "Enter Output file name:- "
read f3
cat $f1 $f2>$f3
echo "$f1 and $f2 are successfully merged into $f3"
echo "Content of $f3 are"
cat $f3

Shell Script to find palindrome

len=0
i=1

echo -n "Enter a String: "
read str
len=`echo $str | wc -c`
len=`expr $len - 1`
halfLen=`expr $len / 2`

while [ $i -le $halfLen ]
do
c1=`echo $str|cut -c$i`
c2=`echo $str|cut -c$len`
if [ $c1 != $c2 ] ; then
echo "string is not palindrome"
exit
fi
i=`expr $i + 1`
len=`expr $len - 1`
done
echo "String is Palindrome"

Shell Script to count days in month

echo -n "Enter Month in numeric form:- "
read mnt
case $mnt in
1|3|5|6|8|10|12)
echo "In $mnt Month total 31 days are present"
;;
2)
if test `expr $mnt % 4` -eq 0
then
    echo "In $mnt Month total 29 days are present"
else
    echo "In $mnt Month total 28 days are present"
fi
;;
4|7|9|11)
    echo "In $mnt Month total 30 days are present"
;;
*)
    echo "Invalid Month"
;;
esac

Shell Script to find Vowels and Consonants

echo "Enter String"
read s
c=`echo $s|cat >> tmp`
count=0
cons=0
a=1
wc=`cat tmp|wc -m`
while test $a -lt $wc
do
r=`cut -c $a tmp`
case $r in
a)
count=`expr $count + 1`
;;
e)
count=`expr $count + 1`
;;
i)
count=`expr $count + 1`
;;
o)
count=`expr $count + 1`
;;
u)
count=`expr $count + 1`
;;
*)
cons=`expr $cons + 1`
esac
a=`expr $a + 1`
done
echo "$count Vowels"
echo "$cons consonants "
x=`rm tmp`

Shell Script for comparing files

read f1
echo -n "Enter File 2 name :- "
read f2
echo "1.cmd"
echo "2.diff"
echo "3.sdiff"
echo -n "Enter your choice:- "
read ch
case $ch in
1)
echo "===========Comparision using cmp command=========="
cmp $f1 $f2
;;
2)
echo "===========Comparision using diff command=========="
diff $f1 $f2
;;
3)
echo "===========Comparision using sdiff command=========="
exec sdiff $f1 $f2
;;
*)
echo "Invalid Option"
esac

Shell Script to find factorial of a number

n=0
ip=0
fact=1

echo -n "Enter number to find factorial : "
read n

ip=$n

while [ $n -ge  1 ]
do
  fact=`expr $fact \* $n`
  n=`expr $n - 1`
done

echo "Factorial for $ip is $fact"

Shell Script for Fibbonacci Series

echo -n "Enter  Length of Fibbonacci Series:- "
read len
n1=1
n2=1
echo "$n1"
echo "$n2"
count=2
while test $count -le $len
do
n3=`expr $n1 + $n2`
n1=$n2
n2=$n3
echo "$n3"
count=`expr $count + 1`
done

Shell Script to find leap year

echo "Enter year"
read y
if test `expr $y % 4` -eq 0
then
echo "Year is Leap"
else
echo "Year is not Leap"
fi

Shell Script to find table of a number

echo -n "Enter the Number:- "
read n1
cnt=1
while [ $cnt -le 10 ]
do
echo -n " $n1 X $cnt = "
tmp=$(( $n1 * $cnt ))
echo $tmp
cnt=$(($cnt + 1))
done

Shell Script to find area of circle,rectangle,square and pentagon

echo "1.Area of circle"
echo "2.Area of Rectangle"
echo "3.Area of Square"
echo "4.Area of Pentagon"
echo "enter the choice of operation:-"
read c
case $c in
1)
echo -n "Enter radius of circle:- "
read r
echo -n "Area of circle is :- $carea"
carea= echo "3.14 * $r * $r" | bc
;;
2)
echo -n "Enter Length of Rectangle:- "
read l
echo -n "Enter Breath of Rectangle:- "
read b
rarea=`expr $l \* $b`
echo "Area of Rectagle is:- $rarea"
;;
3)
echo -n "Enter Length of Square:- "
read l
sarea=`expr $l \* $l`
echo "Area of Square is:- $sarea"
;;
4)
echo -n "Enter apothem length of Rectangle:- "
read a
echo -n "Enter side of the Pentagon:- "
read s
echo -n "Area of Pentagon:- "
parea= echo "2.5 * $s * $a" | bc
;;
*)
echo "wrong input"
;;
esac

Shell Script for Calculator

n=$#
echo "1.addition"
echo "2.subtraction"
echo "3.multiplication"
echo "4.division"
echo "5.Mod"
echo "enter the choice of operation:-"
read c
echo "Enter two number for Arithmatic Operation"
read a
read b
case $c in
1)
add=`expr $a + $b`
echo "addition of number is:- $add"
;;
2)
sub=`expr $a - $b`
echo "subtraction of number is:- $sub"
;;
3)
mult=`expr $a \* $b`
echo "multiplication of number is:- $mult"
;;
4)
div=`expr $a / $b`
echo "division of number is:- $div"
;;
5)
mod=`expr $a % $b`
echo "Mode is $mod"
*)
echo "wrong input"
;;
esac

Shell Script to find even and odd numbers

echo -n "Enter Number:- "
read num
n=$(($num%2))
if test $n -eq 0
then
echo "$num is Even Number"
else
echo "$num is Odd Number"
fi

Shell Script to find average of numbers

echo "Enter Number 1:-"
read n1
echo "Enter Number 2:-"
read n2
echo "Enter Number 3:-"
read n3
avg=$((($n1+$n2+$n3)/3))
echo "Average of Number is "$avg

Shell Script to find power of a number

echo -n "Enter number : "
read n
echo -n "Enter Power of Number:- "
read p
tmp=$p
power=1
while [ $p -gt 0 ]
  do
    power=$(echo "$power * $n" |bc)
    p=$(echo "$p - 1" |bc)
done
echo "$n power $tmp is $power"

Shell Script to find addition,subtraction,multiplication,mod of numbers(Calculator)

echo "Enter the two numbers to be added:"
read n1
read n2
sum=$(($n1+$n2))
sub=$(($n1-$n2))
mult=$(($n1*$n2))
div=$(($n1/$n2))
mod=$(($n1%$n2))
echo "Addition is :- "$sum
echo "Subtraction is :- "$sub
echo "Multiplication is :- "$mult
echo "Division is :- "$div
echo "Mod is :- "$mod

Shell Script for escape and tabs

echo "\tAfter Tabbing"
echo "\nNew line"
echo "\b"

Shell Script to find gross salary

echo "Enter basic salary:- "
read bs
hra=$(($bs/10\*4))
da=$(($bs/10\*3))
gs=$(($bs+$hra+$da))
echo "Basic Salary is "$bs
echo "HRA is "$hra
echo "DA is "$da
echo "Gross Salary is "$gs

Shell Script to find sum of all digits

echo -n "Enter number  "
read n
sd=0
sum=0
while [ $n -gt 0 ]
  do
    sd=$(( $n % 10 ))
    n=$(( $n / 10 ))
    sum=$(( $sum + $sd ))
done
echo "Sum of all digit is $sum"

Shell Script for area of triangle

echo "Enter the length and Breadth of rectangle:"
read n1
read n2
area=$(($n1*$n2))
echo "Area of rectangle is "$area
echo -n "Enter radius of the Circle:- "
read r
echo -n "Area of Circle is "
carea= echo "3.14 * $r * $r" | bc
echo $carea