NP Record
NP Record
NP Record
Write a shell script that accepts a file name, starting and ending line numbers as arguments and displays all the lines between the given line numbers. */
echo "enter the filename" readfname echo "enter the starting line number" read s echo "enter the ending line number" read n sed -n $s,$n\p $fname | cat > newline cat newline
1. Input :
jharvard@appliance (~/Desktop/Nplab): chmod 755 1.sh jharvard@appliance (~/Desktop/Nplab): sh 1.sh Enter the file name > 10.c Enter the starting line number> 1 Enter the ending line number> 10
1. Output :
#include<stdio.h> #include<fcntl.h> int main(int argc, char *argv[10]) { int source, destination, n; char buf[256]; if(argc!=3) { printf("No sufficient Arguments \n");
/* 2. Write a shell script that deletes all lines containing a specified word in one or more files supplied as arguments to it. */
if [ $# -eq 0 ] then echo "no arguments" else echo "enter a deleting word or char" read y fori in $* do grep -v "$y" "$i" > temp if [ $? -ne 0 ] then echo "pattern not found" else cp temp $i rm temp fi done fi
2. Input :
Chmod 755 2.sh Sh 2.sh 1.sh 2. Output : read fname read s read n sed -n $s,$n\p $fname | cat > newline cat newline
2
/*3. Write a shell script that displays a list of all the files in he current directory to which the user has read write and execute permissions. */
echo "enter the directory name" read dir if [ -d $dir ] then cd $dir ls > f exec < f while read line do if [ -f $line ] then if [ -r $line -a -w $line -a -x $line ] then echo "$line has all permissions" else echo "$line files not having all permissions" fi fi done fi
3. Input :
jharvard@appliance (~/Desktop/Nplab): chmod 755 3.sh jharvard@appliance (~/Desktop/Nplab): sh 3.sh enter the directory name Home
3. Output :
10.c files not having all permissions 3.sh has all permissions 4.sh has all permissions Sum.c files not having all permissions 3.sh: line 10: [: too many arguments XDG_VTNR=1 files not having all permissions a.out has all permissions core files not having all permissions f files not having all permissions file files not having all permissions file.sh has all permissions newline files not having all permissions
/* 4. Write a shell script that receives any number of file names as arguments checks if every argument supplied is file or a directory and reports accordingly. */
for x in $* do if [ -f $x ] then echo " $x is a file " echo " no of lines in the file are " wc -l $x elif [ -d $x ] then echo " $x is a directory " else echo " enter valid filename or directory name " fi done
4. Input :
jharvard@appliance (~/Desktop/Nplab): chmod 755 4.sh jharvard@appliance (~/Desktop/Nplab): sh 4.sh jharvard@appliance (~/Desktop/Nplab): sh 4.sh 2.sh 3.sh Home
4. Output :
2.sh is a file no of lines in the file are 39 2.sh 3.sh is a file no of lines in the file are 20 3.sh Home is a directory
/*5. Write a shell script that accepts a list of file names as its arguments, counts and reports the occurrence of each word that is present in the first argument file on other argument files. */
i=1 cnt=`wc -l wordfile | cut -c 6-7` echo $cnt cp wordfile filefor22 while [ $i -le $cnt ] do str=`head -1 filefor22` cnt1=`grep -c $str $1` echo $str $cnt1 ((c=$i+1)) tail +$c wordfile > filefor22 let i++ done
5.Output : chmod 755 5.sh $ cat wordfile apple mango banana $ cat wordfile1 apple chickoo apple banana $ sh27 wordfile1 3 apple 2
6
mango 0 banana 1
/*6. Write a shell script that accepts any number of arguments and prints them in reverse order . */
if [ $# -eq 0 ] then exit fi echo "no of arguments: $#" echo "the input arguments are" num=1
echo "arguments in reverse order" num=$# while [ $num -ne 0 ] do eval echo "arg$num is \$$num" num=`expr $num - 1` done
arguments in reverse order arg4 is arg4 arg3 is arg3 arg2 is arg2 arg1 is arg1
7. Output :
chmod 755 fact.sh ./f.sh Enter a number: 5 Factorial of 5 is:120
10
Output :
Chmod 755 8.sh Sh 8.sh Enter the first number > 5 Enter the second number > 125 Greatest common divisor is 5
11
Output :
jharvard@appliance (~/Desktop): chmod 755 11.sh jharvard@appliance (~/Desktop): sh 11.sh Enter new Directory navc Enter umber of files > 1 Enter file name > pc.c
12
Output :
jharvard@appliance (~/Desktop): chmod 755 10.sh jharvard@appliance (~/Desktop): sh 10.sh Multiplication Table > enter the value of n: 5 5*1=5 5 * 2 = 10 5 * 3 = 15 5 * 4 = 20 5 * 5 = 25 5 * 6 = 30 5 * 7 = 35 5 * 8 = 40 5 * 9 = 45 5 * 10 = 50
13
/* 11. Write a shell script that counts the number of lines and words present in a given file */
echo Enter the filename read file w=`cat $file | wc -w` c=`cat $file | wc -c` l=`grep -c "." $file` echo Number of characters in $file is $c echo Number of words in $file is $w echo Number of lines in $file is $l
Output :
jharvard@appliance (~/Desktop): sh 11.sh Enter the filename wordfile.docx Number of characters in wordfile.docx is 36719 Number of words in wordfile.docx is 692 Number of lines in wordfile.docx is 117
14
/* 12. Write a shell script that displays the list of all files in the given directory */
# !/bin/bash echo "enter directory name" read dir if [ -d $dir ] then echo "list of files in the directory" ls $dir else echo "enter proper directory name" fi
Output :
jharvard@appliance (~/Desktop): chmod 755 12.sh jharvard@appliance (~/Desktop): sh 12.sh enter directory name nav list of files in the directory 1.sh 2.sh 5.sh 8.sh
15
/* 13. Write a shell script that adds, substracts, multiplies, and divides the given two integers. */
input="y" while [[ $input = "y" ]] do echo "------------" echo "Calculator" echo "------------" PS3="Press 1 for Addition, 2 for subtraction, 3 for multiplication and 4 for Quotient 5 for Remainder: " select math in Addition Subtraction Multiplication Quotient Remainder do case "$math" in Addition) echo "Enter first no:" read num1 echo "Enter second no:" read num2 result=`expr $num1 + $num2` echo Answer: $result break ;; Subtraction) echo "Enter first no:" read num1 echo "Enter second no:" read num2 result=`expr $num1 - $num2` echo Answer: $result break ;; Multiplication) echo "Enter first no:" read num1 echo "Enter second no:" read num2 result=`expr $num1 * $num2` echo Answer: $result break ;; Quotient) echo "Enter first no:" read num1 echo "Enter second no:" read num2 result=$(expr "scale=2; $num1/$num2" | bc) echo Answer = $result break ;; Remainder) echo "enter the first no:"
16
read num1 echo "enter second no:" read num2 result=$(expr " $num1%$num2" | bc) echo Answer= $result break ;; *) echo Choose 1 to 5 only!!!! break ;; esac done echo "Do you want to calculate again(y/no):" read input echo "Thank you for using this program" done
Output :
calculator -----------1) Addition 3) Multiplication 5) Remainder 2) Subtraction 4) Quotient Press 1 for Addition, 2 for subtraction, 3 for multiplication and 4 for Quotient 5 for Remainder: 2 Enter first no: 3 Enter second no: 4 Answer: -1
17
/*14 .Write a C program that counts the number of blanks in a text file.( Using System calls )*/
using system calls. #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include<fcntl.h> #include<sys/stat.h> main(intargc, char *argv[]) { int fd1; char buf[520]; intch,i,j=0; if(argc!=2) { printf("give filename as input"); exit(0); } else { fd1=open(argv[1],O_RDONLY ); while((ch=read(fd1,buf,512))>0) { for(i=0;i<ch;i++) if(buf[i]==' ') j=j+1; } printf("the no of blanks are : %d\n",j); } }
Output :
cse@cse-Veriton-M670:~/Desktop$ cc bla.c cse@cse-Veriton-M670:~/Desktop$ ./a.outls.c the no of blanks are : 8
18
/* 15. Write a C program that counts the number of blanks in a text file.( Using standard I/O) */
using standard I/O #include<stdio.h> #include<stdlib.h> #include<fcntl.h> main(intargc,char * argv[]) {
intch; inti=0; FILE *fp1; fp1=fopen(argv[1],"r"); if(fp1==NULL) { printf("file does not exist"); exit(1); } while((ch=getc(fp1))!=EOF) if(ch==' ') i=i+1; printf("the no of blank lines is %d",i); }
Output:
cse@cse-Veriton-M670:~/Desktop$ cc io.c cse@cse-Veriton-M670:~/Desktop$ ./a.outls.c the no of blank lines is 8
19
} return(0); }
Output :
jharvard@appliance (~/Desktop): ./a.out The correct syntax is lsdirname
21
/* 17 .Write a C program to create a child process and allow the parent to display parent and the chil to display child on the screen. */
#include <stdio.h> #include <sys/types.h> #define MAX_COUNT 10 void ChildProcess(void); void ParentProcess(void); void main(void) { pid_tpid; pid = fork(); if (pid == 0) ChildProcess(); else ParentProcess(); } void ChildProcess(void) { inti; for (i = 1; i<= MAX_COUNT; i++) printf(" This line is from child, value = %d\n", i); printf(" *** Child process is done ***\n"); } void ParentProcess(void) { inti; for (i = 1; i<= MAX_COUNT; i++) printf("This line is from parent, value = %d\n", i); printf("*** Parent is done ***\n"); } /* child process prototype */ /* parent process prototype */
22
Output :
cse@cse-AcerPower-Series:~/Desktop$ gccparent.c cse@cse-AcerPower-Series:~/Desktop$ ./a.out This line is from parent, value = 1 This line is from parent, value = 2 This line is from child, value = 1 This line is from parent, value = 3 This line is from child, value = 2 This line is from parent, value = 4 This line is from child, value = 3 This line is from parent, value = 5 This line is from child, value = 4 This line is from parent, value = 6 This line is from child, value = 5 This line is from parent, value = 7 This line is from child, value = 6 This line is from parent, value = 8 This line is from child, value = 7 This line is from parent, value = 9 This line is from child, value = 8 This line is from parent, value = 10 This line is from child, value = 9 This line is from child, value = 10 *** Parent is done *** *** Child process is done ***
23
Output :
cc orphan.c cse@cse-AcerPower-Series:~/Desktop$ ./a.out Parent PID:4847 Parent PPID:4769 Child PID:4848 Child PPid:4847 Parent Terminated!!
24
/*19. Write a C program that displays the real time of the day 10times */
#include <stdio.h> #include <sys/time.h> #include <sys/signal.h> /* Declarations */ void main(); int times_up(); void main() { int a; a=1;
int times_up(sig) int sig; { long now; long time(struct tms *ptr); char *ctime(); time (&now); printf("It is now %s\n", ctime (&now)); return (sig); }
Output :
jharvard@appliance (~/Desktop): ./a.out It is now Sun Mar 31 09:29:07 2013 It is now Sun Mar 31 09:29:07 2013 It is now Sun Mar 31 09:29:07 2013
25
It is now Sun Mar 31 09:29:07 2013 It is now Sun Mar 31 09:29:07 2013 It is now Sun Mar 31 09:29:07 2013 It is now Sun Mar 31 09:29:07 2013 It is now Sun Mar 31 09:29:07 2013 It is now Sun Mar 31 09:29:07 2013 It is now Sun Mar 31 09:29:07 2013
26
27
28
29
30
31
32