Showing posts with label Shell Scripts. Show all posts
Showing posts with label Shell Scripts. Show all posts

Tuesday, 11 June 2013

Shell Script to accept a number n from user and display summation of the following series. 1! + 2! + 3! + … + n!

Code:
echo -n "Enter the number : "
read x
i=1
sum=0
while test $i -le $x
do
        fact=1
        a=1
        while test $a -le $i
        do
                let fact=$fact*$a
                let a=$a+1
        done
        let sum=$sum+$fact
        let i=$i+1
done

let i=2
echo -n "1!"
while test $i -le $x
do
        echo -n " + $i !"
        let i=$i+1
done
echo " = $sum"

Output:
[ty2011330@localhost ~]$ sh j08.sh
Enter the number : 4
1! + 2 ! + 3 ! + 4 ! = 33

Shell Script to accept a number n from user and display the squares of all numbers from 1 to n

Code:
echo -n "Enter the number : "
read n
c=1
a=1
while test $c -le $n
do
        let a=$c*$c
        echo "$c square = $a"
        let c=c+1
done


Output:
[ty2011330@localhost ~]$ sh j09.sh
Enter the number : 5
1 square = 1
2 square = 4
3 square = 9
4 square = 16
5 square = 25

Shell Script to accept 5 subject marks of a student and display whether the student got first class, second class, third class or Student failed .

Code:
echo "Enter the marks of five subjects : "
read m1 m2 m3 m4 m5
let per=$m1+$m2+$m3+$m4+$m5
let per=per/5
echo "Percentage of students is $per %"
if [ $per -lt 35 ]
then
        echo "Student Failed."
elif [ $per -ge 35 -a $per -lt 45 ]
then
        echo "Student passed with third class."
elif [ $per -ge 45 -a $per -lt 60 ]
then
        echo "Student passed with second class."
elif [ $per -ge 60 -a $per -lt 75 ]
then
        echo "Student passed with first class."
fi
Output:
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
20 15 32 14 34
Percentage of students is 23 %
Student Failed.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
35 45 35 36 40
Percentage of students is 38 %
Student passed with third class.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
50 55 45 56 60
Percentage of students is 53 %
Student passed with second class.
[ty2011330@localhost ~]$ sh j05.sh
Enter the marks of five subjects :
60 78 89 96 47
Percentage of students is 74 %
Student passed with first class

Shell Script to accept a number from user and print the day of the week using case … in

Code:
echo -n "Enter the day number : "
read num
case $num in
        1) echo "Sunday";;
        2) echo "Monday";;
        3) echo "Tuesday";;
        4) echo "Wednesday";;
        5) echo "Thursday";;
        6) echo "Friday";;
        7) echo "Saturday";;
        *) echo "Enter any number between 1 to 7."
esac


Output:
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 1
Sunday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 3
Tuesday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 2
Monday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 4
Wednesday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 5
Thursday
[ty2011330@localhost ~]$ sh j06.sh
Enter the day number : 6
Friday

shell script to accept a number from the user and check the number entered is prime or not.

Code:
echo "Enter the number"
read num
i=2
flag=0
while [ $i -lt $num ]
do
   k=`expr $num % $i`
   if  test $k -eq 0
   then
       flag=1
   fi
   i=`expr $i + 1`
done
if test $flag -eq 0
then
   echo "Number is prime"
else
   echo "Number is not prime"
fi    


Output:
Enter the number
2
Number is prime

Enter the number
4
Number is not prime

Shell script to accept a number from the user and display the number of days in that particular month.

Code:
echo Enter month
month=(30 31 29)
read mon
case "$mon" in
        [jJ][aA][nN]*) echo "No.of days : ${month[1]} ";;
        [fF][eE][bB]*) echo enter year
                       read year
                       let rem="$year % 4"
                       if [ $rem -eq 0 ] ; then
                          echo "No. of days : ${month[2]}"
                       else
                          echo "No. of days : 28 "
                       fi;;
         [mM][aA][rR]*) echo "No. of days : ${month[1]}";;
         [aA][pP][rR]*) echo "No. of days: ${month[0]}";;
         [mM][aA][yY]*) echo "No. of days: ${month[1]}";;
         [jJ[uU][nN]*)  echo "No. of days: ${month[0]}";;
         [jJ][uU][lL]*) echo "No. of days: ${month[1]}";;
         [aA][uU][gG]*) echo " No. of days: ${month[1]}";;
         [sS][eE][pP]*) echo " No. of days: ${month[0]}";;
         [aA][cC][tT]*) echo "No. of days: ${month[1]}";;
         [nN][oO][vV]*) echo "No. of days: ${month[0]}";;
         [dD][eE][cC]*) echo "No. of days: ${month[1]}";;
esac

Output:
[ty2011330@localhost ~]$ vi month.sh
[ty2011330@localhost ~]$ sh month.sh
Enter month
feb
enter year
2005
No. of days : 28

shell script to accept two numbers from user and display n^y using while loop.

Code:
echo Enter value for n:
read n
echo Enter value for y:
read y
iter=1
sum=1
while [ $iter -le $y ]
do
        let sum=sum*n
        let iter=$iter+1
done
echo $sum



Output:
[ty2011330@localhost ~]$ sh file21.sh
Enter value for n:
2
Enter value for y:
3
8

shell script to accept a number from user and display multiplication table of given number.

Code:
echo "Multiplication Table:"
tab=1
i=1
a=10
echo "Input Number :"
read x
echo
echo "Table of $x"
for i in 1 2 3 4 5 6 7 8 9 10
do
   i=`expr $x \*  $i`
   echo $i
done


Output:
[pritam1@localhost ~]$ sh demofor.sh
Multiplication Table:
Input Number :
15

Table of 15
15
30
45
60
75
90
105
120
135
150

shell script for menu driven program to see the contents of /etc/passwd, to see the list of users, to see present working directory or to exit.

Code:
echo enter
echo 1 to see the contents of /etc/passwd
echo 2 to see list of users
echo 3 to see present working directory
echo 4 exit
echo enter your choice
read n
case $n in
 1)   ls /etc ;;
 2)   ls /home;;
 3)   pwd;;
 4)  exit;;
*) echo enter the choice as 1,2,3or4;;
esac

Output:
[ty2011330@localhost ~]$ sh  file13.sh
enter
1 to see the contents of /etc/passwd
2 to see list of users
3 to see present working directory
4 exit
enter your choice
2
administrator  pritam   ty11305  ty11313  ty11321  ty11329  ty11337  ty11345
amita          pritam1  ty11306  ty11314  ty11322  ty11330  ty11338  ty11346
dipti          shital   ty11307  ty11315  ty11323  ty11331  ty11339  ty11347
exam5154       shital1  ty11308  ty11316  ty11324  ty11332  ty11340  ty11348
enter your choice
1
a2ps.cfg               inittab                   quotatab
a2ps-site.cfg          inputrc                   racoon
acpi                   iproute2                  raddb

enter your choice
3
/home/tybsc
enter your choice
4





2.



ans=Y
while [ $ans = "Y" ]
do
echo "          MENU"
echo "1 . Query on product"
echo "2 . Query on customer"
echo "0 . Exit"
echo "enter ur choice : "
read ch
case "$ch" in
1) echo "Enter product number :"
  read pno
  grep $pno masterdata
  echo "Query on other product : "
  read ans
  if(test $ans = "Y")
  then
      continue
  fi;;
2) echo "Enter customer number :"   
   read cno
   grep $cno transdata;;
0) exit;;
*) echo "Invalid choice. Try again..."
esac
done      

Shell Program to find the type of the character entered

echo Enter any character
read c
case $c in
[a-z]) echo Small Case letter;;
[A-Z]) echo Capital letter;;
[0-9]) echo digit;;
    ?) echo special symbol;;
    *) echo more than one character, reenter;;
esac

11)#Program to find the pattern

echo Enter the word
read str
case $str in
[aeiou]*) echo The word begins with a vowel;;
  [0-9]*) echo The word starts with a digit;;
  *[0-9]) echo The word ends with a digit;;
    ????) echo The word entered is 4 lettered word;;
       *) echo The word entered is either starts with a
          consonant or incorrect input;;
esac

Shell Script to accept a file name and check whether the file name exist or not in current directory .

Code:
echo -n "Enter the Filename : "
read fname
if [ -f $fname ]
then
        echo "File exists in the current directory. "
else
        echo "File with this name does not exist in the current directory."
fi


Output:
[ty2011330@localhost ~]$ sh j03.sh
Enter the Filename : file5
File exists in the current directory.
[ty2011330@localhost ~]$ sh j03.sh
Enter the Filename : file12
File with this name doesnot exist in the current directory.

Shell Script to accept a number from user and check whether the number is even or odd .

Code:
echo -n "Enter the number : "
read n
let rem=n%2
if test $rem -eq 0
then
        echo "$n is a even number ."
else
        echo "$n is a odd number ."
fi


Output:
[ty2011330@localhost ~]$ sh j11.sh
Enter the number : 7
7 is a odd number .
[ty2011330@localhost ~]$ sh j11.sh
Enter the number : 10
10 is a even number .

Shell Script to accept a character from user and display whether it is alphabet, digit, special character or user has entered more than one character.

Code:
echo -n "Enter the Character : "
read c
case $c in
        [ABCDEFGHIJKLMNOPQRSTUVWXYZZ]) echo "Alphabet";;
        [a-z]) echo "Alphabet";;
        [0-9]) echo "Digit";;
        ?) echo "special Character";;
        *) echo "More than two Characters";;
esac
let i=2
echo -n "1!"
while test $i -le $x
do
        echo -n " + $i !"
        let i=$i+1
done
echo " = $sum"

Output:
[ty2011330@localhost ~]$ sh j12.sh
Enter the Character : A
Alphabet
[ty2011330@localhost ~]$ sh j12.sh
Enter the Character : f
Alphabet
[ty2011330@localhost ~]$ sh j12.sh
Enter the Character : 90
More than two Characters

shell script to accept a number from the user and display all prime numbers till the number user enters.

Code:
echo Enter limit
read limit
j=1
flag=1
echo "Prime Number Series"
while [ $j -le $limit ]
do
    i=2
    while [ $i -lt $j ]
    do
         temp=`expr $j % $i`
         if [ $temp -eq 0 ]
         then
               flag=0
               break;
         else
               flag=1
         fi
         i=`expr $i + 1`   
    done
    if test $flag -eq 1
    then
          echo $j
    fi
    j=`expr $j + 1`
done

Output:
Enter limit
15
Prime Number Series
1
2
3
5
7
11
13

shell script to accept a character from the user and check whether the word begins with vowel or begins with digit or ends with digit or is a four lettered word or a consonant.

Code:
echo Enter the word
read str
case $str in
[aeiou]*) echo The word begins with a vowel;;
  [0-9]*) echo The word starts with a digit;;
  *[0-9]) echo The word ends with a digit;;
    ????) echo The word entered is 4 lettered word;;
       *) echo The word entered is either starts with a
          consonant or incorrect input;;
esac

Output:
[ty2011330@localhost ~]$ sh file12.sh

Enter the word
a
The word begins with a vowel

Enter the word
hello
The word entered is either starts with a consonant or incorrect input

Enter the word
film
The word entered is 4 lettered word

Enter the word
*9
The word ends with a digit

shell script to accept a file name from user and check whether file has permission to write. If write permission is there then append to the file

Code:
echo Enter the file name
read fname
if [ -w $fname ]
then
   echo Type text to append. To stop press Ctrl D.
   cat >> $fname
else
   echo The file has no write permission
fi

Output:
[ty2011330@localhost ~]$ sh per.sh
Enter the file name
b2
The file has no write permission

[ty2011330@localhost ~]$ sh per.sh
Enter the file name
prime.sh
Type text to append. To stop press Ctrl D.

Shell Script to convert number in binary

echo Enter the number
read no
a=$no
s=0
x=1
while(test $a -ne 0)
do
    temp=`expr $a % 2`
    a=`expr $a / 2`
    y=`expr $temp "*" $x`
    s=`expr $s + $y`
    x=`expr $x "*" 10`
done

Shell Script to accept a number and display whether the number is positive, negative or zero.

Code:
echo -n "Enter the Number : "
read x
if [ $x -lt 0 ]
then
        echo "$x is Negative"
elif [ $x -eq 0 ]
then
        echo "Number entered is Zero"
else   
        echo "$x is Positive"
fi


Output:
[ty2011330@localhost ~]$ sh j02.sh
Enter the Number : 7
7 is Positive
[ty2011330@localhost ~]$ sh j02.sh
Enter the Number : -9
-9 is Negative
[ty2011330@localhost ~]$ sh j02.sh
Enter the Number : 0
Number entered is Zero

shell script to accept a number from the user and display even number series till that number

Code:
no=0
echo "Enter limit"
read limit
echo "Even number series"
while [ $no -le $limit ]
do
    echo $no
    let no= $no + 2
done



Output:
[ty2011330@localhost ~]$ sh even.sh
Enter limit
10
Even number series
0
2
4
6
8
10

Shell program to display summation of 1+2+3+4………+n

Code:
echo Enter the value for n:
read num
sum=0
iter=1
while [ $iter -le $num ]
do
        let sum=$sum+$iter
        let iter=$iter+1
done
echo $sum


Output:

[ty2011330@localhost ~]$ sh summation.sh
Enter the value for n:
10
55