In: Computer Science
Design two shell programs working on Linux (Ubuntu)
Take a screenshot showing your shell program and its execution step.
Take three screenshots (The first screenshot shows one or more sh files on your home directory, and the second screenshot shows your shell program and its execution step, the third screenshot shows that sh files do not exist in your home directory after executing your shell program).
Submit your four screenshots.
First program
------------------------------
#! /bin/sh
required=2
if [ $# -eq $required ]
then
result = `expr $1 \* $2`
echo "$result"
else
echo "Wrong Input"
fi
----------------------------
In this program ,number of command line agruments is given by $# and is compared by required
then to calculate an expression expr is used as in script above
Screenshot
Program 2
-------------------------------------
#! /bin/sh
directory = "/home/viju4076" #home directory of user
echo "The PID is $$" #required for killing the process
sigint()
{
echo "SIGINT signal received"
for filename_full in "$directory"/*
do
filename= $(basename -- "$filename_full")
extension="${filename##*.}"
if [ "$extension" = "sh" ]
then
rm "$filename_full"
fi
done
exit 0
}
trap 'sigint' INT
while true ; do
sleep 30
done
-------------------------------
In this program first we store the required path in directory variable ..Then display the PID so that signal could be send to this process by another terminal or one could use ctrl+C which also gives the same signal to the process.
Then the required work is written in sigint() function ..which gets call when signal SIGINT is received..then in function ..traverse each file get the extension and if it matches with sh then remove the file by rm command...
at last run a loop infinte times so that SIGINT can be received before ending of the script..
Screenshots