You are on page 1of 8

Homework Title / No.: 1 Course Code: CSE-306 Course Instructor: Ms.

Darvinder Kaur Course Tutor (if applicable): Date of Allotment: 04-02-2012 Date of submission: 20-02-2012 Students Roll NO: RK3R13B47 Section No: K3R13 Declaration: I declare that this assignment is my individual work. I have not copied from any other students work or from any other source except where due acknowledgment is made explicitly in the text, nor has any part been written for me by another person. Students Signature: Shubham Jain Q1. Linux scripts do not need support comparison symbols like (<,>, etc.) What is the alternative to do this? Explain with an example. Ans: Arithematic expressions Expression1 -eq Expression2 Expression1 -ne Expression2 Expression1 -gt Expression2 Expression1 -ge Expression2 Expression1 -lt Expression2 Expression1 -le Expression2 Example: 1)a=4 b=56 if [ $a -lt $b ] ; then echo a is lesser than b 2)a=47 b=56 if [ $a -gt $b] ; then echo a is greater than b 3)a=56 b=56 if [ $a -le $b ]; then echo a is less than or equal to b Comparision Result Equals to Not equal to Greater than Greater than or equal to Lesser than Lesser than or equal to

Q2. Write a shell script to copy only ordinary files of a directory to another directory. If it encounters other than ordinary files, it should give appropriate message. It should also copy the files to its subdirectories. Illustrate the use of /boot and /tmp directory Ans: Shell script: read -p Enter the name of the source directory dir read -p Enter the name of the destination directory dest cd $dir for check in $(ls *) do if [ -f $check ] ; then cp $check $dest fi if [ -d $check ] ; then echo A sub directory $check has been encountered cd $check for file in $(ls *) do if [ -f $file ] ;then cp $file $2 fi done cd .. fi done exit 0
Use of </boot>

As the name suggests, this is the place where Linux keeps information that it needs when booting up.It consists of all the details which initiates the boot sequence. For example, this is where the Linux kernel is kept. If you list the contents of /boot, you'll see a file called vmlinuz - that's the kernel.
Use of < /tmp >

Programs can write their temporary files here.All the temporary files are written into this directory.

Q3. Write a shell script having the same name as your roll no. It should input the source and destination directory names and copy only the files from source to destination. If it encounters subdirectory, it should not copy and provide appropriate message for the same. Ans: nano b47.sh read -p "Enter the source directory" source read -p "Enter the destination directory" dest cd $source for file in $( ls * ) do if [ -f $file ] ; then cd .. cp $source/$file $dest/$file cd $source else echo " All files have copied and Sub directory found is $file" fi done Q4. Program using shell script to print Fibonacci series upto the number ( counted as number of parameter passed at run time) Ans. Shell Script: a=0 b=1 echo "The number of parameters passed are $# ((p=$#-1)) echo -n "The fibonacci series :" echo -n $a while((k<p)) do ((c=a+b)) ((a=b)) ((b=c)) ((k++)) echo -n " $c" done echo

Q5. Write a shell script to print the GP series where the last term generated does not exceeds 200. Ans: Shell Script: read -p "Enter the first term of the Geometric progression series ft read -p "Enter the first term of the Geometric progression series cr sum=$ft while [ $sum lt 200 ] do echo "$sum" sum=`expr $sum \* $cr`; done Q6. Using shell script, Create a function named degree which takes the input from user as name, age, gender , 10,+2 marks and then display the course for which the candidate is eligible and then the final selection is made based upon the number of seats if available. Ans:Shell Script: choice='y' f_chck=0 s_chck=0 #Files for storing the records touch file1 file2 degree() { echo "1.BBA" echo "2.B.Tech" read -p "Enter your choice" c read -p "Age:" age read -p "12th marks" marks1 read -p "Name" nm read -p "Gender" gen read -p "10th marks" marks2 if [ c eq 1 ] then if [ f_chck lt 5 ] && [ marksl gt 60 ] then cat $nm >> f1 cat $gen >>f1 cat $marksl>>f1 cat $marks2 >>f1 cat $age >>f1 echo "Registration Successful in BBA"

f_chck=`expr $f_chck + 1` fi else if [ $s_chck lt 5 ] &&[ $marks1 gt 60] then cat "$nm" >> f2 cat $gen >>f2 cat $marksl>>f2 cat $marks2 >>f2 cat $age >>f2 echo "Registration successful in B.Tech" s_chck=`expr $s_chck + 1` fi fi } k=1 while [ k eq 1 ] do read -p "Do u want to enter more" choice case $choice in y|Y) degree;; *) k=5;; esac done

Q7. Write a program using system call to open a file and read the value of variables and display on terminal . Ans: #include<sys/stat.h> #include<fcntl.h> #include<sys/types.h> #include<stdio.h> void main() { int m,p; char arr[12]; m=open(File1",O_RDONLY); if (m!=-1) { int p=read(m,&arr,12); printf("\n\t%s",arr);

} } Q8. Write a program to copy the content of one file into another file using system calls. Ans: #include<sys/stat.h> #include<sys/types.h> #include<stdio.h> #include<fcntl.h> void main() { int i,j,c,d,k; char arr[12]; i=open("SrcFile",O_RDONLY); if (i!=-1) { j=read(i,&arr,12); } printf ("Source File contents are %s",arr); k=open("Des_File",O_RDONLY|O_WRONLY|O_CREAT); if(k!=-1) { c =write(k,&arr,j); d=lseek(k,0,SEEK_SET); read(k,&arr,j); printf("%s",arr); } } Q9. Create a directory tree consisting of d1 and d2 consisting of file1 and d3 resp and further d3 consist of file2 and file3. a. Copy the content of file2,file3 in file1. b. Delete the directory d3. c. Change the permission of file1 as r-x-wxr d. Create file4 in d2 having owner name as user123 Ans: mkdir d1 cd d1 touch file1 cd .. mkdir -p d2/d3

cd d2/d3 touch file2 file3 echo "Write the contents of the I file" cat >>file2 echo "Write the contents of the II file" cat >>file3 cd ~ #Copying the contents of file2,file3 in file1 cp d2/d3/file2 d1/file1 cat d2/d3/file3 >> d1/file1 # Deleting the directory d3 interactively cd d2 rm -ri d3 cd .. cd d1 ls -l file1 #Changing the file permission of file1 as r-x-wxr chmod 534 file1 echo ls -l file1 cd ~ #Creating file4 in d2 cd d2 touch file4 echo "current owner name" ls -l file4 #Changing the owner sudo adduser user123 sudo chown user123 d2/file4 ls -l file4

Q10. Write a shell script to display the factorial of 5 and run it up to 4. Ans:

i=2 result=1 while [ $i -le 5 ] do result=`expr $result \* $i` i=`expr $i + 1` done fi echo "Factorial of 5 = $result"

You might also like