Sie sind auf Seite 1von 45

Assignment no : Date : Assignment name : Write a shell program to check a given no is odd or even

echo "enter a no." read n c=` expr $n % 2 ` if [ $c -eq 0 ];then echo "The no is even" else echo "The no is odd" fi

Output:$ evenodd.sh enter a no. 65 The no is odd $ evenodd.sh enter a no. 12 The no is even

Assignment no : Date : Assignment name : Write a shell program to calculate the Factorial of a given number echo "Enter a number" read f if [ $f -lt 0 ];then echo "Improper number" exit elif [ $f -eq 0 ];then echo "The factorial of zero is 1" else i=1 c=1 while [ $c -le $f ];do i=` expr $i \* $c|bc ` c=` expr $c + 1 ` done echo "The factorial of $f is $i" fi

Output:$ fact.sh Enter a number 0 The factorial of zero is 1 $ fact.sh Enter a number -5 Improper number $ fact.sh Enter a number 6 The factorial of 6 is 720

Assignment no : Date : Assignment name : Write a shell program to print the first n Fibonacci number echo "Enter the nth position of the Fibonacci series" read n f=` expr $n - 1 ` i=1 a=0 b=1 echo "The series is " echo $a while [ $i -le $f ];do c=` expr $a + $b ` echo $b a=$b b=$c i=` expr $i + 1 ` done

Output :$ fib.sh Enter the nth position of the Fibonacci series 10 The series is 0 1 1 2 3 5 8 13 21 34

Assignment no : Date : Write a shell program to check whether a number is Prime or not
echo "Enter a number" read n i=2 if [ $n -eq 1 ];then echo "Improper number" else f=` expr $n / 2 ` while [ $i -lt $f ];do t=` expr $n % $i ` if [ $t -eq 0 ];then echo "The given number $n is not a prime number" exit fi i=` expr $i + 1 ` done echo "The given number $n is a prime number" fi

Output :$ prime.sh Enter a number 1 Improper number $ prime.sh Enter a number 65 The given number 65 is not a prime number $ prime.sh Enter a number 23 The given number 23 is a prime number

Assignment no : Date : Assignment name : Write a shell program to display the nonfibonacci number up to a given number
echo "Enter the number" read n i=1 a=0 b=1 d=1 echo "The non-fibonacci numbers are" while [ $d -le $n ];do c=` expr $a + $b ` while [ $c -ne $d ];do echo $d d=` expr $d + 1 ` if [ $d -eq $n ];then if [ $c -ne $d ];then echo $d exit fi fi done d=` expr $d + 1 ` a=$b b=$c i=` expr $i + 1 ` done

Output :$ nonfib.sh Enter the number 10 The non-fibonacci numbers are 4 6 7 9 10 $ nonfib.sh Enter the number 13 The non-fibonacci numbers are 4 6 7 9 10 11 12

Assignment no : Date : Assignment name : Write a shell program to check whether a given number is palindrome or not echo "Enter a number" read n f=$n rev=0 while [ $f -gt 0 ];do a=` expr $f % 10 ` f=` expr $f / 10 ` f1=` expr $rev \* 10 ` rev=` expr $f1 + $a ` done echo "The reverse of $n is $rev" if [ $n -eq $rev ];then echo "The given number is palondrome" else echo "The given number is not palindrome" fi

Output:$ palindrome.sh Enter a number 6254526 The reverse of 6254526 is 6254526 The given number is palondrome $ palindrome.sh Enter a number 54123 The reverse of 54123 is 32145 The given number is not palindrome

Assignment no : Date : Assignment name : Write a shell program to check whether a given number is Armstrong or not echo "Enter a number" read n arm=0 l=` expr $n|wc -c ` temp=$n while [ $temp -gt 0 ];do temp1=` expr $temp % 10 ` temp2=$temp1 i=2 while [ $i -lt $l ];do temp1=` expr $temp1 \* $temp2 ` i=` expr $i + 1 ` done arm=` expr $arm + $temp1 ` temp=` expr $temp / 10 ` done if [ $arm -eq $n ];then echo "The given number is armstrong number" else echo "The given number is not a armstrong number" fi

Output:$ armstrong.sh Enter a number 452 The given number is not a armstrong number $ armstrong.sh Enter a number 153 The given number is armstrong number

Assignment no : Date : Assignment name : Write a shell program to convert a given number from decimal binary echo "Enter a number" read n echo "Enter Input Base" read ib echo "Enter Output Base" read ob out=` echo "obase=$ob;ibase=$ib;$n"|bc ` echo "The number $n is converted into $out"

Output:$ d2b.sh Enter a number 69 Enter Input Base 10 Enter Output Base 2 The number 69 is converted into 1000101

Assignment no : Date: Assignment name : Write a shell program to check whether a year is leap year or not echo "Enter a Year" read y a=` expr $y % 400 ` b=` expr $y % 100 ` c=` expr $y % 4 ` if [ $a -eq 0 ];then echo "$y is a Leap-year" elif [ $b -ne 0 -a $c -eq 0 ];then echo "$y is a Leap-year" else echo "$y is not a Leap-year" fi

Output:$ leapyear.sh Enter a Year 2000 2000 is a Leap-year $ leapyear.sh Enter a Year 1998 1998 is not a Leap-year $ leapyear.sh Enter a Year 1980 1980 is a Leap-year

Assignment no : Date : Assignment name: Write a shell program to calculate GCD of two numbers echo "Enter two numbers" read a b if [ $a -gt $b ];then t=$a a=$b b=$t fi r=` expr $b % $a ` while [ $r -ne 0 ];do b=$a a=$r r=` expr $b % $a ` done echo "The GCD of two numbers is $a"

Output:$ gcd.sh Enter two numbers 36 9 The GCD of two numbers is 9 $ gcd.sh Enter two numbers 16 96 The GCD of two numbers is 16 $ gcd.sh Enter two numbers 23 63 The GCD of two numbers is 1

Assignment no : Date : Assignment name :Write a shell program to copy the content one file to another file. Filename given as command line argument sfile=$1 dfile=$2 echo "Display the source file" cat $sfile echo "Copy the file" cp $sfile $dfile echo "Display the destination file" cat $dfile

Output:$ cf.sh f1 f2 Display the source file WE ARE THE STUDENT OF VIDYASAGAR UNIVERSITY Copy the file Display the destination file WE ARE THE STUDENT OF VIDYASAGAR UNIVERSITY

Assignment no : Date : Assignment name : Write a shell program to calculate GCD and LCM of two numbers echo "Enter two numbers" read a b if [ $a -gt $b ];then t=$a a=$b b=$t fi p=` expr $a \* $b ` r=` expr $b % $a ` while [ $r -ne 0 ];do b=$a a=$r r=` expr $b % $a ` done l=` expr $p / $a ` echo "The GCD of two numbers is $a" echo "The LCM of two numbers is $l"

Output:$ gcd.sh Enter two numbers 56 The GCD of two numbers is 1 The LCM of two numbers is 30 $ gcd.sh Enter two numbers 36 6 The GCD of two numbers is 6 The LCM of two numbers is 36 $ gcd.sh Enter two numbers 28 6 The GCD of two numbers is 2 The LCM of two numbers is 84

Assignment no : Date : Assignment name : Write a shell program to calculate NCR echo "Enter the value of n and r" read n r s1=1 i=1 while [ $i -le $n ];do s1=` expr $s1 \* $i ` i=` expr $i + 1 ` done s2=1 i=1 while [ $i -le $r ];do s2=` expr $s2 \* $i ` i=` expr $i + 1 ` done s3=1 i=1 p=` expr $n - $r ` while [ $i -le $p ];do s3=` expr $s3 \* $i `

i=` expr $i + 1 ` done m=` expr $s2 \* $s3 ` t=` expr $s1 / $m ` echo "The result is $t"

Output:$ ncr.sh Enter the value of n and r 53 The result is 10 $ ncr.sh Enter the value of n and r 94 The result is 126

Assignment no : Date : Assignment name : Write a shell program to calculate NPR echo "Enter the value of n and r" read n r s1=1 i=1 while [ $i -le $n ];do s1=` expr $s1 \* $i ` i=` expr $i + 1 ` done s3=1 i=1 p=` expr $n - $r ` while [ $i -le $p ];do s3=` expr $s3 \* $i ` i=` expr $i + 1 ` done t=` expr $s1 / $s3 ` echo "The result is $t"

Output:$ npr.sh Enter the value of n and r 53 The result is 60 $ npr.sh Enter the value of n and r 94 The result is 3024

Assignment no : Date: Assignment name : Write a shell program to remove all numbers that occurs more than one in a shorted array echo "Enter the array range" read n i=1 while [ $i -le $n ];do read a[i] i=` expr $i + 1 ` done echo ${a[*]} i=1 while [ $i -le $n ];do temp=${a[$i]} echo $temp>>f2 i=` expr $i + 1 ` done i=0 for i in `cat f2`;do

echo $i>>f3 done sort -u f3>>f4 i=1 for j in `cat f4`;do a[$i]=$j i=` expr $i + 1 ` done j=1 echo "The final result-----------" while [ $j -lt $i ];do echo ${a[$j]} j=` expr $j + 1 ` done

Output:$ delda.sh Enter the array range 7 65 25 95 65 33 65 33 65 25 95 65 33 65 33 The final result----------25 33 65 95

Assignment no : Date : Assignment name : Write the list of files which have read , write and execute permission for fname in `ls l` ;do if [ -f $fname a r $fname a w $fname a x $fname ];then echo "$fname has all permission" fi done

Output:SSS.SH has all permission

Assignment no : Date : Assignment name : Write a shell program to convert a given number from binary decimal echo "Enter a number" read n echo "Enter Input Base" read ib echo "Enter Output Base" read ob out=` echo "obase=$ob;ibase=$ib;$n"|bc ` echo "The number $n is converted into $out"

Output:$ d2b.sh Enter a number 1000101 Enter Input Base 2 Enter Output Base 10 The number 1000101 is converted into 69

Assignment no : Date : Assignment name : Write a Shell script to apply binary search method on virtual array.
#binary search over an array..... clear echo -en "\n\nEnter the total no. of elements: " read n m=0 while [ $m -lt $n ] ; do let k=m+1 echo -en "\n\nEnter the element $k: " read a[$m] m=`expr $m + 1` done echo -en "\n\nEnter the element to be searched: "

read sn beg=0 end=`expr $n - 1` while [ $beg -le $end ] ; do mid=`expr $beg + $end` mid=`expr $mid / 2` if [ $sn -eq ${a[$mid]} ] ; then break elif [ $sn -lt ${a[$mid]} ] ; then end=`expr $mid - 1` else beg=`expr $mid + 1` fi done if [ $beg -le $end ] ; then mid=`expr $mid + 1` echo -e "\n\nElement found at: $mid" else echo -e "\n\nElement is not found !!!!!" fi echo -e "\n\nEnd of program ......."

OUTPUT:

Enter the total no. of elements: 8 Enter the element 1: 7 Enter the element 2:47 Enter the element 3: 16 Enter the element 4: 20 Enter the element 5: 1 Enter the element 6: 14 Enter the element 7: 2 Enter the element 8: 10 Enter the element to be searched: 2 Element found at: 7 End of program .......

Assignment no : Date : Assignment name : Write a Shell script to apply selection sorting method on virtual array
#Selection_sort over an array...... clear echo -en "\n\nEnter the total no. of elements: " read n m=0 while [ $m -lt $n ] ; do k=`expr $m + 1` echo -en "\n\nEnter element no. $k: " read a[$m] m=`expr $m + 1` done echo -e "\n\nYour entered sequence is:-->" m=0 while [ $m -lt $n ] ; do k=`expr $m + 1`

echo -en "\n\nThe element $k is: ${a[$m]} " m=`expr $m + 1` done i=0 while [ $i -lt $n ] ; do min=${a[$i]} pos=$i j=`expr $i + 1` while [ $j -lt $n ] ; do if [ ${a[$j]} -lt $min ] ; then min=${a[$j]} pos=$j fi j=`expr $j + 1` done t=${a[$i]} a[$i]=$min a[$pos]=$t i=`expr $i + 1` done echo -e "\n\nSorted sequence:-->" m=0 while [ $m -lt $n ] ; do k=`expr $m + 1` echo -en "\n\nThe element $k is: ${a[$m]} " m=`expr $m + 1` done echo -e "\n\nEnd of program...."

OUTPUT:
Enter the total no. of elements: 6 Enter element no. 1: 2 Enter element no. 2: 15 Enter element no. 3: 10 Enter element no. 4: 7 Enter element no. 5: 1 Enter element no. 6: 5 Your entered sequence is:--> The element 1 is: 2 The element 2 is: 15 The element 3 is: 10

The element 4 is: 7 The element 5 is: 1 The element 6 is: 5 Sorted sequence:--> The element 1 is: 1 The element 2 is: 2 The element 3 is: 5 The element 4 is: 7 The element 5 is: 10 The element 6 is: 15

Assignment no : Date : Assignment name : Write a Shell script to apply insertion sorting method on virtual array
#insertion_sort over an array...... clear echo -en "\n\nEnter the total no. of elements: " read n m=0 while [ $m -lt $n ] ; do k=`expr $m + 1` echo -en "\n\nEnter element no. $k: " read a[$m] m=`expr $m + 1` done echo -e "\n\nYour entered sequence is:-->" m=0 while [ $m -lt $n ] ; do

k=`expr $m + 1` echo -en "\n\nThe element $k is: ${a[$m]} " m=`expr $m + 1` done i=1 while [ $i -lt $n ] ; do temp=${a[$i]} j=$i while [ $j -gt 0 ] ; do h=`expr $j - 1` if [ $temp -lt ${a[$h]} ] ; then a[$j]=${a[$h]} else break fi j=`expr $j - 1` done i=`expr $i + 1` a[$j]=$temp done echo -e "\n\nSorted sequence:-->" m=0 while [ $m -lt $n ] ; do k=`expr $m + 1` echo -en "\n\nThe element $k is: ${a[$m]} " m=`expr $m + 1` done echo -e "\n\nEnd of program...."

OUTPUT:
Enter the total no. of elements: 6 Enter element no. 1: 4 Enter element no. 2: 7 Enter element no. 3: 12 Enter element no. 4: 17 Enter element no. 5: 1 Enter element no. 6: 5 Your entered sequence is:--> The element 1 is: 4 The element 2 is: 7 The element 3 is: 12

The element 4 is: 17 The element 5 is: 1 The element 6 is: 5 Sorted sequence:--> The element 1 is: 1 The element 2 is: 4 The element 3 is: 5 The element 4 is: 7 The element 5 is: 12 The element 6 is: 17

Assignment no : Date: Assignment name :Program of linear search. echo Enter the no. of elements read n i=1 while test $i -le $n do num=a$i echo Enter the search element read $num i=`expr $i + 1` done

echo Enter the item read item i=1 while test $i -ie $n do var =a$i if eval [\$$var -eq $item] then echo Search is successful echo position of item is $i exit fi i=`expr $i + 1` done echo Search is unsuccessful

Output

Enter the no. of elements 5 Enter the element 12 20 45 63 21

Enter the search element 20 Search is successful Position of item is 2 Enter the search element 23 Search is unsuccessful

Assignment no : Date : Assignment name : write a shell program for bubble sort echo Enter no of elements read n i=1 while [$i -le $n] do num=a$i echo Enter the element read $num i=`expr $i + 1` done

i=1 while [$i -lt $n] do j=1 while [$j -le $ ` expr $n - $i `] do var1=a$j var2=a`expr $j +1` if eval [ \$$var1 -gt \$$var2] then eval t =\$$var1 eval $var1=\$$var2 eval $var2=$t fi j=`expr $j + i` done i=`expr $i + 1` done echo The sorted list i=1 while[$i -le $n] do num=a$i eval echo \$$num i=`expr $i +1 ` done

Output
Enter the no. of elements 5 Enter the element 12

20 45 63 21 The sorted list is 12 20 21 45 63

Das könnte Ihnen auch gefallen