055CS59 Linux&Unix Lab Manual
055CS59 Linux&Unix Lab Manual
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.
#!/bin/bash
filename=$1
start_line=$2
end_line=$3
# check if the start line number is less than the end line number
if [ $start_line -gt $end_line ]; then
echo "Starting line number should be less than the ending line number"
exit 1
fi
OUTPUT:
chmod +x display_lines.sh
./display_lines.sh filename.txt 5 10
2.Write a shell script that deletes all lines containing a specified word in one or more files
supplied as arguments to it.
#!/bin/bash
word_to_delete=$1
shift
# delete all lines containing the word to be deleted from the file
sed -i "/$word_to_delete/d" $file
done
OUTPUT:
chmod +x delete_lines.sh
./delete_lines.sh word_to_delete file1.txt file2.txt
3. Write a shell script that displays a list of all the files in the current directory to which the
user has read,write and execute permissions.
#!/bin/bash
OUTPUT:
chmod +x permissions.sh
./permissions.sh
4.Write a shell script that receives any number of files names as arguments check if every
argument is a file, the number of lines on it is also reported.
#!/bin/bash
OUTPUT:
chmod +x count_lines.sh
./count_lines.sh file1.txt file2.txt file3.txt
5. Write a shell script to list all of the directory files in a directory.
#!/bin/bash
directory_name=$1
OUTPUT:
chmod +x list_files.sh
./list_files.sh directory_name
6.Write an awk script to count the number of lines in a file that do not contain vowels
#!/usr/bin/awk -f
BEGIN {
# initialize the line count to zero
count = 0
}
END {
# print the final count
print "Number of lines without vowels: " count
}
OUTPUT:
chmod +x count_lines_without_vowels.awk
#!/bin/bash
OUTPUT:
./count_words.sh file1.txt file2.txt file3.txt
8. Write a program that takes one or more files/directory names as command line input and
reports the following information on the files.file type,Number of links and read,write and
execute permissions
#!/bin/bash
OUTPUT:
chmod +x file_info.sh
if (argc != 2) {
fprintf(stderr, "Usage: %s directory_name\n", argv[0]);
exit(EXIT_FAILURE);
}
closedir(dir);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
if (pipe(fd1) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
// child process
close(fd1[0]);
dup2(fd1[1], STDOUT_FILENO);
execlp("cat", "cat", "file1.txt", NULL);
perror("execlp");
exit(EXIT_FAILURE);
}
if (pipe(fd2) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
// child process
close(fd1[1]);
dup2(fd1[0], STDIN_FILENO);
close(fd2[0]);
dup2(fd2[1], STDOUT_FILENO);
execlp("ls", "ls", "-l", NULL);
perror("execlp");
exit(EXIT_FAILURE);
}
close(fd1[0]);
close(fd1[1]);
pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
// child process
close(fd2[1]);
dup2(fd2[0], STDIN_FILENO);
int fd_out = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
dup2(fd_out, STDOUT_FILENO);
execlp("mv", "mv", "-", NULL);
perror("execlp");
exit(EXIT_FAILURE);
}
close(fd2[0]);
close(fd2[1]);
return 0;
}
11.Write a C program to create a Zombie process.d
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) {
// child process
printf("Child process (PID %d) is running.\n", getpid());
exit(EXIT_SUCCESS);
} else {
// parent process
printf("Parent process (PID %d) is exiting without waiting for child (PID %d).\n", getpid(),
pid);
exit(EXIT_SUCCESS);
}
return 0;
}
12.Write C program that illustrate communication between two unrelated processes using named
pipe.Write a C program (sender.c) to create a message queue with read and write permissions to
write 3 messages to it with different priority numbers
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
int main() {
pid_t pid1, pid2;
int fd;
char buffer[BUFFER_SIZE];
if (pid1 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid1 == 0) {
// Child process 1 (writer)
printf("Writer process (PID %d) is running.\n", getpid());
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
if (pid2 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid2 == 0) {
// Child process 2 (reader)
printf("Reader process (PID %d) is running.\n", getpid());
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
return 0;
}
13.Write a shell script to accept three numbers and display the largest
#!/bin/bash
echo "Enter the first number:"
read num1
echo "Enter the second number:"
read num2
echo "Enter the third number:"
read num3
OUTPUT:
14.Write a shell script to check if a particular user has logged in or not.
#!/bin/bash
#!/bin/bash
if [ -e "$filename" ]
then
echo "$filename exists."
else
echo "$filename does not exist."
fi