SP cs6

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 4

find command

find <searchpath> <searchPattern> <search Action>


-perm
-name
-type
-mtime
-atime
-inum
-user
-group
-links

1. write a shell script to find sum of first N numbers starting from Number M

M=5 ; N=4; 5,6,7,8 26


M=5 N=5 ; 5,6,7,8,9 35
start= M
EndNos= M+N-1

for ((i=M,sum=0;i<=M+N-1;i++))
do
sum=`expr $sum + Si`
done
echo $sum

Fact(N)= 1X2X...N

for ((i=1,fact=1;i<=N;i++))
do
fact=`expr $fact \* $i`
done
echo $fact

Reversal of a number

N=12345
rev=54321
for ((i=N,rev=0;i>0;i=i/10)
do
digit= `expr $i % 10`
rev=`expr \( $rev \* 10 \) + digit
done
echo $N reverse is $rev
iter 1 : i=12345; digit=5 ; rev=5
iter2: i=1234 ; digit=4 ; rev=54
iter3: i=123 ; digit=3 ; rev=543
iter4: i=12 ; digit=2 ; rev=5432
iter5 i=1 ; digit=1 ; rev=54321

write a shell script to generate a Fibbonaci series for length N.


f0=0
f1=1
f(n)=f(n-1) + f(n-2) ; n>=2
f(2)=1
f(3)= 2

N=6 0 1 1 2 3 5 ...
N=3 0 1 1

echo "enter N"


read N
if [ $N -lt 1 ]
echo invalid
exit
fi

f0=0
f1=1
i=1
while [ $i -le $N ]
do
echo $f0
M=$f0
f0=$f1
f1=`expr $f0 + $M
i=`expr $i +1`
done

iter1 , 0 , 1,

Nested Loops

Mult Table: 5 , 8

5X1=5
5X2=10

5X10=50

6X1=6

for ((i=M ; i<=N; i++))


do
for ((j=2,flag=0; j<=i/2; j++))
do
if [ `expr $i % $j` -eq 0 ]; then
flag=1
break
fi
done
if [ $flag -eq 0 ]
then
echo $i
fi
done

script to find all even numbers.

while read line


do
if [ `expr $line % 2` -eq 0 ]
then
echo $line
fi
done <integernos.txt

CS7
1. Write a shell script to find all prime numbers from integernos.txt file and
store in another file
called primes.txt.

2.Create a text file called integernos.txt and store some random numbers in it
(onenumber in one line).]
Write a shell script program to display all the even numbers from the file

3.Write a script to print the first N numbers in Fibonacci series


Given, f0=0, f1=1, and fn=fn1+fn2 for all n>=2.

4.Write a shell script that receives two file names as arguments. It should check
whether
#the two files contents are same or not. If they are same then second file should
be deleted.

5. Write a script to copy files to a directory only when they don’t exist there.
The filenames are
#supplied as arguments and the last argument is the directory.

6. script to find the file size.

7. Write a shell function that locates a directory supplied as argument in the home
directory tree and switches to it. Will the same code work if placed in a shell
script.

8. script to find the hard and soft links of a given file.

9.Write a script that looks up every .c file in the current directory for the
strings printf or fprintf.
If found the script adds the statement # include <stdio.h> at the beginning of the
file but only if it doesn’t already have it included.

You might also like