In: Computer Science
Design a shell program to remove all the shell
programming files ending with sh on your home directory when
a SIGINT signal is received.
I understand the problem, But dosent specified, from where do you get the SIGINT signal. So what I had done is to generate a SIGINT signal in the program (press ctrl+c) for that.
We can ignore the SIGINT with the command
trap '' SIGINT
To do something when a SIGINT command has occured , handle it with the command
trap handler SIGINT
and the function
handler()
{
echo "Do something here"
}
So to answer the question completely
handler()
{
echo "Removing all the bash script in home dir.... :"
rm -r ~/*.sh
echo "Removed all the bash script in home dir :"
}
while read -p "enter (ctrl+c) for SIGINT : " temporaryVariable
do
echo "you entered : $temporaryVariable"
trap handler SIGINT
done
save theis script anywhere other than the home directory(becase we need to remove all the sh files in home)
Explenations :
I had created three bash scripts in the home directory
Screenshot of the ls command (which list the .sh files)
Now I had placed the script in another directory and run it
screenshot the running of program
( make sure that you enter first an alphebet , only enter teh ctrl + c in next iteratioon)
I entered ctrl+c to generate the SIGINT
and ctrl+z to stop the execution of the script
Now I will again show the screenshot of the home directory
The highlighted output is the result of the command ls *.sh after executing the bash script.
-----------------------------------------------------------