SP cs6
SP cs6
SP cs6
1. write a shell script to find sum of first N numbers starting from Number M
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
N=6 0 1 1 2 3 5 ...
N=3 0 1 1
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
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
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.
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.
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.