1.
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
# check if correct number of arguments were passed
if [ $# -ne 3 ]; then
echo "Usage: $0 filename start_line end_line"
exit 1
fi
filename=$1
start_line=$2
end_line=$3
# check if the given file exists
if [ ! -f $filename ]; then
echo "File '$filename' does not exist"
exit 1
fi
# 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
# print the lines between the given line numbers
sed -n "${start_line},${end_line}p" $filename
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
# check if the word to be deleted is provided as argument
if [ $# -eq 0 ]; then
echo "Usage: $0 word_to_delete file1 file2 ..."
exit 1
fi
word_to_delete=$1
shift
# iterate over all the files provided as arguments
for file in "$@"
do
# check if the file exists
if [ ! -f $file ]; then
echo "File '$file' does not exist"
continue
fi
# 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
echo "Files with read, write, and execute permissions:"
echo "-----------------------------------------------"
# iterate over all the files in the current directory
for file in *
do
# check if the file has read, write, and execute permissions for the user
if [ -r $file ] && [ -w $file ] && [ -x $file ]; then
echo $file
fi
done
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
# iterate over all the arguments
for file in "$@"
do
# check if the argument is a file
if [ -f "$file" ]; then
# count the number of lines in the file
num_lines=$(wc -l < "$file")
echo "File: $file has $num_lines lines"
else
echo "$file is not a file"
fi
done
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
# check if a directory is provided as argument
if [ $# -eq 0 ]; then
echo "Usage: $0 directory_name"
exit 1
fi
directory_name=$1
# check if the provided directory exists
if [ ! -d $directory_name ]; then
echo "Directory '$directory_name' does not exist"
exit 1
fi
# list all the files (including directories) in the directory
ls -al $directory_name
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
}
# check each line in the file
{
# check if the line does not contain vowels
if (tolower($0) !~ /[aeiou]/) {
# if the line does not contain vowels, increment the count
count++
}
}
END {
# print the final count
print "Number of lines without vowels: " count
}
OUTPUT:
chmod +x count_lines_without_vowels.awk
cat file.txt | ./count_lines_without_vowels.awk
7. Write a shell script that accepts a list of files names as its arguments,, counts and reports
the occurrence of each word that is present in the first arguments file on other arguments
files.
#!/bin/bash
# check if at least two arguments are provided
if [ $# -lt 2 ]; then
echo "Usage: $0 file1.txt file2.txt file3.txt ..."
exit 1
fi
# extract the first argument file
first_file=$1
# check if the first argument file exists
if [ ! -f $first_file ]; then
echo "File '$first_file' does not exist"
exit 1
fi
# initialize an associative array to store the word counts
declare -A word_counts
# iterate over the remaining argument files
for file in "${@:2}"
do
# check if the argument file exists
if [ ! -f $file ]; then
echo "File '$file' does not exist"
continue
fi
# iterate over each word in the first argument file
while read -r word; do
# count the number of occurrences of the word in the current file
count=$(grep -o "\<$word\>" "$file" | wc -l)
# add the count to the word_counts array
if [ -n "${word_counts[$word]}" ]; then
word_counts[$word]=$((word_counts[$word] + count))
else
word_counts[$word]=$count
fi
done < "$first_file"
done
# print the word counts
for word in "${!word_counts[@]}"
do
echo "$word: ${word_counts[$word]}"
done
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
# iterate over each argument
for arg in "$@"
do
# check if the argument is a file or directory
if [ -f "$arg" ]; then
echo "File: $arg"
# display file type
file "$arg"
# display number of links
echo "Number of links: $(stat -c '%h' "$arg")"
# display read, write and execute permissions
echo "Permissions: $(stat -c '%a' "$arg")"
elif [ -d "$arg" ]; then
echo "Directory: $arg"
# display number of links
echo "Number of links: $(stat -c '%h' "$arg")"
# display read, write and execute permissions
echo "Permissions: $(stat -c '%a' "$arg")"
else
echo "$arg is not a file or directory"
fi
done
OUTPUT:
chmod +x file_info.sh
./file_info.sh file1.txt dir1 file2.txt
9. Write a C program to list for every files in a directory, its inode number and files name.
#include <stdio.h>
#include <stdlib.h>
#include <dirent.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
DIR *dir;
struct dirent *entry;
struct stat file_stat;
if (argc != 2) {
fprintf(stderr, "Usage: %s directory_name\n", argv[0]);
exit(EXIT_FAILURE);
}
if ((dir = opendir(argv[1])) == NULL) {
perror("opendir");
exit(EXIT_FAILURE);
}
while ((entry = readdir(dir)) != NULL) {
char filename[1024];
snprintf(filename, 1024, "%s/%s", argv[1], entry->d_name);
if (stat(filename, &file_stat) == -1) {
perror("stat");
continue;
}
printf("%ld %s\n", file_stat.st_ino, entry->d_name);
}
closedir(dir);
return 0;
}
10.Implement in C the following Unix command using System calls cat,ls,mv
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char *argv[]) {
int fd1[2], fd2[2];
pid_t pid;
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]);
// wait for all child processes to finish
wait(NULL);
wait(NULL);
wait(NULL);
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>
#define FIFO_NAME "/tmp/myfifo"
#define BUFFER_SIZE 1024
int main() {
pid_t pid1, pid2;
int fd;
char buffer[BUFFER_SIZE];
// Create the named pipe
if (mkfifo(FIFO_NAME, 0666) == -1) {
perror("mkfifo");
exit(EXIT_FAILURE);
}
// Fork the first child process (writer)
pid1 = fork();
if (pid1 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid1 == 0) {
// Child process 1 (writer)
printf("Writer process (PID %d) is running.\n", getpid());
// Open the named pipe for writing
fd = open(FIFO_NAME, O_WRONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
// Write a message to the named pipe
strcpy(buffer, "Hello, world!");
if (write(fd, buffer, strlen(buffer) + 1) == -1) {
perror("write");
exit(EXIT_FAILURE);
}
printf("Writer process (PID %d) wrote message: %s\n", getpid(), buffer);
// Close the named pipe
close(fd);
exit(EXIT_SUCCESS);
}
// Fork the second child process (reader)
pid2 = fork();
if (pid2 == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid2 == 0) {
// Child process 2 (reader)
printf("Reader process (PID %d) is running.\n", getpid());
// Open the named pipe for reading
fd = open(FIFO_NAME, O_RDONLY);
if (fd == -1) {
perror("open");
exit(EXIT_FAILURE);
}
// Read a message from the named pipe
if (read(fd, buffer, BUFFER_SIZE) == -1) {
perror("read");
exit(EXIT_FAILURE);
}
printf("Reader process (PID %d) read message: %s\n", getpid(), buffer);
// Close the named pipe
close(fd);
exit(EXIT_SUCCESS);
}
// Wait for both child processes to complete
waitpid(pid1, NULL, 0);
waitpid(pid2, NULL, 0);
// Remove the named pipe
if (unlink(FIFO_NAME) == -1) {
perror("unlink");
exit(EXIT_FAILURE);
}
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
if [ $num1 -gt $num2 ] && [ $num1 -gt $num3 ]
then
echo "$num1 is the largest number."
elif [ $num2 -gt $num1 ] && [ $num2 -gt $num3 ]
then
echo "$num2 is the largest number."
else
echo "$num3 is the largest number."
fi
OUTPUT:
14.Write a shell script to check if a particular user has logged in or not.
#!/bin/bash
echo "Enter the username you want to check:"
read username
if who | grep -wq $username
then
echo "$username has logged in."
else
echo "$username has not logged in."
fi
15.Write a shell script to check whether a file is existing or not
#!/bin/bash
echo "Enter the file name you want to check:"
read filename
if [ -e "$filename" ]
then
echo "$filename exists."
else
echo "$filename does not exist."
fi