SP ASSIGNMENT
1. Write a shell script to determine whether a given number is a prime number or not.
a) Interactively.
b) By command line arguments.
Solution:
a) Interactively
#!/bin/bash
echo -e "Enter Number : \c"
read n
for((i=2; i<=$n/2; i++))
do
ans=$(( n%i ))
if [ $ans -eq 0 ]
then
echo "$n is not a prime number."
exit 0
fi
done
echo "$n is a prime number."
b) Command line argument: 1 4
Output:
1 is prime number.
4 is not a prime number.
2.Write a shell script which counts the number of lines and number of words present in a given file.
a) Interactive version
b) Command Line arguments version
solution:
a) Interactive version
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
b) Command line argument: File1
Output:
File1 has been created.
After executing the codes it will ask to enter filename.
Our filename is File1.
File1 will shows the number of characters, words and lines.
3. Write a shell script to generate a multiplication table.
a) Interactive version: The program should accept an integer n given by the user and should print the
multiplication table of that n.
b) Command line arguments version: The program should take the value of n from the arguments
followed by the command.
c) Redirection version: The value of n must be taken from a file using input redirection.
solution:
a)
clear
echo "which number to generate multiplication table"
read number
i=1
while [ $i -le 10 ]
do
echo " $number * $i =`expr $number \* $i ` "
i=`expr $i + 1`
done
b)Command line arguments : 7 8
output:
4. Write a shell script using nested if-else with comparison operators of your own and explain each
statement of that script.
Solution:
#!/bin/bash
echo -n "Enter the first number: "
read VAR1
echo -n "Enter the second number: "
read VAR2
echo -n "Enter the third number: "
read VAR3
if [[ $VAR1 -ge $VAR2 ]]
then
if [[ $VAR1 -ge $VAR3 ]]
then
echo "$VAR1 is the largest number."
else
echo "$VAR3 is the largest number."
fi
else
if [[ $VAR2 -ge $VAR3 ]]
then
echo "$VAR2 is the largest number."
else
echo "$VAR3 is the largest number."
fi
fi
Command line Argument: 10 20 30
Output:
5.Write a program to read all txt files (that is files that ends with .txt) in the current directory and
merge them all to one txt file and returns a file descriptor for the new file.
Solution:
#include <stdio.h>
#include <stdlib.h>
int main()
// Open two files to be merged
FILE *fp1 = fopen("file1.txt", "r");
FILE *fp2 = fopen("file2.txt", "r");
// Open file to store the result
FILE *fp3 = fopen("file3.txt", "w");
char c;
if (fp1 == NULL || fp2 == NULL || fp3 == NULL)
puts("Could not open files");
exit(0);
// Copy contents of first file to file3.txt
while ((c = fgetc(fp1)) != EOF)
fputc(c, fp3);
// Copy contents of second file to file3.txt
while ((c = fgetc(fp2)) != EOF)
fputc(c, fp3);
printf("Merged file1.txt and file2.txt into file3.txt");
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
Output:
Merged file1.txt and file2.txt into file3.txt
Finish...