Anda di halaman 1dari 31

1

Index
Question
Q1: Demonstration of filter Q:2 Demonstrate of Redirection operation Q:3 Demonstrate the general purpose utility Q : 4 Write a shell script using command line argument to perform all arithmetic operation. Q5: Demonstrate Use of ace Construct To Display Greeting Message. Q:6 Using for Loop in shell script display multiplication table Q7: Write a interactive shell script to accept a perttern from the user and find it in a text file Q :8 Write a shell script to change extension of files. Q :9 Concatenate and join two user specified file using System call specified File using System Call Q :10 Find the attributes of given file. Q:11 Count the number of characters,line,and words. Q12: Write a shellscript to count number of directories and regulars files in present Working directories Q13: Write a C program using error handling system call to display numbers of error available in the operating system
Sign Remar ks

Q:14 A shellscript to concate 2 user defined files

Q.1) Demonstration of filters


Code:
#!bin/sh echo "Filters" printf "\n" echo " 1) cat: Display the content of file" echo "cat a1.txt" cat a1.txt printf "\n" echo "2) head: To filter out the begninigs of the string" echo "head -2 a1.txt" head -2 a1.txt printf "\n" echo "3) tail: Display ending part of file" echo "tail -3 a1.txt" tail -3 a1.txt printf "\n" echo "4) cut: cut in vertical section" echo "cut -c 1-5 a1.txt" cut -c 1-5 a1.txt printf "\n" echo "5) paste: Paste the 2 or more files vertically" echo "cat abc.txt" cat abc.txt echo "paste a1.txt abc.txt" paste a1.txt abc.txt

OUTPUT:

Q.2) Demonstrate of Redirection operation


Code:
#!bin/sh echo "Demonstrate the input Direction" echo " infile.txt" cat infile.txt echo " l w c " wc < infile.txt echo "Demonstrate the output Direction" echo "infile1.txt" cat infile1.txt cat infile1.txt infile.txt > output.txt echo " cat infile1.txt infile.txt > output.txt" echo " output.txt" cat output.txt echo "Append output direction" echo "This text is append in append.txt file to demonstrate the Append Output Direction" >> append.txt cat append.txt

OUTPUT:

Q.3) Demonstrate the general purpose utility


Code:
#!bin/sh echo "General Purpose Utilities" printf "\n" echo " 1) echo:Display Messages" printf "echo hello" echo "hello" printf "\n" echo " 2) date :Display system date" echo "date" date date +"%d %h %m" printf "\n" echo " 3) who:Show he maintained a/c of all users" echo "who" who printf "\n" echo " 4) ls : show all files and directory" echo "ls" ls printf "\n" echo " 5) cat: Display the content of file" echo "cat a1.txt" cat a1.txt printf "\n" echo " 6) cal:Display calender of a particular month or for a entire year" echo "cal 2011" cal 2011

OUTPUT:

Q.4) Write a shell script using command line argument to perform all arithmetic operation.
Code:
#!bin/sh echo "The shell script using command line argument to perform all arithmetic operation " if [ $# -ne 2 ] then echo "\n Please enter two number as command line argument\n " else echo "First no. is : $1" echo "Second no. is : $2" echo " " echo "Enter the choice" echo " " echo "Press 1 for Addition" echo "Press 2 for subtraction" echo "Press 3 for Multiplication" echo "Press 4 for Division" printf "\n" read choice case $choice in 1) a=$(($1+$2)) echo " " echo "Addition is: $a";; 2) a=$(($1-$2)) echo " " echo "Subtration is: $a";; 3) a=$(($1*$2)) echo " " echo "Multiplication is: $a";; 4) a=$(($1/$2)) echo " " echo "Division is: $a";; *) echo "Wrong choice";; esac fi

OUTPUT

Q.5) Demonstrate Use of ace Construct To Display Greeting Message.


Code:
#!bin/sh time=`date +%H` echo "Time $time hr " case "$time" in [0-9] ) echo "Good Morning" ;; [1][1]) echo "Good Morning" ;; [1][2]) echo "Good Noon" ;; [1][3-6]) echo "Good Afternoon" ;; [1][6-9]) echo "Good Evening" ;; [2][0]) echo "Good Evening" ;; [2][1-3]) echo "Good Night" ;; *) echo "wrong";; esac

OUTPUT:

Q.6) Using for Loop in shell script display multiplication table


code:
#!bin/sh echo "" echo "-----------------------------------" echo " Multiplication table" echo "-----------------------------------" echo "" echo "" for i in 1 2 3 4 5 6 7 8 9 10 do for j in 1 2 3 4 5 6 7 8 9 10 do printf $(($i*$j)) printf "\t" done echo "" done

OUTPUT:

Q.7 )Write a interactive shell script to accept a parttern from the user and find it in a text file
Code:
#!bin/sh echo "Enter the patter u want to matched: " echo " " read pat if grep "$pat" abc.txt then echo "Pattern is Found" else echo "Pattern is not Found" fi echo " "

Q.8) Write a shell script to change extension of files.


Code:
#!bin/sh echo "These are the following files present in current directory" echo " " ls for file in *.doc do name=`basename $file doc` mv $file ${name}txt done echo "These are the following files after the basecommand in current directory" echo " " ls

OUTPUT:

Q.9) Concatenate and join two user specified file using System call specified File using System Call
Code:
#include<stdio.h> #include<fcntl.h> #include<sys/stat.h> int main() { int fd1=open("./xyz.txt", O_RDONLY); int fd2=open("./abc.txt", O_RDONLY); int fd3=open("./concate.txt",O_WRONLY | O_CREAT,0666); int s1,s2,i; char ch,a='\n'; s1 =lseek(fd1,-1,SEEK_END); lseek(fd1,0,SEEK_SET); s2 =lseek(fd2,-1,SEEK_END); lseek(fd2,0,SEEK_SET); for(i=1;i<=s1;i++) { read(fd1,&ch,1); write(fd3,&ch,1); printf("%c",ch); } lseek(fd3,0,SEEK_END); write(fd3,&a,1); for(i=1;i<=s2;i++) { read(fd2,&ch,1); write(fd3,&ch,1); printf("%c",ch); } }

OUTPUT

Q.10) Find the attributes of given file.


Code: #!bin/sh echo "The file attribute it is readile writable or exectuble " ch=y while [ $ch = y ] do echo "enter the file name" read file1 if [ ! -e $file1 ] then echo "File is not exit " elif [ ! -r $file1 ] then echo "File is not readable " elif [ ! -w $file1 ] then echo "File is not writable " else echo "file is readible and Writable " fi if [ -x $file1 ] then echo "File is exectuble " else echo "File is not executable" fi if [ -d $file1 ] then echo "File is a directory " else echo "File is nat a directory " fi echo "Do you want to continue Y|N please enter " read ch done

OUTPUT :

Q.11) Count the number of characters,line,and words.

Code:
#include<stdio.h> #include<fcntl.h> void main() { int fd,char1=0,words=0,lines=0; char a; fd=open("./a1.txt",O_RDONLY); if(fd==-1) { perror("file a1.txt"); } else { while(read(fd,&a,1)>0) { if(a==' '||a=='\n') { words++; } if(a=='\n') { lines++; } char1++; printf("%c",a); } printf("\nThe number of characters=%d\n",char1); printf("\nThe number of words=%d\n",words); printf("\nThe number of lines%d\n",lines); } close(fd); }

OUTPUT :

Q.12) Write a shellscript to count number of directories and regulars files in present Working directories Code:
#!bin/sh echo "The shell script to count number of directories and regular file in present working directory" numberofdir=0 numberoffile=0 for f1 in * do if test -e $f1 then if test -d $f1 then numberofdir=$(($numberofdir+1)) else numberoffile=$(($numberoffile+1)) fi fi done echo "Number of files =$numberoffile" echo "Number of directory=$numberofdir" echo "\n Directories and files are given bellow :" ls -l exit 0

OUTPUT :

Q.13) C program using error Handling system calls to display no. Of error available in operation system
Code: #include<stdio.h> int main() { int i; for(i=0;i<135;i++) { printf("\n%d:\t%s",i,strerror(i)); } }

OUTPUT :

Q.14) A shellscript to concate 2 user defined files


Code:
#!bin/sh for counter in $1 $2 do if [-f $counter] then cat $counter else echo "wrong file $counter" fi done

OUTPUT :

Anda mungkin juga menyukai