Experiment 2
Experiment 2
Experiment No. 02
Grade: AA / AB / BB / BC / CC / CD /DD
AIM: To study the shell script and write the program using shell.
______________________________________________________________________
Expected Outcome of Experiment:
_____________________________________________________________________
Books/ Journals/ Websites referred:
The shell provides you with an interface to the UNIX system. It gathers input from you
and executes programs based on that input. When a program finishes executing, it
displays that program's output.
Shell Scripts
The basic concept of a shell script is a list of commands, which are listed in the order of
execution. A good shell script will have comments, preceded by a pound sign, #,
describing the steps.
2.Insert the script/ commands in file and save the file to execute the file we need to give
execute permission to the file
3.$ chmod 775 filename
4.Now execute the above file using any of following methods:
$ sh filename
OR
$ ./filename
NOTE: Before adding anything to your script, you need to alert the system that a shell
script is being started. This is done using the shebang construct. For example −
#!/bin/sh.
______________________________________________________________________
Description of the application to be implemented:
1. Write a shell Script that accepts two file names as command line arguments and
compare two file contents and check whether contents are same or not. If they are
same, then delete second file.
2. Write a shell script that accepts integer and find the factorial of number.
1. Write a Program for creating process using System call (E.g fork())
Create a child process. Display the details about that process using
getpid and getppid functions. In a child process, Open the file using file
system calls and read the contents and display.
1. compare.sh
rm -rf "$2"
else
fi
file1.txt
Hello world!
file2.txt
Hello world!
Output:
2. factorial.sh
fact=1
for((i=2;i<=num;i++))
{
fact=$((fact*i))
}
Output:
3. Adding users
5. Number of processes
Output:
System Call:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
int main(void) {
pid_t process_id = fork();
if(process_id == 0)
{
printf("\nChild process:\n");
char ch = fgetc(file_ptr);
while (ch != EOF)
{
printf ("%c", ch);
ch = fgetc(file_ptr);
}
fclose(file_ptr);
printf ("\n");
exit(1);
}
else {
printf("Cannot create child process\n");
}
Output:
Conclusion :
Learned and successfully implemented shell scripts and system call fork().