Beruflich Dokumente
Kultur Dokumente
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.
bash-3.2$ vi lp1.sh
echo "enter the filename"
read fname
read s
a=`expr $s + 1`
read n
b=`expr $n - 1`
cat newline
output:
bash-3.2$ sh lp1.sh
sales.dat
1 computers 9161
1 textbooks 21312
2 clothing 3252
2. Write a shell script that deletes all lines containing a specified word in one or more files
supplied as arguments to it.
## for this program we have to create one or more files (optional),
## I am creating two files names are del ,dell.
bash-3.2$ vi del
unix is os
dos is also os
here using unix
unix is powerful os
~
bash-3.2$ vi dell
windowsnt is also os
there are some difference between unix and windowsnt
but unix is great among all os
## after creation two files now we have to write sed script file name is del.sed using vi editor.
bash-3.2$ vi del.sed
{
/os/d
}
output:
bash-3.2$ sed -f del.sed del dell
here using unix
3. Write a shell script that displays a list of all the files in the current directory to which the user
hasread, write and execute permissions.
bash-3.2$ vi lp3.sh
bash-3.2$ cd cse
bash-3.2$ ls
chap1 chap2 f file1 file2
bash-3.2$ chmod 777 file1
bash-3.2$ cd ..
bash-3.2$ sh lp3.sh
enter the directory name
cse
chap1 not having all permissions
chap2 not having all permissions
f not having all permissions
file1 has all permissions
file2 not having all permissions
4. Write a shell script that receives any number of file names as arguments checks if every
argument supplied is a file or a directory and reports accordingly. Whenever the argument is a
file, the number of lines on it is also reported.
bash-3.2$ vi lp4.shl
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
output:
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.
bash-3.2$ vi lp5.sh
if [ $# -ne 2 ]
then
echo "Error : Invalid number of arguments."
exit
fi
str=`cat $1 | tr '\n' ' '`
for a in $str
do
echo "Word = $a, Count = `grep -c "$a" $2`"
done
output:
bash-3.2$ cat>test
hello adams
bash-3.2$ cat>test1
hello adams
hello adams
hello
hai r u
hello
bash-3.2$ sh lp5.sh test test1
Word = hello, Count = 4
Word = adams, Count = 2
bash-3.2$ sh lp6.sh
enter directory name
cse
list of files in the directory
chap1 chap2 f file1 file2 lp2.sh
bash-3.2$ sh fact.sh
enter a number
5
factorial of 5 is 120
8. Write an awk script to count the number of lines in a file that do not contain vowels.
9. Write an awk script to find the number of characters, words and lines in a file.
bash-3.2$ vi count.awk
total
characters : 63
words: 12
lines : 3
8. Write an awk script to count the number of lines in a file that do not contain vowels.
-bash-3.2$ vi lp8.sh
#!/bin/bash # Direct the script to use bash shell for its execution
Output
10. Write a c program that makes a copy of a file using standard I/O and system calls
bash-3.2$ vi lp10.c
#include<stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[])
{
int fd1, fd2;
char buffer[100];
long int n1;
if(((fd1 = open(argv[1], O_RDONLY)) == -1) || ((fd2=open(argv[2],O_CREAT|O_WRONLY|
O_TRUNC,
0700)) == -1)){
perror("file problem ");
exit(1);
}
while((n1=read(fd1, buffer, 100)) > 0)
if(write(fd2, buffer, n1) != n1){
perror("writing problem ");
exit(3);
}
}
output:
-bash-3.2$ cat del
unix is os
dos is also os
here using unix
unix is powerful os
-bash-3.2$ cc lp10.c
a. Cat
b. ls
c. mv.
-bash-3.2$ vi lp2.c
#include<sys/types.h>
#include<sys/stat.h>
#include<stdio.h>
#include<fcntl.h>
main( int argc,char *argv[] )
{
int i,fd1,fd2;
char *file1,*file2,buf[2];
file1=argv[1];
file2=argv[2];
printf("file1=%s file2=%s \n",file1,file2);
fd1=open(file1,O_RDONLY,0777);
fd2=creat(file2,0777);
while(i=read(fd1,buf,1)>0)
write(fd2,buf,1);
remove(file1);
close(fd1);
close(fd2);
}
Output
-bash-3.2$ cc lp2.c
-bash-3.2$ ./a.out random sample
file1=random file2=sample
-bash-3.2$ cat sample
this is a random file
-bash-3.2$ cat random
cat: random: No such file or directory
-bash-3.2$ vi lp3.c
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/param.h>
#include <stdio.h>
#define FALSE 0
#define TRUE 1
extern int alphasort();
char pathname[MAXPATHLEN];
main() {
int count,i;
struct dirent **files;
int file_select();
if (getwd(pathname) == NULL )
{ printf("Error getting pathn");
exit(0);
}
printf("Current Working Directory = %sn",pathname);
count = scandir(pathname, &files, file_select, alphasort);
if (count <= 0)
{ printf("No files in this directoryn");
exit(0);
}
printf("Number of files = %dn",count);
for (i=1;i<count+1;++i)
printf("%s \n",files[i-1]->d_name);
}
int file_select(struct direct *entry)
{
if ((strcmp(entry->d_name, ".") == 0) ||(strcmp(entry->d_name, "..") == 0))
return (FALSE);
else
return (TRUE); }
Output
-bash-3.2$ cc lp3.c
-bash-3.2$ ./a.out
12. Write a program that takes one or more file/directory names as command line input and
reports the following information on the file.
A. File type. B. Number of links.
C. Time of last access. D. Read, Write and Execute permissions.
-bash-3.2$ vi lp12.c
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<time.h>
int main();
{
int isdevice = FALSE;
struct stat stat_buf;
if (argc != 2)
{
printf("Usage: %s filename\n", argv[0]);
exit (1);
}
if ( stat( argv[1], &stat_buf) == ERR)
{
perror("stat");
exit (1);
}
printf("\nFile: %s status:\n\n",argv[1]);
if ((stat_buf.st_mode & S_IFMT) == S_IFDIR)
printf("Directory\n");
else if ((stat_buf.st_mode & S_IFMT) == S_IFBLK)
{
printf("Block special file\n");
isdevice = TRUE;
}
else if ((stat_buf.st_mode & S_IFMT) == S_IFCHR)
{
printf("Character special file\n");
isdevice = TRUE;
}
else if ((stat_buf.st_mode & S_IFMT) == S_IFREG)
printf("Ordinary file\n");
else if ((stat_buf.st_mode & S_IFMT) == S_IFIFO)
printf("FIFO\n");
if (isdevice)
printf("Device number:%d, %d\n", (stat_buf.st_rdev > 8) & 0377,
stat_buf.st_rdev & 0377);
exit (0);
}
Output
-bash-3.2$ cc lp12.c
-bash-3.2$ ./a.out del
Ordinary file
I-node: 23861966; Links: 1; Size: 64
file last accessed:Thu Aug 28 14:52:44 2014
Permissions: 644
13. Write a C program that redirects a standard output to a file. Ex: ls >f1.
-bash-3.2$ vi lp13.c
Output
-bash-3.2$ cc lp13.c
-bash-3.2$ ./a.out
-bash-3.2$ cat myfile.txt
This sentence is redirected to a file.
14. Write a C program to list for every file in a directory, its inode number and file name.
-bash-3.2$ vi lp14.c
printf("%s", success_msg);
return 1;
}
void print_error(int error_index, int exit_code)
{
fprintf(stderr, "%s", error_msg[error_index]);
exit(exit_code);
Output
-bash-3.2$ cc lp14.c
-bash-3.2$ ./a.out
-bash-3.2$ vi lp14.c