Code In Tech - The Ultimate World of Computer Technology

Saturday 12 May 2012

Shell script to find a max no out of three nos using else if & and operator.

SOURCE CODE:

echo “Enter the three nos.”
read a
read b
read c
if [ $a –gt $b –a $a –gt $c ]
then
     echo $a “is maximum no.”
elif [ $b –gt $a –a $b –gt -$c ]
then
     echo $b “maximum no.”
else
     echo $c “is maximum no.”

fi

Shell script to concate two strings and find the length of resulting string.

SOURCE CODE:

echo “Enter 1st string:”
read s1
echo “ Enter 2nd string”
read s2

s=$s1$s2
l =`expr length $s`
 echo “string:” $s “ length :” $l

Shell script to reverse the given no.

SOURCE CODE:

echo  “Enter the no.:”
read n
l =`expr length $n `
a=1
d=$n
while [ $a –le $l ]
do
     m=`expr $d % 10`
     d=`expr $d / 10`
     s=$s$m
     a=`expr $a + 1`
done
echo $s

Shell script to sum of digits of a given no.

SOURCE CODE:

echo “Enter the no:”
read n
l =`expr length $n `
a=1
while [ $a –le $l ]
do
     r=`expr $n | cut –c $a`
     s=`expr $s + $r`
     a=`expr $a + 1`
done
echo “Ans:“ $s

Shell script to reverse a string.

SOURCE CODE:

echo “Enter the string”
read str
l=`expr length $str`
a=1
r=$l
while [ $a –le $r ]
do
     s=`expr $str | cut –c $l`
     l=`expr $l – 1`
     a=`expr $a + 1`

Shell script to find the max. no out of given five nos.

SOURCE CODE:

echo “Enter five nos.”
i=0
b=0
t=0
while [ $i –lt 5 ]
do
     read a
     b= $t
     if teat $b –lt $a
     then

Shell script to find the length of string, if it’s greater than 10 then print string is greater than 10 else less than 10.

SOURCE CODE:

echo “Enter the string:“
read str
l =`expr length $str`
if [ $l –lt 10 ]
then
     echo “String is less than 10”
fi
if [ $l –eq 10 ]
then
     echo “String is equal to 10”
fi

Shell script to print the fibonaci series up to given no.

SOURCE CODE:

echo “Enter the no.:“
read n
i=0
j=1
k=1
echo $i
while [ $j –le -$n ]
do
     echo $k
     k=`expr $j + $i`

Shell script to check whether the given no. is Armstrong or not

SOURCE CODE:

echo “ Enter the no.”
read n
l =`expr length $n`
i=1
d=1
while [ $i –le $l ]
do
     r=`expr $n | cut –c $i`
     i=`expr $i + 1`
     m=`expr $r \* $r \* $r`

Shell script to check whether the given no. is perfect or not.

SOURCE CODE:

echo  “Enter the no.:”
read n
i=1
k=0
while [ $i –lt $n ]
do
     j =`expr $n % $i`
     if [ $j = 0 ]
     then
           k =`expr $k + $i`

Shell script to print the series of a prime no. up to given no.

SOURCE CODE:


echo  “Enter the no:“
read n
i=1
echo $i
while [ $i -le $n ]
do
     k=2
     while [ $k – lt $i ]
     do
           j =`expr $i % $k`

Shell script to find the factorial of a given no.

SOURCECODE:

echo “Enter the no:“
read n
i=1
a=1
d=0
if [ $n –eq $d ]
then
     echo $a
else
while [ $i –le  $n ]
do

Shell script to check whether the given string is palindrome or not.

SOURCE CODE:

echo  “Enter the string:“
read str
l =`expr length $str`
a=l
while [ $a –lt -$l ]
do
     s1=`echo $str | cut –c $a`
     s2=`echo $str | cut –c $l`
     if [ $s1 !=  $s2 ]
     then

Shell script to check whether the given no. is prime or not

SOURCE CODE:


echo “Enter the no.”
read n
i=2
while [ $i –lt $n ]
do
e=`expr $n % $i`
if test  $e –eq 0
then
     echo “Given no. is not PRIME”
     break;

Shell script to find whether the given no. is odd or even


SOURCE CODE:

echo “Enter the no.:”
read a
e= `expr $a % 2`
if test $e –eq 0
then
echo “no. is EVEN”
else
echo “no. is ODD”
fi

OUTPUT:

Enter the no.: 
7
 no. is ODD

Shell script to count the number of users


SOURCE CODE:

echo “no of users:”
who | wc –l

OUTPUT:

no of users: 

BASIC LINUX COMMANDS

DATE 
$ date
Displays the current
Date.
MAN 
$ man
Displays the manual
Page of our terminal.
Ls
$ ls
List the number of files and directories.
 Ls-al
$ ls-al
List all the files in thecurrent working directory

showing permissions,
ownership, size, time and
date.