Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent a user from using ctrl-c to stop a script?

Tags:

bash

shell

I load a script in .bash_profile and this script will ask for right password whenever a user opens a terminal window. If the user enters a wrong code, the script will run exit to stop the current terminal.

if [ $code = "980425" ]; then
    echo hello
else
    exit
fi

But I realize that the user can always use ctrl-c to stop the script and enter the terminal. How to avoid that?

like image 988
Patroclus Avatar asked May 10 '16 20:05

Patroclus


Video Answer


2 Answers

You can always trap SIGINT:

trap 'echo got SIGINT' SIGINT

Once you're done, reinstall the default handler again with

trap SIGINT

See the POSIX spec for trap for details. This works in all Bourne shells, not just bash.

like image 56
Jens Avatar answered Sep 24 '22 14:09

Jens


You can use:

trap '' 2 
commands
trap 2

This disables signal 2 (control c) and then re-enables it after the command has run.

like image 37
Heidi Negrete Avatar answered Sep 25 '22 14:09

Heidi Negrete