Sie sind auf Seite 1von 6

Homework Title / No. : 01 Course Instructor : Ms.

Darvinder Kaur Date of Allotment : 02/04/2012

Course Code : CSE207 Course Tutor (if applicable) : Ms. Darvinder kaur Date of submission : 20/02/2012

Students Roll No.RK3R13B21


Declaration:

Section No. :k3r13

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 : Arun Thakur Evaluators comments: _____________________________________________________________________ Marks obtained : ___________ out of ______________________ Content of Homework should start from this page only:

Q1. Linux scripts do not need support comparison symbols like (<,>, etc.) What is the alternative to do this? Explain with an example. Ans. For this we can use alternatives as following:
a)For < we use gt

Example:

Instead of writing if [ a<b ];then Echo a is lesser than b we just write if [ a lt b ];then echo a is lesser than b b) For > symbol we use gt

Example:

Instead of writing if [ a>b ];then Echo a is greater We just write If [ a gt b ];then Echo a is greater

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.
echo enter the first directory name read a echo enter the second directory name read b if [ -d $a ] && [ -d $b ] then

for file in $(ls $a/*) do if [ -f $file ] then cp r $file $b else echo not an ordinary file fi done else echo Not a directory fi

Uses of /boot and /tmp directory:/boot:As the name suggests, this is the place where Linux keeps information that it needs when booting up. 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.

/tmp:This directory is used to temporary files that are large or need to exist for longer than they should in /tmp.

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.
echo enter the source read a echo enter the destination read b if [ -d $a ] && [ -d $b ] then for file in $(ls $a/*) do if [ -f $file ] then cp $file $b elif [ -d $file ] then echo not a file fi done else echo Not a valid directory fi

Q4. Program using shell script to print Fibonacci series upto the number ( counted as number of parameter passed at run time). Ans.
a=0 b=1 echo $a echo $b for((i=1;i<=$#;i++)) do c=`expr $a + $b` ((a=b)) ((b=c)) echo $c done

Output:
arun@ubuntu:~ bash fab a b c 0 1 1 2 3

Q5. Write a shell script to print the GP series where the last term generated does not exceeds 200. Ans.
Echo Enter the ratio for the GP series Read a Echo Enter the digit from where you have to start the series Read b While((b<200)) Do Echo $b ((b=b*a)) Done Exit 0

Output:arun@ubuntu~$bash gp Enter the ratio for the GP series 2 Enter the digit from where you have to start the series 1 1 2 4 8 16 32 64

128

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.
degree() { echo Enter the name read a echo Enter the age read b echo Enter the gender read c echo Enter the 10+2 marks percentage read d if((d>60)) Then echo Courses available: echo e 1) B.Tech echo e 2) BCA echo Select option read e case $e in 1) echo e CSE-20\tMEC-34\tECE-36;; 2) echo e Available seats\t60;; *) echo wrong option;; esac else echo not eligible fi } degree

Output:

arun@ubuntu:~$bash deg Enter the name Arun Enter the age 18 Enter the gender Male Enter 10+2 marks percentage 67 Courses available: 1) B.Tech 2) BCA Select option 1

CSE-20 MEC-34 ECE-36

Q7. Write a program using system call to open a file and read the value of variables and display on terminal . Ans. #include<stdio.h>
#include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<sys/types.h> int main() { int a; char fn; char c; printf(Enter the file name:); scanf(%s,&fn); A= open (&fn,O_RDONLY); while (read(a,&c,1)==1) write(1,&c,1); return 0; }

Q8. Write a program to copy the content of one file into another file using system calls. Ans.
#include<unistd.h> #include<sys/stat.h> #include<fcntl.h> #include<stdlib.h> int main() { char c; int a,b; a=open(ar.txt,O_RDONLY); b=open(ni.txt,O_WRONLY|O_CREAT,S_IRUSR|S_IWUSR); while(read(a,&c,1)==1) write(b,&c,1); exit(0); }

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 tree

cd tree mkdir d1 d2 cd d1 touch file1 cd .. cd d2 mkdir d3 cd d3 cat >file2 cat>file3

(a) Copy the content of file2,file3 in file1:cat >> /home/arun/tree/d1/file1 /home/arun/tree/d2/d3/file2 cat >> /home/arun/tree/d1/file1 /home/arun/tree/d2/d3/file3

b) Delete the directory d3:cd .. rmdir d3

c) Change the permission of file1 as r-x-wxr:cd .. cd d1 chmod 534 file1

d) Create file4 in d2 having owner name as user123:cd .. cd d2 touch file4 chown user123 file4

Q10. Write a shell script to display the factorial of 5 and run it upto 4 Ans.
((fact=1)) ((n=5)) For((a=1;a<=n;a++)) Do ((fact=fact*a)) Done Echo $n $fact Exit 0

Das könnte Ihnen auch gefallen