Systems Programming
Systems Programming
1. Write a shell script to determine whether a given number is a prime number or not.
a) Interactively.
Solution:
a) Interactively
#!/bin/bash
read n
do
ans=$(( n%i ))
if [ $ans -eq 0 ]
then
exit 0
fi
done
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
solution:
a) Interactive version
read file
Output:
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
read number
i=1
while [ $i -le 10 ]
do
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
read VAR1
read VAR2
read VAR3
then
else
fi
else
then
else
fi
fi
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()
char c;
exit(0);
fputc(c, fp3);
fputc(c, fp3);
fclose(fp1);
fclose(fp2);
fclose(fp3);
return 0;
Output:
Finish...